How to pass output parameter to a Stored Procedure?

半城伤御伤魂 提交于 2019-12-20 12:46:09

问题


I have written a stored procedure with the following format:

ALTER PROCEDURE usp_data_migration 
   (@sourceDatabase varchar(50),
    @sourceTable varchar(50),
    @targetDatabase varchar(50),
    @targetTable varchar(50),
    @finaloutput varchar(max) output)
AS
BEGIN
----Set of SQL Blocks


END

Then, I am executing the procedure:

DECLARE @finaloutput1 varchar(300)

EXEC usp_data_migration 'Yousuf', 'emp', '[City Branch]', 'emp_tgt', @finaloutput1 output 

SELECT @finaloutput1 

By executing this way I don't proper output.

When I execute this way:

DECLARE @finaloutput1 varchar(300)

EXEC usp_data_migration @sourceDatabase = 'Yousuf',
                        @sourceTable = 'emp',
                        @targetDatabase = '[City Branch]',
                        @targetTable = 'emp_tgt',
                        @finaloutput1 output 

SELECT @finaloutput1 

I get an error message saying:

Msg 119, Level 15, State 1, Line 41
Must pass parameter number 5 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'.

And if I removed my output parameter and execute the procedure, I get my desired output but I am not able to get my result as an output.

EXEC usp_data_migration @sourceDatabase = 'Yousuf',
                        @sourceTable = 'emp',
                        @targetDatabase = '[City Branch]',
                        @targetTable = 'emp_tgt'

What should I do?

Thanks in advance.


回答1:


You have to Select like this

Example 1

  create procedure p1
    (
    @id INT,
    @name varchar(20) OUTPUT,
    @company varchar(20) OUTPUT
    )
    AS
     BEGIN
    Set @name = 'name'
    Set @company = 'company'
        select @name , @company from table1 where id = @id;
     END
    GO

Example 2

CREATE PROCEDURE Myproc
    @parm varchar(10),
    @parm1OUT varchar(30) OUTPUT,
    @parm2OUT varchar(30) OUTPUT
    AS
      SELECT @parm1OUT='parm 1' + @parm
     SELECT @parm2OUT='parm 2' + @parm
GO
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
DECLARE @parmIN VARCHAR(10)
DECLARE @parmRET1 VARCHAR(30)
DECLARE @parmRET2 VARCHAR(30)
SET @parmIN=' returned'
SET @SQLString=N'EXEC Myproc @parm,
                             @parm1OUT OUTPUT, @parm2OUT OUTPUT'
SET @ParmDefinition=N'@parm varchar(10),
                      @parm1OUT varchar(30) OUTPUT,
                      @parm2OUT varchar(30) OUTPUT'

EXECUTE sp_executesql
    @SQLString,
    @ParmDefinition,
    @parm=@parmIN,
    @parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT

SELECT @parmRET1 AS "parameter 1", @parmRET2 AS "parameter 2"
go
drop procedure Myproc

Please refer more here




回答2:


The error message is self-explanatory - you should name all of your parameters.

DECLARE @finaloutput1 varchar(300);

EXEC dbo.usp_data_migration -- always use schema prefix
  @sourceDatabase = 'Yousuf',
  @sourceTable = 'emp',
  @targetDatabase = '[City Branch]',
  @targetTable = 'emp_tgt',
  @finaloutput = @finaloutput1 OUTPUT;

SELECT @finaloutput1;



回答3:


Simple Example:

create procedure proc2 @var int out,@var2 varchar(10) out  
as  
begin  
set @var=(select max(id) from customer);  
set @var2=(select name from customer where id=@var);  
end

declare @maxid int;  
declare @maxname varchar(10);  
exec proc2 @maxid out,@maxname out;  
select @maxid,@maxname;


来源:https://stackoverflow.com/questions/21058541/how-to-pass-output-parameter-to-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!