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 2005 Forums
 Transact-SQL (2005)
 Return Value in SQLCMD

Author  Topic 

karuna
Aged Yak Warrior

582 Posts

Posted - 2007-07-11 : 15:20:43
All-

How do I write the return value from the stored procedure when using SQLCMD.

Here is what I have.
SQLCMD -S<servername> -d<db> -U<user> -P<PASSWORD> -iP:\<folder1>\<folder2>\<filename>.sql -oP:\<folder1>\<folder2>\results.txt

I want to write the return value to the results text file. Is that possible?


Thanks
Karunakaran

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-11 : 15:31:41
All output from filename.sql will be put in the results.txt output file. Could you explain what you mean by return value?

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2007-07-11 : 15:48:37
the input file has the below lines which calls a stored proc

SET NOCOUNT ON
GO
SET ARITHABORT OFF
GO
exec sp_name paramvalue
GO


the structure of sp is like this.

CREATE PROCEDURE [dbo].[sp_name]
@paramvalue VARCHAR(30)
AS

BEGIN

BEGIN TRANSACTION

UPDATE TBL1
SET COL1 = T2.COL3
FROM
TBL1 T1 INNER JOIN
TBL2 T2 ON T1.COL1 = T2.COL1

IF @@ERROR <> 0
GOTO ERR_COL1

UPDATE TBL1
SET COL2 = T3.COL4
FROM
TBL1 T1 INNER JOIN
TBL3 T3 ON T1.COL1 = T3.COL1

IF @@ERROR <> 0
GOTO ERR_COL2

COMMIT TRANSACTION
RETURN 0 -- How do I capture this return value..

ERR_COL1:
SELECT @ERR_DESC = 'ERROR UPDATING COL1'
GOTO ERROR_HANDLER

ERR_COL2:
SELECT @ERR_DESC = 'ERROR UPDATING COL2'
GOTO ERROR_HANDLER

ERROR_HANDLER:
RAISERROR (@ERR_DESC,18,1)
ROLLBACK TRANSACTION
RETURN -1 -- How do I capture this return value..

END



Thanks
Karunakaran
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-11 : 15:53:05
You will need to print it out that way it goes in the file:

declare @rc int
exec @rc = sp_name paramvalue

<SELECT or PRINT> 'Return value: ' + CONVERT(varchar(5), @rc)

I believe both SELECT and PRINT will work, but please do test it out.


Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2007-07-11 : 16:09:20
quote:
Originally posted by tkizer

You will need to print it out that way it goes in the file:

declare @rc int
exec @rc = sp_name paramvalue

<SELECT or PRINT> 'Return value: ' + CONVERT(varchar(5), @rc)

I believe both SELECT and PRINT will work, but please do test it out.


Tara Kizer
http://weblogs.sqlteam.com/tarad/


Thanks Tara, That worked.

SELECT and PRINT works in a different way, i guess it depends on how the user wants.


SET NOCOUNT ON
GO
SET ARITHABORT OFF
GO
DECLARE @rc int
exec @rc = sp_name paramvalue
SELECT '(return status = '+CONVERT(VARCHAR,@rc)+')'
GO

Output in text file


------------------------------------------------
(return status = 0)


SET NOCOUNT ON
GO
SET ARITHABORT OFF
GO
DECLARE @rc int
exec @rc = sp_name paramvalue
PRINT'(return status = '+CONVERT(VARCHAR,@rc)+')'
GO

Output in text file

(return status = 0)


Thanks
Karunakaran
Go to Top of Page
   

- Advertisement -