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'.消息119,级别15,状态1,行41必须将参数号5和后续参数传递为“@name = value”。在使用'@name = value'形式后,所有后续参数必须以'@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.
提前致谢。
3 个解决方案
#1
7
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
请在此处详细说明
#2
7
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
0
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;
#1
7
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
请在此处详细说明
#2
7
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
0
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;