update 问题

lily_ok 2007-03-22 11:06:24
table1
id status
1 0011
2 0011

table2
id s1 s2 s3 s4
1 1 3 6 9

我想做的是我传一个参数
如何参数在s1和s2之间就把0011变为0001 也就是把第三位变成1
如何参数在s3和s4之间就把0011变为0010 也就是把第四位变成1

用一条SQL语句
...全文
245 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
playwarcraft 2007-03-22
  • 打赏
  • 举报
回复
create table t1(id int,status varchar(04))
insert into t1 select 1,'0011'
insert into t1 select 2,'0011'

create table t2(id int,s1 int, s2 int,s3 int,s4 int)
insert into t2 select 1,1,3,6,9
insert into t2 select 2,6,9,1,3

declare @s int
set @s=2
update t1
set status=case when @s between s1 and s2 then stuff(status,3,1,'0')
when @s between s3 and s4 then stuff(status,4,1,'0')
else status end
from t1 ,t2
where t1.id=t2.id

select * from t1
/*
id status
----------- ------
1 0001
2 0010
*/

drop table t1,t2
chuifengde 2007-03-22
  • 打赏
  • 举报
回复

create proc test(@s int)
as

declare @a table(id int, status varchar(10))
insert @a select 1 ,'0011'
union all select 2 ,'0011'

declare @b table(id int, s1 int,s2 int,s3 int,s4 int)
insert @b select 1, 1, 3, 6, 9

update a set status= stuff(status,y,1,'0') from @a a,
(select case when @s between s1 and s2 then 3 else 4 end y from @b) b

select * from @a


test 6
paoluo 2007-03-22
  • 打赏
  • 举报
回复
红尘,when a.id between b.s1 and b.s2, 這個應該不對,應該是和傳進來的參數做比較
paoluo 2007-03-22
  • 打赏
  • 举报
回复
Create Table table1
(id Int,
status Char(4))
Insert table1 Select 1, '0011'
Union All Select 2, '0011'

Create Table table2
(id Int,
s1 Int,
s2 Int,
s3 Int,
s4 Int)
Insert table2 Select 1, 1, 3, 6, 9
GO
Declare @I Int
Select @I = 2
Update
A
Set
status = (Case When @I Between s1 And s2 Then Stuff(status, 3, 1, '0')
When @I Between s3 And s4 Then Stuff(status, 4, 1, '0')
Else status End)
From
table1 A
Inner Join
table2 B
On A.ID = B.ID

Select * From table1
GO
Drop Table table1, table2
--Result
/*
ID status
1 0001
2 0011
*/
子陌红尘 2007-03-22
  • 打赏
  • 举报
回复
update a
set
status=case
when a.id between b.s1 and b.s2 then stuff(a.status,3,1,'0')
when a.id between b.s3 and b.s4 then stuff(a.status,4,1,'0')
else a.status
end
from
table1 a,table2 b
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
对,为0,也就是改一下状态
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
比如说
如何参数s1〈 s2 就
update table1 set status= STUFF(status,3,1,'1')

如何参数s3〈 s4 就
update table1 set status= STUFF(status,4,1,'1')

paoluo 2007-03-22
  • 打赏
  • 举报
回复
應該是變為0,而不是1吧。
paoluo 2007-03-22
  • 打赏
  • 举报
回复

Declare @I Int
Select @I = 5
Update
A
Set
status = (Case When @I Between s1 And s2 Then Stuff(status, 3, 1, '0')
When @I Between s3 And s4 Then Stuff(status, 4, 1, '0')
Else status End)
From
table1 A
Inner Join
table2 B
On A.ID = B.ID
sp4 2007-03-22
  • 打赏
  • 举报
回复
说清楚一些
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
谢谢楼上各位,不过我发现了一问题
table1
id status
1 0011
2 0011

table2
id s1 s2 s3 s4
1 1 7 5 9

我想做的是我传一个参数
如何参数在s1和s2之间就把0011变为0001 也就是把第三位变成1
如何参数在s3和s4之间就把0011变为0010 也就是把第四位变成1

用一条SQL语句

比如说上边的的数据,我参数是6的话 就只变了一个条件的语句,只改变了一个状态
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
再帮帮我啊,大哥
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
这个方法不太好啊,那我要是有几位都要判断
比如说 0000000 这样的8位状态
嵌套的太多了啊
paoluo 2007-03-22
  • 打赏
  • 举报
回复
修改下

Create Table table1
(id Int,
status Char(4))
Insert table1 Select 1, '0011'
Union All Select 2, '0011'

Create Table table2
(id Int,
s1 Int,
s2 Int,
s3 Int,
s4 Int)
Insert table2 Select 1, 1, 7, 5, 9
GO
Declare @I Int
Select @I = 6
Update
A
Set
status = (Case When @I Between s1 And s2 And @I Between s3 And s4 Then Stuff(Stuff(status, 3, 1, '0'), 4 ,1, '0')
When @I Between s1 And s2 Then Stuff(status, 3, 1, '0')
When @I Between s3 And s4 Then Stuff(status, 4, 1, '0')
Else status End)
From
table1 A
Inner Join
table2 B
On A.ID = B.ID

Select * From table1
GO
Drop Table table1, table2
--Result
/*
ID status
1 0000
2 0011
*/
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
谢谢楼上各位,不过我发现了一问题
table1
id status
1 0011
2 0011

table2
id s1 s2 s3 s4
1 1 7 5 9

我想做的是我传一个参数
如何参数在s1和s2之间就把0011变为0001 也就是把第三位变成1
如何参数在s3和s4之间就把0011变为0010 也就是把第四位变成1

用一条SQL语句

比如说上边的的数据,我参数是6的话 就只变了一个条件的语句,只改变了一个状态
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
谢谢楼上各位,不过我发现了一问题
table1
id status
1 0011
2 0011

table2
id s1 s2 s3 s4
1 1 7 5 9

我想做的是我传一个参数
如何参数在s1和s2之间就把0011变为0001 也就是把第三位变成1
如何参数在s3和s4之间就把0011变为0010 也就是把第四位变成1

用一条SQL语句

比如说上边的的数据,我参数是6的话 就只变了一个条件的语句
lily_ok 2007-03-22
  • 打赏
  • 举报
回复
谢谢楼上各位,不过我发现了一问题
table1
id status
1 0011
2 0011

table2
id s1 s2 s3 s4
1 1 7 5 9

我想做的是我传一个参数
如何参数在s1和s2之间就把0011变为0001 也就是把第三位变成1
如何参数在s3和s4之间就把0011变为0010 也就是把第四位变成1

用一条SQL语句

比如说上边的的数据,我参数是6的话 就只变了一个条件的语句

34,593

社区成员

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

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