Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 documentation stating that call is the right syntax.

Why does EXEC work when CALL does not?

like image 200
Billy ONeal Avatar asked Oct 21 '11 07:10

Billy ONeal


People also ask

What does EXEC mean in SQL?

The EXEC command is used to execute a stored procedure.

What is SQL call?

Purpose. Use the CALL statement to execute a routine (a standalone procedure or function, or a procedure or function defined within a type or package) from within SQL. Note: The restrictions on user-defined function expressions specified in "Function Expressions" apply to the CALL statement as well.

What is the significance of execute and EXEC?

What is the difference between EXEC and EXECUTE? I have seen enough times developer getting confused between EXEC and EXEC(). EXEC command executes stored procedure where as EXEC() function takes dynamic string as input and executes them.

Can we use EXEC in SQL function?

Exec is not allowed in functions, but it is allowed in stored procedures, so you can just rewrite the function as a stored procedure which retuns a resultset.


2 Answers

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

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

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

like image 146
Kieren Johnstone Avatar answered Sep 23 '22 06:09

Kieren Johnstone


The T-SQL language does not recognise ODBC escape sequences; EXEC is 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.

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

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

like image 36
Christian Hayter Avatar answered Sep 25 '22 06:09

Christian Hayter