Had to go back to the days I worked with batch files - and I think I've overcomplicated the solution...What I've come up with is to have the procedure return an output variable with the number of rows added and then display this valueThe batch file now looks something like this and the code to create the table and proc are below this@REM ***************************************************@ECHO OFFSET Server=malazanSET DB=ScratchPadSET SQL_SELECT=SET SQL_EXEC=SET LogFile=C:\Temp\Log.txtSET ResultsFile=C:\Temp\Results.txtSET SQL_SELECT=%SQL_SELECT% SET NOCOUNT ONSET SQL_SELECT=%SQL_SELECT% SELECT DISTINCT Col1,Col2 FROM TestSQLCMDTab@REM CALL SQLCMD -Q "exit( %SQL_SELECT% )" /b /S %Server% /d %DB% CALL OSQL -Q "exit( %SQL_SELECT% )" -S%Server% -d%DB% -E -bset /p var1= Enter Col1: set /p var2= Enter Col2: SET SQL_EXEC=%SQL_EXEC% SET NOCOUNT ONSET SQL_EXEC=%SQL_EXEC% DECLARE @RetVal intSET SQL_EXEC=%SQL_EXEC% DECLARE @RowsAffected intSET SQL_EXEC=%SQL_EXEC% EXEC @RetVal = TestSQLCMDSP %var1%,%var2%,@RowsAffected OUTSET SQL_EXEC=%SQL_EXEC% SELECT ISNULL(@RowsAffected,0) RowsAffected@REM CALL SQLCMD -Q "exit( %SQL_EXEC% )" /b /S %Server% /d %DB% > %ResultsFile% CALL OSQL -Q "exit( %SQL_EXEC% )" -S%Server% -d%DB% -E -b > %ResultsFile% @REM This outputs info from the file to the screenFOR /f "tokens=1* delims=*" %%i in (%ResultsFile%) do (echo %%i) @REM This appends info the fileFOR /f "tokens=1* delims=*" %%i in (%ResultsFile%) do (echo %%i) >> %LogFile%PAUSE@REM ***************************************************
Code for the procedure/table used in the batch file above:IF OBJECT_ID('dbo.TestSQLCMDTab') IS NOT NULLBEGIN DROP TABLE dbo.TestSQLCMDTab;ENDGOIF OBJECT_ID('dbo.TestSQLCMDSP') IS NOT NULLBEGIN DROP PROCEDURE dbo.TestSQLCMDSP;ENDCREATE TABLE dbo.TestSQLCMDTab(Col1 int NOT NULL, Col2 int NOT NULL);GOINSERT dbo.TestSQLCMDTab(Col1,Col2)SELECT 100,100GOCREATE PROCEDURE dbo.TestSQLCMDSP @Col1 int, @Col2 int, @RowsAffected int OUTPUTASBEGIN BEGIN TRY DECLARE @ErrMsg nvarchar(2048); IF @Col1 = @Col2 BEGIN INSERT dbo.TestSQLCMDTab(Col1,Col2) SELECT @Col1,@Col2; SET @RowsAffected = @@ROWCOUNT; END END TRY BEGIN CATCH SET @ErrMsg = ERROR_MESSAGE(); RAISERROR(@ErrMsg,16,1); END CATCHENDGO
Edit: The thread is named "osql" in .bat file - so replaced sqlcmd with osql