批量更新sql |批量update sql
SELECT g.GoodsID,g.StockNum,a.GoodsID,a.Amount FROM Goods g INNER JOIN
(SELECT GoodsID,SUM(Amount) AS Amount FROM OrderGoods WHERE OrderID IN(
SELECT OrderID FROM [Order] WHERE OrderState = 1 AND OrderTime <='2016-01-27 15:56:39.643'
) GROUP BY GoodsID) a ON g.GoodsID = a.GoodsID
UPDATE Goods SET StockNum = StockNum+a.Amount FROM Goods g INNER JOIN
(SELECT GoodsID,SUM(Amount) AS Amount FROM OrderGoods WHERE OrderID IN(
SELECT OrderID FROM [Order] WHERE OrderState = 1 AND OrderTime <='2016-01-27 15:56:39.643'
) GROUP BY GoodsID) a ON g.GoodsID = a.GoodsID
级联删除
SQL SERVER级联删除:在删除主表时,自动删除副表(外键约束)相应内容
SQL SERVER级联删除包含主键值的行的操作,该值由其它表的现有行中的外键列引用。在级联删除中,还删除其外键值引用删除的主键值的所有行。
如:
create database temp
go
use temp
go
create table UserInfo
(
UserId int identity(1,1) primary key ,
UserName varchar(20), --用户名
password varchar(20) not null --密码
)
create table UserDetails
(
id int identity(1,1) primary key,
name varchar(50) not null, --真实姓名
userId int,
foreign key (userId) references UserInfo(UserId) on delete cascade
)
insert UserInfo values ('ly','jeff')
insert UserInfo values('wzq','wzqwzq')
insert UserInfo values('lg','lglg')
insert UserDetails values('李四',1)
insert UserDetails values('王五',2)
insert UserDetails values('刘六',3)
此时:Delete From UserInfo Where UserId = 1 就可删除UserInfo表和UserDetails表的UserId=1 的内容