Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Osql in a .BAT file

Author  Topic 

woodsy1978
Starting Member

8 Posts

Posted - 2011-07-13 : 07:18:59
Hi All,

I have the script below which is performing correctly, but basically I want the .bat to ask the user if they want a list of casinos before entering the casino name and username, then if they select 'l' for list in produces the list of casinos. currently getting 'error converting exit value'. Can anyone tell me the code for this? many thanks

@echo off
osql -STEMP7 -E -dAAMS888 -w256 -qEXIT("SET NOCOUNT ON SELECT casino_desc from casino") -b

set /p var1= Enter Casino Name :
set /p var2= Enter Screen name :

osql -STEMP7 -E -dAAMS888 -w256 -QEXIT("DECLARE @r int EXEC @r = usp_AddToObservationtbl '%var1%','%var2%' SELECT @r") -b -oc:\bat\observation.log
exit errorlevel

vaari
Starting Member

15 Posts

Posted - 2011-07-13 : 08:32:36
I don't know why the error happens but if I use the -Q switch (instead of the -qEXIT or -QEXIT) I see that the error vanishes (and the -Q switch is supposed to execute the query provided and exit). I don't know if this actually solves your issue though....

Something like this:

@echo off
osql -STEMP7 -E -dAAMS888 -w256 -Q "SET NOCOUNT ON SELECT casino_desc from casino" -b

set /p var1= Enter Casino Name :
set /p var2= Enter Screen name :

osql -STEMP7 -E -dAAMS888 -w256 -Q "DECLARE @r int EXEC @r = usp_AddToObservationtbl '%var1%','%var2%' SELECT @r" -b -oc:\bat\observation.log
exit errorlevel
Go to Top of Page

woodsy1978
Starting Member

8 Posts

Posted - 2011-07-13 : 11:05:18
bingo, thanks alot vaari! I was trying to edit an old script I had so not sure what that part was for myself!

I just need to add one last section, how can I indicate if the stored procedure actually added a row to the table?
Go to Top of Page

vaari
Starting Member

15 Posts

Posted - 2011-07-13 : 13:44:59
quote:
I just need to add one last section, how can I indicate if the stored procedure actually added a row to the table?


I didn't understand this - do you want to return information in the batch file if the procedure added a row to a table?

Go to Top of Page

woodsy1978
Starting Member

8 Posts

Posted - 2011-07-14 : 04:06:07
Yes, I want to confirm if the stored procedure successfully added a row to the table or not
Go to Top of Page

vaari
Starting Member

15 Posts

Posted - 2011-07-14 : 06:58:14
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 value

The batch file now looks something like this and the code to create the table and proc are below this


@REM ***************************************************
@ECHO OFF

SET Server=malazan
SET DB=ScratchPad
SET SQL_SELECT=
SET SQL_EXEC=
SET LogFile=C:\Temp\Log.txt
SET ResultsFile=C:\Temp\Results.txt

SET SQL_SELECT=%SQL_SELECT% SET NOCOUNT ON
SET 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 -b

set /p var1= Enter Col1:
set /p var2= Enter Col2:

SET SQL_EXEC=%SQL_EXEC% SET NOCOUNT ON
SET SQL_EXEC=%SQL_EXEC% DECLARE @RetVal int
SET SQL_EXEC=%SQL_EXEC% DECLARE @RowsAffected int
SET SQL_EXEC=%SQL_EXEC% EXEC @RetVal = TestSQLCMDSP %var1%,%var2%,@RowsAffected OUT
SET 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 screen
FOR /f "tokens=1* delims=*" %%i in (%ResultsFile%) do (echo %%i)

@REM This appends info the file
FOR /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 NULL
BEGIN
DROP TABLE dbo.TestSQLCMDTab;
END
GO
IF OBJECT_ID('dbo.TestSQLCMDSP') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.TestSQLCMDSP;
END
CREATE TABLE dbo.TestSQLCMDTab(Col1 int NOT NULL, Col2 int NOT NULL);
GO
INSERT dbo.TestSQLCMDTab(Col1,Col2)
SELECT 100,100
GO
CREATE PROCEDURE dbo.TestSQLCMDSP @Col1 int, @Col2 int, @RowsAffected int OUTPUT
AS
BEGIN
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 CATCH
END
GO



Edit: The thread is named "osql" in .bat file - so replaced sqlcmd with osql
Go to Top of Page
   

- Advertisement -