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

Execute a Stored Procedure with different missing parameter calls

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

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。

如何在缺少参数的情况下调用它?例如。如果希望结果为 [NULL,NULL,3,NULL,5]?

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

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