在 SQL Server 中,如何验证查询是否返回 NULL 并根据它运行块。例如在查询 1 中,我想检查if count(*) is not null
然后检查它是否 >0 。我应该使用if exists
here ?
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
or IF updating THEN
or 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
DECLARE @ErrorMsg nvarchar(400)
IF (SELECT count(*) FROM tbl1) = 0
BEGIN
SET @ErrorMsg = 'You are returning nothing'
SELECT @ErrorMsg Err
RETURN
END
Else IF (SELECT count(*) FROM tbl1) >= 1
BEGIN
SET @ErrorMsg = 'You are returning something'
SELECT @ErrorMsg Err
RETURN
END
您无法从计数中得到 null,因此如果您检查 0,则实际上是等价的。
else if 检查计数返回的任何内容
你也可以使用IF EXISTS
IF EXISTS (
SELECT 1 FROM tbl1
)
BEGIN
SET @ErrorMsg = 'You are returning something'
SELECT @ErrorMsg Err
RETURN
END
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)