optional parameters in SQL Server stored proc?

前端 未结 3 814
长发绾君心
长发绾君心 2020-11-30 03:20

I\'m writing some stored procs in SQL Server 2008, and wondered if the concept of optional input parameters is possible here?

I suppose I could always pass in NULL

3条回答
  •  春和景丽
    2020-11-30 04:11

    You can declare like this

    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
    

提交回复
热议问题