一是多写几个更新语句,一次更新一个字段
二是把更新语句,拼接成string形式,最后一次执行.
或用 @@rowcount判断是否存在:
create procedure alStuentsInfor
@StuId char(10),
@StuImage IMAGE=null,
@StuName varchar(20)=null,
@StuSex char(5)=null, @StuAge smallint=null
AS
BEGIN
UPDATE dbo.Students
SET iStuImage = ISNULL(@StuImage,iStuImage),
vStuName = ISNULL(@StuName,vStuName),
cStuSex = ISNULL(@StuSex,cStuSex),
sStuAge = ISNULL(@StuAge,sStuAge)
WHERE cStudentsId = @StuId
IF @@ROWCOUNT>0
RETURN 0
ELSE
begin
PRINT'NO RECORDS FOUND!'
RETURN 1
END
END
GO
IF--THEN语句结合
复制代码 代码如下:
UPDATE mytable
SET myfield = CASE other_field
WHEN 1 THEN 'value'
WHEN 2 THEN 'value'
WHEN 3 THEN 'value'
END
WHERE id IN (1,2,3)
我们可以使用以下SQL语句:
复制代码 代码如下:
UPDATE categories
SET display_order = CASE id
WHEN 1 THEN 3
WHEN 2 THEN 4
WHEN 3 THEN 5
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)