Ⅰ 触发器批量更新问题
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;