T-SQL 中的 CALL 和 EXEC 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7846418/
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 12:33:38  来源:igfitidea点击:

What is the difference between CALL and EXEC in T-SQL?

sqlsql-servertsqlsql-server-2008

提问by Billy ONeal

Consider:

考虑:

CREATE PROCEDURE LowerCityDiscounts @city VARCHAR(45), @decrease DECIMAL(10,2) AS
BEGIN
    BEGIN TRANSACTION;
    UPDATE Customers SET discnt = discnt - @decrease
    WHERE Customers.city = @city;

    UPDATE Customers SET discnt = 0
    WHERE Customers.city = @city AND discnt < 0
    COMMIT;
END;

I tried to call this procedure with:

我尝试使用以下方法调用此过程:

CALL LowerCityDiscounts 'Cleveland', 5;

but this only produces

但这只会产生

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Cleveland'.

Yet, if I change things to

然而,如果我把事情改成

EXEC LowerCityDiscounts 'Cleveland', 5;

everything works fine. This despite that the documentationstating that callis the right syntax.

一切正常。尽管文档说明这call是正确的语法。

Why does EXECwork when CALLdoes not?

为什么EXEC工作时CALL不工作?

采纳答案by Kieren Johnstone

Yup.. CALLis an construct/syntax usable from an ODBC driver, as your documentation indicates.

CALL是的..是可从 ODBC 驱动程序使用的构造/语法,如您的文档所示。

There's no reference in the T-SQL documentation to CALL, only EXEC.

T-SQL 文档中没有提到CALL,只有EXEC

It doesn't work because it's not T-SQL.

它不起作用,因为它不是 T-SQL。

回答by Christian Hayter

The T-SQL language does not recognise ODBC escape sequences; EXECis the only command available for calling a stored procedure. ODBC escape sequences are interpreted by client-side libraries (e.g. ODBC, OLE DB, ADO, ADO.NET) and translated to real T-SQL syntax on the fly before execution.

T-SQL 语言无法识别 ODBC 转义序列;EXEC是唯一可用于调用存储过程的命令。ODBC 转义序列由客户端库(例如 ODBC、OLE DB、ADO、ADO.NET)解释并在执行前即时转换为真正的 T-SQL 语法。

The end result is, you can call your top-level stored procedure from the client using CALLif you want to, but if that procedure calls others, it must use EXEC.

最终结果是,您可以根据需要从客户端调用顶级存储过程CALL,但如果该过程调用其他过程,则必须使用EXEC.

The same principle applies for the date/time literal escape sequences.

相同的原则适用于日期/时间文字转义序列。

回答by Kna?is

I ran across an issue (while migrating databases) that MSSQL will accept CALLstatement in a stored procedure - the SQL Management Studio complains but the query itself is executed successfully.

我遇到了一个问题(在迁移数据库时),MSSQL 将接受CALL存储过程中的语句 - SQL Management Studio 抱怨但查询本身已成功执行。

So a statement like this does execute:

所以像这样的语句确实会执行:

create procedure spwho
as begin
    call sp_who2
end
go

exec spwho

Unfortunately even though the procedure is created, it does not produce any results (but neither does it produce any errors or warnings).

不幸的是,即使创建了该过程,它也不会产生任何结果(但也不会产生任何错误或警告)。

So in cases like this the CALLstatement will not produce errors in MSSQL but anyway should never used since it does not work.

所以在这种情况下,该CALL语句不会在 MSSQL 中产生错误,但无论如何都不应该使用,因为它不起作用