一个简单的sql效果

gongjie416 2010-07-27 04:28:14

name age
a 10
t 10
c 10
h 10
c 10
修改为:
name age
a 10
t 20
c 30
h 40
c 50
...全文
58 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
claro 2010-07-27
  • 打赏
  • 举报
回复

if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (name varchar(1),age int)
insert into #tb
select 'a',10 union all
select 't',10 union all
select 'c',10 union all
select 'h',10 union all
select 'c',10
go
declare @n int =0
update #tb set @n=age+@n,age=@n
go
select * from #tb

学习,的确金典。
claro 2010-07-27
  • 打赏
  • 举报
回复
了解。。
pt1314917 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 js_szy 的回复:]
SQL code
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (name varchar(1),age int)
insert into #tb
select 'a',10 union all
select 't',10 union all
s……
[/Quote]
顶5楼的一下,比较经典。总是忘记这个写法。。。
pt1314917 2010-07-27
  • 打赏
  • 举报
回复

--没有主键,很难实现修改。。2个列都不是唯一的。怎么改?效果好做。
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (name varchar(1),age int)
insert into [tb]
select 'a',10 union all
select 't',10 union all
select 'c',10 union all
select 'h',10 union all
select 'c',10
go

;with wsp
as
(select px=row_number() over(order by getdate()),name,age from tb)
select name,age=(select sum(age) from wsp where px<=a.px) from wsp a

--结果:
name age
---- -----------
a 10
t 20
c 30
h 40
c 50

华夏小卒 2010-07-27
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (name varchar(1),age int)
insert into #tb
select 'a',10 union all
select 't',10 union all
select 'c',10 union all
select 'h',10 union all
select 'c',10

declare @n int
set @n=0

update #tb set @n=age+@n,age=@n

select * from #tb

name age
---- -----------
a 10
t 20
c 30
h 40
c 50

(5 行受影响)
水族杰纶 2010-07-27
  • 打赏
  • 举报
回复
--> --> (wufeng4552)生成測試數據

if not object_id('tt') is null
drop table tt
Go
Create table tt([name] nvarchar(1),[age] int)
Insert tt
select N'a',10 union all
select N't',10 union all
select N'c',10 union all
select N'h',10 union all
select N'c',10
Go
;with ttt
as(
select
px=row_number()over(order by getdate()),
*
from tt)
select name,
age=(select sum(age)from ttt where px<=t.px)
from ttt t
/*
name age
---- -----------
a 10
t 20
c 30
h 40
c 50

(5 個資料列受到影響)

*/
百年树人 2010-07-27
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(1),[age] int)
insert [tb]
select 'a',10 union all
select 't',10 union all
select 'c',10 union all
select 'h',10 union all
select 'c',10

alter table tb add id int identity(1,1);
go

update t
set t.age=(select sum(age) from tb where id<=t.id)
from tb t
go

alter table tb drop column id;
go

select * from tb
/**
name age
---- -----------
a 10
t 20
c 30
h 40
c 50

(5 行受影响)
**/
pt1314917 2010-07-27
  • 打赏
  • 举报
回复
没有主键列?
gongjie416 2010-07-27
  • 打赏
  • 举报
回复
帮帮忙呀,

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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