在SQL Server中,我该如何验证查询是否返回了NULL,并根据此运行代码块。例如,在查询1中,我想检查如果count(*)不为null
,然后检查它是否大于0。我应该在这里使用if exists
吗?
if select count(*) from tbl1 not is NULL then
if select count(*) from tbl1 where count(*)>0 then
raiserror()
end if
end if
在Oracle中,可以使用IF INSERTING THEN
、IF updating THEN
或if deleting then
根据列运行某个代码块。那么在SQL Server中该如何实现呢?请参考以下Oracle代码。
CREATE OR REPLACE TRIGGER tr_name
BEFORE DELETE OR INSERT OR UPDATE OF column1 ON tbl1
FOR EACH ROW
WHEN (NEW.column1 IS NOT NULL)
begin
IF INSERTING THEN
run some code like check if there are more than row in a table and if >0 then not allow any inserts
IF updating THEN
run some code
IF deleting THEN
run some code
end
COUNT
永远不会返回NULL
。 - Robby CornelissenISNULL
的说明,因为我认为您在评论中使用ISNULL(...) > 0
的例子并不是它的正确用法。 - Matthew Haugen