使用不同的缺少参数调用执行存储过程

时间:2015-06-21 22:51:18

标签: sql sql-server stored-procedures

如果我有这样的存储过程:

CREATE Procedure [dbo].[insertIntoTableX]
    -- table_name as there are three tables with the same schema...
    @table_name varchar(200),
    -- all the other variables for the columns
    @column_1 = NULL,
    @column_2 = NULL,
    @column_3 = NULL,
    @column_4 = NULL,
    @column_5 = NULL
AS
BEGIN TRY
  SET NOCOUNT ON

  INSERT INTO @table_name (column_1, column_2, column_3, column_4, column_5)
  VALUES (@column_1, @column_2, @column_3, @column_4, @column_5)
END TRY
BEGIN CATCH
  -- ....
END CATCH

如果我使用Execute insertIntoTableX (1,2,3,4)调用它,结果将是[1234NULL],因为第五个被存储过程中的默认值设置为null。

如何使用缺少的参数调用它?例如。如果希望结果为[NULLNULL3NULL5]?

1 个答案:

答案 0 :(得分:3)

您可以使用命名参数调用存储过程。例如:

exec [dbo].[insertIntoTableX] @column_3 = 3, @column_5 = 5;