获取当前的Sp参数和值 - (反射相似)?

时间:2013-06-19 18:18:38

标签: sql-server sql-server-2008-r2

让我说我有这个简单的SP:

CREATE PROCEDURE mySp
  @a int, 
  @b int
AS
  BEGIN
      ...
      ...
      select ________
  END

现在让我说我通过以下方式执行:

exec mySp 1,2

问题:

我可以在______中放置任何代码,以便它显示给我:

"you sent param named a with value 1"
"you sent param named b with value 2"

此代码应动态(粘贴在某些SP中)。因此,我无法做到:

select "you sent param named a...with value "+cast (@a...)
select "you sent param named b...with value "+cast (@b...)

(必须在运行时读取其上下文参数和值)

有可能吗?

1 个答案:

答案 0 :(得分:2)

我认为不可能干净利落地做到这一点。一种非常脏的方法是传入一个额外的varchar列,该列是其余参数的逗号分隔列表值。然后,您可以获取各个参数的名称,并将其与传入的参数进行匹配。这是一个样本

CREATE PROCEDURE mySp
  @a int, 
  @b INT,
  @c varchar(MAX)
AS
  BEGIN
  declare @names varchar(MAX) = ''
      SELECT @names += name +','
      FROM sys.parameters
      WHERE object_id = @@PROCID AND name <> '@c'
      ORDER BY parameter_id

      SELECT 'The passed in values for '+@names + ' are ' +@c

  END


go
EXEC mySp 1,1, '1,1'