Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 736
krul Avatar asked May 11 '11 15:05

krul


People also ask

Can we pass function as a parameter in stored procedure?

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

Can we use function in stored procedure?

You cannot execute a stored procedure inside a function, because a function is not allowed to modify database state, and stored procedures are allowed to modify database state.

How can we execute stored procedures and functions?

To execute a stored procedure or function, you only need to include its object name. Procedures and functions that are created outside of a package are called stored or standalone subprograms. Procedures and functions defined within a package are known as packaged subprograms.

How do I execute a stored procedure with parameters in SQL Server?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.


2 Answers

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 
like image 188
Oded Avatar answered Oct 07 '22 18:10

Oded


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

like image 35
Raja Avatar answered Oct 07 '22 20:10

Raja