SQL 如何从另一个存储过程调用一个存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1184325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:58:28  来源:igfitidea点击:

How to Call a stored procedure from another stored procedure?

sqlsql-server-2005stored-procedures

提问by Amr Elgarhy

I have an insert stored procedure which takes many parameters - 2 of them are @FirstName, @LastName. I also have an update stored procedure which takes many parameters - 2 of them are @FirstName, @LastName.

我有一个带有许多参数的插入存储过程 - 其中两个是@FirstName、@LastName。我还有一个更新存储过程,它采用许多参数 - 其中 2 个是@FirstName、@LastName。

What I want to do is, from inside the insert SP, when it's done, call the update SP and send to it the @FirstName, @LastName.

我想要做的是,从插入 SP 内部,完成后,调用更新 SP 并将@FirstName、@LastName 发送给它。

I don't know the right syntax to do that; I tried:

我不知道这样做的正确语法;我试过:

exec  LandData_Update @FirstName, @LastName

But I think it's wrong.

但我认为这是错误的。

Can someone tell me how to write this calling?

谁能告诉我这个电话怎么写?

And if I will call the update sp with different param names? Such as @MyFirstName, @MyLastName? Would I write it like this: EXECUTE LandData_Update @MyFirstName=@FirstName, @MyLastName=@LastName?

如果我将使用不同的参数名称调用更新 sp?比如@MyFirstName、@MyLastName?我会这样写:EXECUTE LandData_Update @MyFirstName=@FirstName, @MyLastName=@LastName?

回答by Jesper Fyhr Knudsen

What makes you think it's wrong?

是什么让你认为这是错误的?

CREATE PROCEDURE MyInsertSP
    @FirstName varchar(255),
    @LastName  varchar(255)
AS
BEGIN
    INSERT INTO Table VALUES('Some Value')

    EXECUTE LandData_Update @FirstName, @LastName
END

Do you get an error or something?

你得到一个错误或什么?

EDIT:It doesn't matter what the name of the variables are, but to do what you want you can declare two new variables.

编辑:变量的名称无关紧要,但是要执行您想要的操作,您可以声明两个新变量。

DECLARE @MyFirstName varchar(255)
DECLARE @MyLastName  varchar(255)

SET @MyFirstName = @FirstName
SET @MyLastName  = @LastName

And then use the new variables. But again, the Store Procedure doesn't care what the variables are called.

然后使用新变量。但同样,存储过程并不关心变量的名称。