如何检查空值或空表值参数?


9

我有一个存储过程(SS2k8),带有几个表值参数,有时会为null或为空。我看过这个StackOverflow帖子,其中指出应该从调用参数列表中省略空/空TVP。我的问题是我无法弄清楚如何在存储过程中检查空或空,因为“ IF(@tvp IS NULL)”在创建过程时失败,并显示消息“必须声明标量变量” @tvp”。我是否必须在TVP上执行SELECT COUNT(*)并检查零?

代码摘录:

CREATE PROCEDURE [foo] (@tvp [TvpType] READONLY) AS

IF (@tvp IS NOT NULL) -- doesn't work
BEGIN
  -- lots of expensive processing
END
ELSE
BEGIN
  -- a little bit of cheap processing
END
...

Answers:


14

表格不能是NULL,TVP也不能。您如何检查表格是否为空?你当然不会说IF Sales.SalesOrderHeader IS NULL。:-)

IF EXISTS (SELECT 1 FROM @tvp)
BEGIN
  -- lots of expensive processing
END
ELSE
BEGIN
  -- a little bit of cheap processing
END
...

3

表值参数不会为空。像桌子一样对待它,@ aaraon Bertrand击败了我。是的,检查是否有行。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.