update 语句中给变量赋值的问题

xiaomaoxia 2009-05-12 11:21:17
declare @b int
select @b=-1
update #Temp set yhdzdID=2,@b=yhdzdID,id=@b
select * from #Temp


出来的结果 id 列都是null

@b 都没有得到yhdzdID 列的值;都是null
...全文
566 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_wsx 2009-05-12
  • 打赏
  • 举报
回复
可以的呀!@b得到了yhdzdid的值
如下:
if object_id('temp') is not null drop table temp
select top 50 0 as id,100 as yhdzdID into temp
from syscolumns a,syscolumns b

declare @b int
select @b=-1
update Temp set yhdzdID=2,@b=yhdzdID,id=@b

select * from Temp

--更新前的值
0 100
0 100
0 100
0 100
0 100
0 100
0 100

--更新后的值
100 2
100 2
100 2
100 2
100 2
100 2
100 2
100 2
xiaomaoxia 2009-05-12
  • 打赏
  • 举报
回复
yhdzdID=2 只是测试。 现在主要的问题是@b 没有得到yhdzdID 的值
soft_wsx 2009-05-12
  • 打赏
  • 举报
回复
yhdzdID=2有意义吗?


结果弄成ID以全为2呀
SQL77 2009-05-12
  • 打赏
  • 举报
回复

declare @b int
select @b=-1
update #Temp set yhdzdID=2
update #Temp set @b=yhdzdID
update #Temp set ID=@B
select * from #Temp


这样试试看哦
lgx0914 2009-05-12
  • 打赏
  • 举报
回复
update #Temp set yhdzdID=2,id=yhdzdID 
ks_reny 2009-05-12
  • 打赏
  • 举报
回复

exec('update #Temp set yhdzdID=2,'+@b+'=yhdzdID,id='+@b )
xiaomaoxia 2009-05-12
  • 打赏
  • 举报
回复
declare @b int
select @b=-1
Create TABLE #Temp
(
日期 varchar(50),
凭证编号 varchar(50),
支票号 varchar(100),
借方金额 decimal(18,2),
贷方金额 decimal(18,2),
余额 decimal(18,2),
银行对账单ID int,

)

declare @Temp2 TABLE
(
ID int,
日 varchar(50),
编号 varchar(50),
借方金额1 decimal(18,2),
贷方金额1 decimal(18,2),
余额1 decimal(18,2)
)
update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID <>@b),@b=银行对账单ID

select * from #Temp


我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉;

看来没办法这么做了
xiaomaoxia 2009-05-12
  • 打赏
  • 举报
回复
实际上我的语句是这样的


declare @b int
select @b=-1
Create TABLE #Temp
(
日期 varchar(50),
凭证编号 varchar(50),
支票号 varchar(100),
借方金额 decimal(18,2),
贷方金额 decimal(18,2),
余额 decimal(18,2),
yhdzdID int,
id int
)

declare @Temp2 TABLE
(
ID int,
日 varchar(50),
编号 varchar(50),
借方金额1 decimal(18,2),
贷方金额1 decimal(18,2),
余额1 decimal(18,2)
)
update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID <>@b),@b=银行对账单ID

select * from #Temp


我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉;

看来没办法这么做了
xiaomaoxia 2009-05-12
  • 打赏
  • 举报
回复
实际上我的语句是这样的

declare @b int
select @b=-1
update @Temp set 银行对账单ID=(select top 1 ID from @Temp2 where 借方金额1=借方金额 and 贷方金额1=贷方金额 and ID<>@b),@b=银行对账单ID

select * from #Temp

我是想在给银行对账单ID更新值的时候把@Temp2 里相同的记录排除掉;

看来没办法这么做了
linguojin11 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zxkid 的回复:]
楼主想说明什么问题?
update语句,总是先执行变量的赋值再执行字段的赋值,所以
update #Temp set yhdzdID=2,@b=yhdzdID,id=@b
赋值次序是
1)@b=yhdzdID
2) yhdzdID=2, id=@b
所以id = 原yhdzdID字段值(而不是2)
[/Quote]
这位大哥回答很是
LZ可以试下就知道,你把对列yhdzdID=2放在外面,就是先赋值,然后
update #Temp set @b=yhdzdID,id=@b 此时值都是2。。。
linguojin11 2009-05-12
  • 打赏
  • 举报
回复
create table test(b int)
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
declare @a int
set @a=0
update test set @a=@a+1,b=@a
select * from test
go
drop table test
------------------------------------
create table test(b int)
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
insert into test select 1
declare @a int
set @a=0
update test set b=@a,@a=@a+1 --區別
select * from test
go
drop table test
sharelove 2009-05-12
  • 打赏
  • 举报
回复
declare @b int
select @b=-1
update #Temp set yhdzdID=2,@b=yhdzdID,id=@b
此條語句中的@b=yhdzdid沒有更新前的值,如果沒更新前是空值,那當然就是空值啦!
你雖@b=-1啦,但再語句中又賦啦一次把前邊給覆蓋啦!
sharelove 2009-05-12
  • 打赏
  • 举报
回复
update #Temp set yhdzdID=2,@b=yhdzdID,id=@b

這條一句是一次性更新的
那有更新變量值的?
karl133123 2009-05-12
  • 打赏
  • 举报
回复
#temp里没有数据怎么更新,当然都是null啦
lg3605119 2009-05-12
  • 打赏
  • 举报
回复
楼主的语句没问题~~UP
zxkid 2009-05-12
  • 打赏
  • 举报
回复
楼主想说明什么问题?
update语句,总是先执行变量的赋值再执行字段的赋值,所以
update #Temp set yhdzdID=2,@b=yhdzdID,id=@b
赋值次序是
1)@b=yhdzdID
2) yhdzdID=2, id=@b
所以id = 原yhdzdID字段值(而不是2)
yanleiyigan 2009-05-12
  • 打赏
  • 举报
回复
不可能吧

22,209

社区成员

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

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