當前位置:首頁 » 編程語言 » sql觸發器的if語句

sql觸發器的if語句

發布時間: 2022-09-20 06:21:12

1. sql 觸發器 if

不知道是 Oracle 還是 SQL Server

如果是 Oracle , 基本上是用 fei07100107 那種的用法

如果是 SQL Server

那麼需要定義幾個變數。

create trigger lev
on customer
for update
as
DECLARE
@NewIntegral INT;
BEGIN

-- 取得 本次 更新的 integral
-- 如果一條語句,更新很多條記錄的,這里要用游標處理。

SELECT @NewIntegral = integral FROM INSERTED

-- 如果這里不是更新全部表的,
-- 那麼麻煩上面再多定義一個 變數,
-- 從 INSERTED 裡面,取得 主鍵, 下面這里加 WHERE 條件。
if @NewIntegral>50 and @NewIntegral<200
begin
update customer
set lev=1
end

else if @NewIntegral>200 and @NewIntegral<500
begin
update customer
set lev=2
end

else @NewIntegral>500
begin
update customer
set lev=3
end
END

2. 在Sql Server trigger中使用if條件句總報錯

看樣子是mssql,我覺得你的as下第一行的if語句不太對啊,orders. orderkey = inserted. orderkey是什麼意思,orders. orderkey應該是一個表的欄位值,不得用select語句來判斷嗎?比如
If exists( select * from orders where orderkey = inserted. orderkey)
.....

3. SQL Server觸發器問題,語句有問題

你以前一直用Oracle資料庫吧,SQL Server的觸發器和Oracle的差別還是比較大的,MSsql中沒有:new.…或:old.…的用法,inserted指的是更新或插入的數據,deleted指的是被修改或刪除的數據。MSsql中的IF語句不是「IF THEN END IF」而是「IF begin end」的模式。並且各句末尾不需要加分號。
CREATE TRIGGER Insert_SC ON SC
FOR INSERT
declare @oldcno 類型,@newcno 類型,@newsnum 類型,@oldsnum 類型;
AS
BEGIN
select @oldcno=Cno from deleted
select @newcno=Cno from inserted
select @oldsnum=Snumber from deleted
IF (@oldcno= @newcno)
begin
@newsnum= @oldsnum+ 1;
update SC set Snumber =@newsnum where 唯一標識你正在操作的行;
end
END;
以上寫的這個並不能直接供你使用,你還需要琢磨下修改後才能使用。

4. SQL IF 語句

來晚了~
關於判斷語句看資料庫吧
SQL server 支持的查詢語句命令case when:
結構如下:
case
when 條件1 then 結果1
when 條件2 then 結果2
……
end
在access中,不支持case when 結構,使用 iif 代替:
結構如下:
IIF(條件,結果1,結果2)
解釋:當條件成立,取結果1;當條件不成立,取結果2
IIF(TypeID=-1,'一般客人',(select ClubType.Name from ClubType where CustomerInfo.TypeID =ClubType.ID)) as TypeName

5. SQL 觸發器語句

create trigger checkRoysched
on roysched
INSTEAD OF INSERT --改成這個,這是在Insert之前的觸發。
as
if ((select title_id from roysched) is null) or
((select royalty from roysched) is null)
begin
print 'royalty,titlt_id欄位不能為空'
rollback transaction
end
go

6. mssql觸發器 if

ALTERTRIGGER[dbo].[ICMO_update2]
ON[dbo].[ICMO]
afterupdate
AS
begin
declare@FCheckerID_pint,@FCheckerIDint
select@FCheckerID_p=t3.FCheckerID,@FCheckerID=t3.FCheckerID
fromICMOt1,insertedt2,ICMOt3
wheret1.FInterID=t2.FInterID
andt3.FParentInterID=t1.FInterID

if(@FCheckerID_pisnull)
if(@FCheckerIDisnotnull)
raiserror("請先處理子任務",18,1)withNOWAIT
else
updatet1setFCheckerID=1fromICMOt1,insertedt2
wheret1.FInterID=t2.FInterID
end

7. sqlserver觸發器添加條件if語句後不起作用,求解

select @t1=Not_Withdraw_Total from tbUserInfo

看看你這個@t1的具體值,應該是最後一條記錄的值
這個值是否大於等於插入記錄的Price*Rest_Num

8. mssql怎樣在觸發器中寫if語句

if exists (select * 表2 where name=(select sl from inserted))
update 表2 set name=(select sl from inserted) where name=(select sl from inserted)
else
insert into ...

9. 關於sql觸發器中的if和else

你用的是sql2000嗎?
在sql2000里if 和else 之間必須有語句,沒有語句是錯誤語法,你嵌套了很多層是不是有的裡面沒有寫東西啊
例如

10. SQL觸發器語句

呵呵,看到你的這個問題了,回答一下,希望能給你增加印象。

由於SqlServer 沒有oracle中的行級觸發器的概念,觸發器如下:

create trigger [TC2]
on [dbo].[teacher]
for insert,update
as if (select salary from inserted)<3000
update teacher set salary=3000 and tid= (select tid from inserted)

說明:當你插入數據的時候,這條數據是存放在【inserted】表中的,在這個表中把【teacher】表的主鍵得到(假如是【tid】)然後把這個主鍵信息加到where 條件上,這樣就能起到只更新插入的那一條數據的效果了,否則會出現更新了全表的問題。

---
以上,希望對你有所幫助。

熱點內容
android文件夾重命名 發布:2025-05-15 01:13:50 瀏覽:481
cns腳本 發布:2025-05-15 01:13:38 瀏覽:722
數據結構與演算法筆試題 發布:2025-05-15 01:04:20 瀏覽:417
搜狗輸入法如何直接編輯配置文件 發布:2025-05-15 00:51:47 瀏覽:668
電箱都有哪些配置 發布:2025-05-15 00:30:21 瀏覽:74
安卓qq邀請碼在哪裡尋找 發布:2025-05-15 00:02:04 瀏覽:35
三菱fx編程口 發布:2025-05-15 00:01:23 瀏覽:810
醫院招商引資宣傳片腳本 發布:2025-05-15 00:01:21 瀏覽:368
linuxcftp伺服器 發布:2025-05-14 23:58:18 瀏覽:718
探岳什麼配置才有駕駛模式選擇 發布:2025-05-14 23:53:17 瀏覽:146