开发者

Using function as a parameter when executing a stored procedure? [duplicate]

开发者 https://www.devze.com 2023-03-05 16:36 出处:网络
This question already has answers here: 开发者_运维知识库 Incorrect syntax near ')' calling stored procedure with GETDATE
This question already has answers here: 开发者_运维知识库 Incorrect syntax near ')' calling stored procedure with GETDATE (3 answers) Closed 5 years ago.

I'm testing a stored procedure and wanted to submit 'GETDATE()' function in place of parameter:

DECLARE @return_value int

EXEC @return_value = my_stored_procedure
        @MyId = 1,
        @MyDateField = GETDATE()

SELECT  'Return Value' = @return_value
GO

SQL Server 2005 complains with following error:

Incorrect syntax near ')'.

Anybody care to shed some light on the matter?


You can't use a function directly as a stored procedure parameter.

You can do the following:

DECLARE @now DateTime
SET @now = GETDATE()

DECLARE @return_value int
EXEC @return_value = my_stored_procedure
        @MyId = 1,
        @MyDateField = @now
SELECT  'Return Value' = @return_value
GO


per MSDN

Execute a stored procedure or function
[ { EXEC | EXECUTE } ]
    { 
      [ @return_status = ]
      { module_name [ ;number ] | @module_name_var } 
        [ [ @parameter = ] { value 
                           | @variable [ OUTPUT ] 
                           | [ DEFAULT ] 
                           }
        ]
      [ ,...n ]
      [ WITH RECOMPILE ]
    }
[;]

    Execute a character string
    { EXEC | EXECUTE } 
        ( { @string_variable | [ N ]'tsql_string' } [ + ...n ] )
        [ AS { LOGIN | USER } = ' name ' ]
    [;]

    Execute a pass-through command against a linked server
    { EXEC | EXECUTE }
        ( { @string_variable | [ N ] 'command_string [ ? ]' } [ + ...n ]
            [ { , { value | @variable [ OUTPUT ] } } [ ...n ] ]
        ) 
        [ AS { LOGIN | USER } = ' name ' ]
        [ AT linked_server_name ]
    [;]

Notice for @parameter you can either specify a value or a variable or specify Default. So you got to set the value of a variable as GetDate() (as others have specified) and use that variable.

HTH


Function calls as parameters are not allowed (except for those system functions prefixed @@ - i.e. those that used to be called global variables)

You need to assign to a variable.

Microsoft acknowledge this is less than great in this related Connect item: T-SQL: use scalar functions as stored procedure parameters

Agreed! More generally, wherever TSQL expects, say, and integer value, it should accept a literal, a variable, or the result of a function whose return type is integer. It just makes the language more regular ("orthogonal") and easier to learn/use.

That said, it's too late for this feature in the Katmai release, but I'll add it to our TODO list.


You could use

DECLARE @test DATE;
SET @test = GETDATE();

and then

DECLARE @return_value int
EXEC @return_value = my_store procedure
        @MyId = 1,
        @MyDateField = @test
SELECT  'Return Value' = @return_value
GO


Try:

DECLARE @return_value int
EXEC @return_value = my_store procedure
        @MyId = 1,
        @MyDateField = (SELECT GETDATE())
SELECT  'Return Value' = @return_value
GO
0

精彩评论

暂无评论...
验证码 换一张
取 消