62,242
社区成员




if object_id('tb') is not null
drop table tb
create table tb(id int,subject varchar(2),shu int,liang int,一 int,二 int,三 int)
insert tb
select 1,'一',100,200,null,null,null union all
select 2,'二',333,111,null,null,null union all
select 3,'三',44,55,null,null,null
declare @id int,@name nvarchar(50), @Sql1 nvarchar(500)
declare newCursor Cursor for select [id],subject from tb
open newCursor
fetch next from newCursor into @id,@name
while @@fetch_status = 0
begin
set @Sql1 = 'update tb set '+@name+' =shu+liang where id ='+convert(varchar(10), @id)
exec sp_executesql @Sql1
fetch next from newCursor into @id,@name
end
close newCursor
deallocate newCursor
if object_id('tb') is not null
drop table tb
create table tb(id int,subject nvarchar(2),shu int,liang int)
insert tb
select 1,N'一',100,200 union all
select 2,N'二',333,111 union all
select 3,N'三',44,55
select id,subject,shu,liang,(case when subject = '一' then shu+liang end ) '一',
(case when subject ='二' then shu+liang end) '二',
(case when subject = '三' then shu+liang end) '三'
from tb
if object_id('tb') is not null
drop table tb
create table tb(id int,subject varchar(2),shu int,liang int)
insert tb
select 1,'一',100,200 union all
select 2,'二',333,111 union all
select 3,'三',44,55
select *,(case when subject = '一' then shu+liang end ) '一',
(case when subject ='二' then shu+liang end) '二',
(case when subject = '三' then shu+liang end) '三'
from tb