parámetros opcionales en SQL Server almacenado proc?


Estoy escribiendo algunos procs almacenados en SQL Server 2008, y me preguntaba si el concepto de parámetros de entrada opcionales es posible aquí?

Supongo que siempre podría pasar NULL para los parámetros que no quiero usar, verificar el valor en el proc almacenado, luego tomar las cosas de allí, pero me interesaba si el concepto está disponible aquí. ¡Gracias!

Author: FMFF, 2009-11-28

2 answers

Puedes declarar así

CREATE PROCEDURE MyProcName
    @Parameter1 INT = 1,
    @Parameter2 VARCHAR (100) = 'StringValue',
    @Parameter3 VARCHAR (100) = NULL
AS

/* check for the NULL / default value (indicating nothing was passed */
if (@Parameter3 IS NULL)
BEGIN
    /* whatever code you desire for a missing parameter*/
    INSERT INTO ........
END

/* and use it in the query as so*/
SELECT *
FROM Table
WHERE Column = @Parameter
 154
Author: Raj More,
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
2009-11-27 21:19:07

Sí, lo es. Declare el parámetro así:

@Sort varchar(50) = NULL

Ahora ni siquiera tiene que pasar el parámetro. El valor predeterminado será NULL (o lo que elijas por defecto).

 42
Author: Mike Cole,
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
2009-11-27 21:11:20