SQL Server stored procedure parameters

前端 未结 5 1552
[愿得一人]
[愿得一人] 2020-12-08 07:09

I am developing a framework, where in I am a calling stored procedure with dynamically created parameters. I am building parameter collection at the runtime.

The pr

5条回答
  •  难免孤独
    2020-12-08 07:41

    SQL Server doesn't allow you to pass parameters to a procedure that you haven't defined. I think the closest you can get to this sort of design is to use optional parameters like so:

    CREATE PROCEDURE GetTaskEvents
        @TaskName varchar(50),
        @ID int = NULL
    AS
    BEGIN
    -- SP Logic
    END;
    

    You would need to include every possible parameter that you might use in the definition. Then you'd be free to call the procedure either way:

    EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2;
    EXEC GetTaskEvents @TaskName = 'TESTTASK'; -- @ID gets NULL here
    

提交回复
热议问题