询问一条undate语句

opou 2008-07-29 10:01:40

create table tb4(a int, b int, c int)
select * from tb4
--显示:
a b c
1 1 2 3
2 4 5 6
3 7 8 9
4 7 8 9

我想修改第四行中的8为10。
如何修改?
...全文
193 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2008-07-29
  • 打赏
  • 举报
回复

if object_id('tb4') is not null
drop table tb4
if object_id('c1') is not null
drop table c1
go

create table tb4
( a int,
b int,
c int
)
insert into tb4 select 1,2,3
union all select 4,5,6
union all select 7,8,9
union all select 7,8,9
go

select * ,identity(int,1,1) d INTO c1 from tb4
update c1 set b=10 where d=4
select a,b,c from c1
寒塘鹤影 2008-07-29
  • 打赏
  • 举报
回复
5楼的做法是有用: 他的做法是在原有的表中添加了一个主键字段. 然后,通过主键来修改的.

这样也可以吗?
zdgood 2008-07-29
  • 打赏
  • 举报
回复
5楼的语句可以显示上面的结果。LZ能否把原始的TABLE贴出来看看。
opou 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yueqing 的回复:]
SQL code
Create table tb4(a int, b int, c int)
insert into tb4 select 1,2,3 Union all select 4,5,6 Union all select 7,8,9 Union all select 7,8,9
select * from tb4

alter table tb4 add sid int identity(1,1)
select * from tb4
update tb4 set b=10 where sid=4
alter table tb4 drop column sid

select * from tb4
drop table tb4
[/Quote]

列名 'sid' 无效。
yueqing 2008-07-29
  • 打赏
  • 举报
回复

Create table tb4(a int, b int, c int)
insert into tb4 select 1,2,3 Union all select 4,5,6 Union all select 7,8,9 Union all select 7,8,9
select * from tb4

alter table tb4 add sid int identity(1,1)
select * from tb4
update tb4 set b=10 where sid=4
alter table tb4 drop column sid

select * from tb4
drop table tb4

水族杰纶 2008-07-29
  • 打赏
  • 举报
回复
if object_id('tb1') is not null
drop table tb1
go
create table tb1(a int, b int, c int)
insert tb1 select
1, 2, 3 union all select
4, 5, 6 union all select
7, 8, 9 union all select
7, 8, 9
if object_id('tempdb..#temp') is not null
drop table #temp
select identity(int,1,1) ID,* into #temp from tb1
update #temp set b=10 where id=4
truncate table tb1
insert tb1(a,b,c) select a,b,c from #temp
select * from tb1
drop table #temp

(4 行受影响)

(4 行受影响)

(1 行受影响)

(4 行受影响)
a b c
----------- ----------- -----------
1 2 3
4 5 6
7 8 9
7 10 9

(4 行受影响)
opou 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 WANGXUELLD 的回复:]
不知道搞什么
[/Quote]
3行和4行的数据是一模一样的,我只想修改第四行的b=10
wangxuelid 2008-07-29
  • 打赏
  • 举报
回复
不知道搞什么
opou 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wackyboy 的回复:]
SQL code
alter table tb4 add sid int identity(1,1)
update tb4 set b=10 where sid=4
alter table tb4 drop column sid
[/Quote]
还是不行啊
wackyboy 2008-07-29
  • 打赏
  • 举报
回复

alter table tb4 add sid int identity(1,1)
update tb4 set b=10 where sid=4
alter table tb4 drop column sid
opou 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lgxyz 的回复:]
create table tb4(a int, b int, c int)
select * from tb4
--显示:
a b c
1 1 2 3
2 4 5 6
3 7 8 9
4 7 8 9


这两行有什么区别
3 跟4 两行
你表只有三个字段?
重复的话,随便改一行不行?
[/Quote]

最好能修改我想修改的那一行。
opou 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wackyboy 的回复:]
SQL code
update tb4 set b=10 where b=(select top 1 b from tb4 where b not in (select top 3 b from tb4))
[/Quote]
运行结果:
(所影响的行数为 0 行)
lgxyz 2008-07-29
  • 打赏
  • 举报
回复
create table tb4(a int, b int, c int)
select * from tb4
--显示:
a b c
1 1 2 3
2 4 5 6
3 7 8 9
4 7 8 9


这两行有什么区别
3 跟4 两行
你表只有三个字段?
重复的话,随便改一行不行?
wackyboy 2008-07-29
  • 打赏
  • 举报
回复

update tb4 set b=10 where b=(selet top 1 b from tb4 where b not in (select top 3 b from tb4))

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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