¿Usar la función como parámetro al ejecutar un procedimiento almacenado? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

Estoy probando un procedimiento almacenado y quería enviar la función 'GETDATE ()' en lugar del parámetro:

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

SQL Server 2005 se queja con el siguiente error:

Sintaxis incorrecta cerca ')'.

Alguien que pueda arrojar algo de luz sobre el asunto?

Author: Luke Girvin, 2011-05-11

6 answers

Por 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 ]
    [;]

Aviso para @parámetro puede especificar un valor o una variable o especificar por defecto. Así que tienes que establecer el valor de una variable como GetDate () (como otros han especificado) y usar esa variable.

HTH

 12
Author: Raja,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-11 15:58:20

No puede usar una función directamente como un parámetro de procedimiento almacenado.

Puedes hacer lo siguiente:

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
 26
Author: Oded,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-05 19:56:34

No se permiten llamadas a funciones como parámetros (excepto para aquellas funciones del sistema con el prefijo @@ - es decir, aquellas que solían llamarse variables globales)

Necesita asignar a una variable.

Microsoft reconoce que esto no es muy bueno en este elemento de conexión relacionado: T-SQL: use funciones escalares como parámetros de procedimiento almacenados

Acordado! Más generalmente, donde TSQL espera, por ejemplo, y el valor entero, que debe aceptar un literal, una variable, o el resultado de una función cuya el tipo de retorno es entero. Sólo hace el idioma más regular ("ortogonal") y más fácil de aprender/usar.

Dicho esto, es demasiado tarde para esto característica en la versión Katmai, pero Lo añadiré a nuestra lista de tareas PENDIENTES.

 9
Author: Martin Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-11 15:57:19

Podrías usar

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

Y luego

DECLARE @return_value int
EXEC @return_value = my_store procedure
        @MyId = 1,
        @MyDateField = @test
SELECT  'Return Value' = @return_value
GO
 1
Author: user748261,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-11 15:53:50

Intenta:

DECLARE @return_value int
EXEC @return_value = my_store procedure
        @MyId = 1,
        @MyDateField = (SELECT GETDATE())
SELECT  'Return Value' = @return_value
GO
 0
Author: garnertb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-11 15:50:45

¿Por qué necesita pasar GetDate ()? Simplemente úselo en el procedimiento llamado Almacenado.

No Se puede pasar directamente. Simplemente asígnelo a una variable y pase eso

DECLARE @dt as datetime
SET @dt=GETDATE()

EXEC @return_value = my_store procedure
        @MyId = 1,
        @MyDateField = dt
 -1
Author: Phil Murray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-26 18:28:36