Execute stored procedure with an Output parameter?

2020-01-24 10:49发布

I have a stored procedure that I am trying to test. I am trying to test it through SQL Management Studio. In order to run this test I enter ...

exec my_stored_procedure 'param1Value', 'param2Value'

The final parameter is an output parameter. However, I do not know how to test a stored procedure with output parameters.

How do I run a stored procedure with an output parameter?

14条回答
对你真心纯属浪费
2楼-- · 2020-01-24 11:17

I'm using output parameter in SQL Proc and later I used this values in resultset.

enter image description here

查看更多
混吃等死
3楼-- · 2020-01-24 11:21

Procedure Example :

Create Procedure [dbo].[test]
@Name varchar(100),
@ID int Output   
As  
Begin   
SELECT @ID = UserID from tbl_UserMaster where  Name = @Name   
Return;
END     

How to call this procedure

Declare @ID int    
EXECUTE [dbo].[test] 'Abhishek',@ID OUTPUT   
PRINT @ID
查看更多
Anthone
4楼-- · 2020-01-24 11:26

The easy way is to right-click on the procedure in Sql Server Management Studio(SSMS),

select execute stored procedure...

and add values for the input parameters as prompted.

SSMS will then generate the code to run the proc in a new query window, and execute it for you. You can study the generated code to see how it is done.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-01-24 11:28

Check this, Where first two parameters are input parameters and 3rd one is Output parameter in Procedure definition.

DECLARE @PK_Code INT;
EXEC USP_Validate_Login  'ID', 'PWD', @PK_Code OUTPUT
SELECT @PK_Code
查看更多
时光不老,我们不散
6楼-- · 2020-01-24 11:28
CREATE PROCEDURE DBO.MY_STORED_PROCEDURE
(@PARAM1VALUE INT,
@PARAM2VALUE INT,
@OUTPARAM VARCHAR(20) OUT)
AS 
BEGIN
SELECT * FROM DBO.PARAMTABLENAME WHERE PARAM1VALUE=@PARAM1VALUE
END

DECLARE @OUTPARAM2 VARCHAR(20)
EXEC DBO.MY_STORED_PROCEDURE 1,@OUTPARAM2 OUT
PRINT @OUTPARAM2
查看更多
▲ chillily
7楼-- · 2020-01-24 11:30

Return val from procedure

ALTER PROCEDURE testme @input  VARCHAR(10),
                       @output VARCHAR(20) output
AS
  BEGIN
      IF @input >= '1'
        BEGIN
            SET @output = 'i am back';

            RETURN;
        END
  END

DECLARE @get VARCHAR(20);

EXEC testme
  '1',
  @get output

SELECT @get 
查看更多
登录 后发表回答