當前位置:首頁 » 編程語言 » sql批量更新觸發器
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql批量更新觸發器

發布時間: 2022-04-26 23:02:55

Ⅰ 觸發器批量更新問題

create trigger change_state
on B
after delete
as
update A set state='停用' where exists(select 1 from deleted where deleted.id=A.id)

sql server中的觸發器問題:當批量增刪數據時,inserted和deleted中的數據會多於一行嗎

會,當sql server , update 或delete 多條記錄時,觸發器只觸發一次,
inserted和deleted中的數據是 update 或 delete 的所有記錄

Ⅲ sql批量更新

假定有一個Proct表,欄位有(Id,Name,Quantity,...)我們要一次批量更新Quantity的值
首先在Gridview中,Quantity列以TemplateField顯示,其他的列屬性設為只讀,把顯示格式設為TextBox
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow"
Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
</ItemTemplate>
</asp:TemplateField>
在GridView下面添加一個Button控制項,定義onclick方法為updateButton_Click
最後updateButton_Click代碼為:

protected void updateButton_Click(object sender, EventArgs e)
{
int rowsCount = grid.Rows.Count; GridViewRow gridRow; TextBox quantityTextBox; string proctId; int quantity; bool success = true;
// 遍歷GridView中的每一行
for (int i = 0; i < rowsCount; i++)
{
// 獲行當前行
gridRow = grid.Rows[i];
// 通過DATAKEYS來取行沒顯示出來的ID號
Id = grid.DataKeys[i].Value.ToString();
//
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
// 轉換為整形,如果輸入的是非法字元Int32.TryParse返回FALSE
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// 調用業務層的方法更新數據
success = success && BLL.UpdateItem(Id, quantity);
}
else
{
// 更新失敗
success = false;
}
// 顯示信息
statusLabel.Text = success ?
"
更新成功!
" :
"
更新失敗!
";
}
// 重新綁定GridVIEW
PopulateGridView();
}

Ⅳ sql2005 多行修改update觸發器怎麼寫

根據表A的條件創建觸發器對表B進行相關操作即可,應該比較容易的,既然你已經解決了,就不再獻丑貼代碼了, ^_^.

Ⅳ SQL批量更新

一、臨時表法
1、select item_no, item_subo into TT_diff from ASC001 where item_no <> item_subo
必要時,在TT_diff上建立索引,如create index I_TT_item_no on TT_diff( item_no )
2、針對ASC001以及另外的45個表,各自執行如下命令:
update 表 set t.item_no = x.item_subo from 表 t, TT_diff x where t.item_no = x.item_no
3、drop table TT_diff
二、觸發器法
1、在ASC001上建立如下觸發器
create trigger TR_update_item_no
on ASC001
for update
as
begin
if update( item_no ) then
begin
-- 針對45個表,重復如下框架的命令
update 表
set t.item_no = i.item_no
from 表 t, inserted i, deleted d
where t.item_no = i.item_no
and i.item_no <> d.item_no
end
end
2、執行命令,使觸發器邏輯發生作用
update ASC001 set item_no = item_subo where item_no <> item_subo
3、刪除觸發器
drop trigger TR_update_item_no

說明:若採用第二種方法,當ASC001上已經有觸發器的時候,需要先保存其腳本,幹完這個活兒後再恢復,並且,所有改變發生在一個事務中,要求日誌空間得足夠大。

Ⅵ sql 批量update in

這個建議你去用資料庫觸發器做,不用在程序裡面寫sql語句,直接在資料庫哪裡做就行了
CREATE TRIGGER [觸發器名]
ON [A]
FOR INSERT
AS
UPDATE a SET I_Valid = (SELECT DATEADD(day, 30, UpdateTime ) FROM inserted) WHERE I_ID = (SELECT tel FROM inserted)

CREATE TRIGGER [觸發器名]
ON [A]
FOR UPDATE
AS
UPDATE a SET I_Valid = (SELECT DATEADD(day, 30, UpdateTime ) FROM DELETED)WHERE I_ID = (SELECT tel FROM DELETED)

還有什麼不懂qq 175904944

Ⅶ 關於SQL SERVER觸發器批量更新的問題

y以下是測試數據,發現觸發器是沒有問題,請在資料庫查詢分析器檢查是否有其他觸發器或者規則在執行而影響到結果,如果仍然不明白原因請貼出表結構及數據導出發送給我(qq:157424212,E-mail:[email protected]
create table a
(num int,
id int)

create table b
(nun1 int,
num2 int,
id int)

select * from a

select * from b

insert into a
select 1,1
union
select 2,2
union
select 3,3
union
select 4,4
union
select 5,5

insert into b
select 10,10,1
union
select 10,10,2
union
select 10,10,3
union
select 10,10,4
union
select 10,10,5

create trigger del
on A
for delete
as
begin

update B set num1 = num1 + a.num, num2 = num2 - A.num
from B inner join deleted A on b.id = a.id

end

delete a where id = 1

delete a

select * from b

num1,num2,id
11 9 1
12 8 2
13 7 3
14 6 4
15 5 5

Ⅷ sql server觸發器批量更新只對第一條記錄起作用是怎麼回事

FOREACHROW選項說明觸發器為行觸發器。行觸發器和語句觸發器的區別表現在:行觸發器要求當一個DML語句操作影響資料庫中的多行數據時,對於其中的每個數據行,只要它們符合觸發約束條件,均激活一次觸發器;而語句觸發器將整個語句操作作為觸發事件,當它符合約束條件時,激活一次觸發器。

一般語法:
CREATE[ORREPLACE]TRIGGERtrigger_name
{BEFORE|AFTER}
{INSERT|DELETE|UPDATE[OFcolumn[,column…]]}
[OR{INSERT|DELETE|UPDATE[OFcolumn[,column…]]}...]
ON[schema.]table_name|[schema.]view_name
[REFERENCING{OLD[AS]old|NEW[AS]new|PARENTasparent}]
[FOREACHROW]
[WHENcondition]
PL/SQL_BLOCK|CALLprocere_name;