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
 General SQL Server Forums
 New to SQL Server Programming
 How to use EXEC on a SP

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-03-14 : 06:22:17
Hi,

I'm trying to execute a simple stored procedure which returns a value but don't know how to see the value. The line is:

exec usp_COR_sel_tblCORAppeal_IDFromName 'E1021101X'.

But this just says 'command completed successfully.'

Just for info this is the SP:


ALTER PROCEDURE [dbo].[usp_COR_sel_tblCORAppeal_IDFromName]
(
@nName NVARCHAR(50) = 'Dev'
)
AS


RETURN (
SELECT AppealID
FROM tbl_COR_Appeal
WHERE ([Code] = @nName)
)

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-03-14 : 06:32:41
check your table whether it has a value 'Dev' for the column 'Code'
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-03-14 : 06:40:54
quote:
Originally posted by ahmeds08

check your table whether it has a value 'Dev' for the column 'Code'



I have checked the table for 'Dev' and 'E1021101X' both of which have a code but how do I see the code using exec? The exec statement is in a cursor and for some reason returns o as below:


FETCH NEXT FROM @cGifts INTO @iGiftID, @iCORWEBID, @nAppealCode, @tDate, @dAmount
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
-- Get PersonID
EXEC @iPersonID = usp_COR_sel_tblCORPerson_IDFromCorWebUrn @iCORWEBID
EXEC @iAppealID = usp_COR_sel_tblCORAppeal_IDFromName @nAppealCode

IF @iAppealID = 0
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-14 : 06:59:13
Can you post the complete code of SP ?
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-03-14 : 08:13:46
quote:
Originally posted by pk_bohra

Can you post the complete code of SP ?


If you don't mind trawling through the SP although if it makes sense to me then must be easy. I'm thinking it must be some anomally why it returns 0 but I would like to know how to run the exec command and see what it returns.

Anyway here is the Sp that tries to insert the data from an xml field into the gift table. In my situation it doesn't insert although an appeal code I'm using is in the appeal table. So it inserts data into the error table.

Inside the cursor in the SP the code calls another SP which checks if there is an appeal code for the given name (which I show in my first post). So I'm trying to figure out why but would like to know how to get the result of using exec command.


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO



----------------------------------------------------------------------------------------------------------------------------------------------------------
--
-- Name: dbo.usp_COR_ins_tblCORGiftOnline_FromXML
-- Author:
-- Date: June 2005
--
----------------------------------------------------------------------------------------------------------------------------------------------------------
ALTER PROCEDURE [dbo].[usp_COR_ins_tblCORGiftOnline_FromXML]
(
@iTransactionID INT ,
@nXMLTransaction NVARCHAR(4000)
)
AS

-- Declare Error variables -------------------------------------------------------------------------------------------------------------------
DECLARE @vUSER VARCHAR(128)
DECLARE @vPROC VARCHAR(128)
DECLARE @vACTION VARCHAR(10)
DECLARE @vTABLE VARCHAR(128)
DECLARE @vMessage VARCHAR(250)

-- Set error defaults
SET @vUSER = CURRENT_USER
SET @vPROC = 'dbo.usp_COR_ins_tblCORGiftOnline_FromXML'
SET @vACTION = 'Insert'
SET @vTABLE = 'tbl_COR_GiftOnline'

-- END OF Declare Error variables ------------------------------------------------------------------------------------------------------

-- Declare variables
DECLARE @iDoc INT -- Pointer to XML
DECLARE @iCORWEBID INT -- Cursor variable
DECLARE @nAppealCode NVARCHAR(50) -- Cursor variable
DECLARE @nInteractionType NVARCHAR(50) -- Cursor variable
DECLARE @dAmount NUMERIC(9,2) -- Cursor variable
DECLARE @tDate DATETIME -- Cursor variable
DECLARE @cGifts CURSOR -- Cursor
DECLARE @iPersonID INT -- Variable
DECLARE @iAppealID INT -- Variable
DECLARE @iReturn INT -- Variable
DECLARE @iGiftID INT -- Cursor variable

-- Create temp table
CREATE TABLE #GiftIns (
GiftID INT NULL ,
CORWebID INT NULL ,
AppealCode NVARCHAR(50) NULL,
[Date] DATETIME NULL,
Amount NUMERIC(9,2) NULL
)




-- Make sure case is not an issue
SET @nXMLTransaction = REPLACE(@nXMLTransaction, ' CORWEBID',' CORWebID')

-- Open XML doc
EXEC sp_xml_preparedocument @iDoc OUTPUT, @nXMLTransaction

-- Copy XML data in to temp table
INSERT INTO #GiftIns
SELECT *
FROM OPENXML(@iDoc, '/Gift')
WITH #GiftIns

-- Close XML
EXEC sp_xml_removedocument @iDoc

--Declare Cursor (After temp table has been populated)
SET @cGifts = CURSOR FOR SELECT GiftID,
CORWEBID,
AppealCode,
[Date],
Amount
FROM #GiftIns

-- Open the cursor
OPEN @cGifts

--Scroll around the cursor
FETCH NEXT FROM @cGifts INTO @iGiftID, @iCORWEBID, @nAppealCode, @tDate, @dAmount
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
-- Get PersonID
EXEC @iPersonID = usp_COR_sel_tblCORPerson_IDFromCorWebUrn @iCORWEBID
EXEC @iAppealID = usp_COR_sel_tblCORAppeal_IDFromName @nAppealCode

IF @iAppealID = 0
BEGIN
SET @vMessage = 'Unable to send send process because we cannot locate appeal code: ' + ISNULL(@nAppealCode,'') + ' ' + CONVERT(nvarchar, @iAppealID)
EXEC usp_COR_ins_tblCORError @vUSER, @vPROC, @vTABLE, @vACTION, @vMessage, 10, @iTransactionID
RETURN -1
END
ELSE
BEGIN
EXEC @iReturn = usp_COR_ins_tblCORGiftOnline @iGiftID, @iPersonID, @iAppealID, @tDate, @dAmount
END
END
FETCH NEXT FROM @cGifts INTO @iGiftID, @iCORWEBID, @nAppealCode, @tDate, @dAmount
END

-- Close the cursor
CLOSE @cGifts
DEALLOCATE @cGifts

-- Drop temp table
DROP TABLE #GiftIns

-- Return correct error status (convert from returned identity no)
IF @iReturn > 0
SET @iReturn = 0
ELSE
SET @iReturn = -1

RETURN @iReturn

----------------------------------------------------------------------------------------------------------------------------------------------------------
--DROP PROC usp_COR_ins_tblCORGiftOnline_FromXML










Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-14 : 14:50:06
If you want to capture the return value of SP then you need to use the below method.

--Sample SP
Create procedure TestProc(@ReturnType varchar(10))
AS
Begin
if @ReturnType ='Success'
Return 0
else
Return -1
End


--Calling the SP

DECLARE @return_status int;
EXEC @return_status = TestProc 'Success';
SELECT 'Return Status' = @return_status;
GO

If you want to return the data of any table then you need to use select before return:
e.g., Select * from #GiftIns


Go to Top of Page
   

- Advertisement -