sql 问高手

symbol_bc 2009-09-17 08:03:44
数据库
id subject shu liang 一 二 三
1 一 100 200
2 二 333 111
3 三 44 55

求把每行的subject的对应列的值用shu 和liang的和填充
结果应该如下

id subject shu liang 一 二 三
1 一 100 200 300
2 二 333 111 444
3 三 44 55 99


这是我的方法,我觉得逻辑没问题,但是有一个错误

消息 245,级别 16,状态 1,第 10 行
在将 nvarchar 值 'update sales set 一= shu + liang where id=' 转换成数据类型 int 时失败。

请大家帮我,如果有其他方法也行

declare @id int,@name nvarchar(50), @Sql1 nvarchar(500)
declare newCursor Cursor for select id,subject from sales

open newCursor
fetch next from newCursor into @id,@name

while @@fetch_status = 0
begin
set @Sql1 = 'update sales set ' + @name + '= shu + liang where id=' + @id
exec sp_executesql @Sql1
fetch next from newCursor into @id,@name
end

close newCursor
deallocate newCursor
...全文
135 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
szldk 2009-09-17
  • 打赏
  • 举报
回复
路过的
kingcsx666 2009-09-17
  • 打赏
  • 举报
回复
kingcsx666 2009-09-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 symbol_bc 的回复:]
而且这是一个实验的例子,其实subject 项会有很多,难道case... when ...一直这样下去吗
[/Quote]

可以用动态语句+Pivot(交叉表)

我一直在用,可以看看Pivot
  • 打赏
  • 举报
回复
@sql1为nvarchar型,但是@id是int型,所以会出现问题。。
  • 打赏
  • 举报
回复

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
wuyq11 2009-09-17
  • 打赏
  • 举报
回复
set @Sql1 = 'update sales set ' + @name + '= shu + liang where id=' + cast(@id as varchar)
exec sp_executesql @Sql1
symbol_bc 2009-09-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lixinramaxel41658133 的回复:]
引用 6 楼 symbol_bc 的回复:
而且这是一个实验的例子,其实subject 项会有很多,难道case... when ...一直这样下去吗

case...when是可以写成动态语句的!~~exec执行就可以了!~
[/Quote]


兄弟,我觉得你的方法也不错,咱俩的方法复杂度差不多,你帮我看看我的方法错误到底在那里????
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 symbol_bc 的回复:]
而且这是一个实验的例子,其实subject 项会有很多,难道case... when ...一直这样下去吗
[/Quote]
case...when是可以写成动态语句的!~~exec执行就可以了!~
bancxc 2009-09-17
  • 打赏
  • 举报
回复
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
  • 打赏
  • 举报
回复
你的表已经有了'一'、'二'、'三'这几列了呀??
我觉得这么设计是不是不太好!~~可以行列转化一下实现呀!~
symbol_bc 2009-09-17
  • 打赏
  • 举报
回复
而且这是一个实验的例子,其实subject 项会有很多,难道case... when ...一直这样下去吗
symbol_bc 2009-09-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lixinramaxel41658133 的回复:]
SQL codeifobject_id('tb')isnotnulldroptable tbcreatetable tb(idint,subjectvarchar(2),shuint,liangint)insert tbselect1,'一',100,200unionallselect2,'二',333,111unionallselect3,'三',44,55select*,(casewhen s¡­
[/Quote]

我的表结构是确定的,这样执行的画,多出了几列
  • 打赏
  • 举报
回复

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
symbol_bc 2009-09-17
  • 打赏
  • 举报
回复
shu 和 liang 我用的是 int 啊
symbol_bc 2009-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chen_ya_ping 的回复:]
shu和 liang这两个字段你用的是不是 nvarchar
用这个进行转换 cast(shu as int) +cast(liang as int) 前提是要能够进行转换,要是包含了非数字字符就出错。
[/Quote]

哥们你能来我太高行了,你的意思是不是换成这样
set @Sql1 = 'update sales set ' + @name + '= cast(shu as int) + cast(liang as int) where id=' + @id

可是还是不行啊
chen_ya_ping 2009-09-17
  • 打赏
  • 举报
回复
shu和 liang这两个字段你用的是不是 nvarchar
用这个进行转换 cast(shu as int) +cast(liang as int) 前提是要能够进行转换,要是包含了非数字字符就出错。

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧