大家进来讨论一下这样的赋值操作

winstonbonaparte 2010-09-23 03:53:45
declare @U_FPJE numeric(24,8)
update @Temp set @FPJE = FPJE = JE - FP + @FPJE

/*说明下@Temp是个临时表。JE和FP的定义如下:
JE numeric(24,8) null,
FP numeric(24,8) null,*/


不是很明白这个语句的意思和过程,虽然结果是明白的。
这个临时表的FPJE这个字段是保存本行记录和上面的所有记录加减后的结果。但为什么会产生这种效果就不是很明白了,有哪位说明一下的
...全文
166 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
z184931481 2010-09-27
  • 打赏
  • 举报
回复
参数的递加值传递
winstonbonaparte 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hao1hao2hao3 的回复:]

值传递,可以拆开看

@FPJE = FPJE = JE - FP + @FPJE


1、FPJE = JE - FP + @FPJE
2、@FPJE = FPJE

每条记录都会做一次这样的处理。
[/Quote]
噢,我原來只想到了會改變那行的值,沒想到它循環的意思
Rotel-刘志东 2010-09-23
  • 打赏
  • 举报
回复
declare @temp table(fpje numeric(24,8),je numeric(24,8),fp numeric(24,8))
declare @U_FPJE numeric(24,8)
set @U_FPJE = 20.00
insert into @temp select 0,10.00,20.00

update @Temp set @U_FPJE = FPJE = JE - FP + @U_FPJE

select * from @temp
select @U_FPJE as [@u_fpje]
/*
fpje je fp
--------------------------------------- --------------------- ---------------------10.00000000 10.00000000 20.00000000

(1 行受影响)

*/
hao1hao2hao3 2010-09-23
  • 打赏
  • 举报
回复
值传递,可以拆开看

@FPJE = FPJE = JE - FP + @FPJE


1、FPJE = JE - FP + @FPJE
2、@FPJE = FPJE

每条记录都会做一次这样的处理。

ai_li7758521 2010-09-23
  • 打赏
  • 举报
回复
关键在于@U_FPJE,每更新一行数据,它就更新一次。

declare @temp table(id int identity,fpje numeric(24,8),je numeric(24,8),fp numeric(24,8))
declare @U_FPJE numeric(24,8)
set @U_FPJE = 20.00
insert into @temp
select 0,10.00,20.00 union all
select 0,10.00,20.00 union all
select 0,10.00,20.00 union all
select 0,10.00,20.00

测试1:
update @Temp
set FPJE = JE - FP + @U_FPJE
select * from @temp

id fpje je fp
----------- --------------------------------------- --------------------------------------- ---------------------------------------
1 10.00000000 10.00000000 20.00000000
2 10.00000000 10.00000000 20.00000000
3 10.00000000 10.00000000 20.00000000
4 10.00000000 10.00000000 20.00000000

测试2:
update @Temp
set @U_FPJE=FPJE = JE - FP + @U_FPJE
select * from @temp

id fpje je fp
----------- --------------------------------------- --------------------------------------- ---------------------------------------
1 10.00000000 10.00000000 20.00000000
2 0.00000000 10.00000000 20.00000000
3 -10.00000000 10.00000000 20.00000000
4 -20.00000000 10.00000000 20.00000000

(4 行受影响)
-狙击手- 2010-09-23
  • 打赏
  • 举报
回复

当多行是@U_FPJE是结果集的最后一条记录运算结果,注意这个变量是一直变化的
declare @temp table(fpje numeric(24,8),je numeric(24,8),fp numeric(24,8))
declare @U_FPJE numeric(24,8)
set @U_FPJE = 20.00
insert into @temp select 0,10.00,20.00
insert into @temp select 0,30.00,40.00
update @Temp set @U_FPJE = FPJE = JE - FP + @U_FPJE

select * from @temp
select @U_FPJE as [@u_fpje]
/*
fpje je fp
--------------------------------------- --------------------------------------- ---------------------------------------
10.00000000 10.00000000 20.00000000
0.00000000 30.00000000 40.00000000

(2 行受影响)

@u_fpje
---------------------------------------
0.00000000

(1 行受影响)

*/

dawugui 2010-09-23
  • 打赏
  • 举报
回复
参考这个例。
一张表..本来只有ID,ParentID字段 
如这样:
ID ParentID
1 null
2 null
3 1
4 3
5 4
....
现在加了一个字段 PIDs
想通过一条UPDATE 语句来实现下面这种结果...
ID ParentID PIDs
1 null null
2 null null
3 1 1
4 3 1,3
5 4 1,3,4
....

能否...不过也应该很基础地吧...?


if object_id('tb') is not null drop table tb
go
create table tb(ID int, ParentID int)
insert tb
select 1 , null union all
select 2 , null union all
select 3 , 1 union all
select 4 , 3 union all
select 5 , 4

alter table tb add PIDs varchar(200)

declare @i varchar(200)

update tb set pids=stuff(@i,1,1,''),
@i=isnull(@i,'')+','+ltrim(ParentID)

select * from tb


drop table tb

ID ParentID PIDs
----------- ----------- ----
1 NULL NULL
2 NULL NULL
3 1 1
4 3 1,3
5 4 1,3,4

(5 行受影响)
dawugui 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 winstonbonaparte 的回复:]
有多行记录也是可以这样值传递下去吗?[/Quote]

是的,是累计的。
dawugui 2010-09-23
  • 打赏
  • 举报
回复
declare @U_FPJE numeric(24,8)
update @Temp set FPJE = JE - FP + @FPJE



一个一个累计的。
winstonbonaparte 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyflystone 的回复:]

值传递

自己写一个测试应该明白了
SQL code
declare @temp table(fpje numeric(24,8),je numeric(24,8),fp numeric(24,8))
declare @U_FPJE numeric(24,8)
set @U_FPJE = 20.00
insert into @temp select 0,10.00,20.00

upda……
[/Quote]
有多行记录也是可以这样值传递下去吗?
winstonbonaparte 2010-09-23
  • 打赏
  • 举报
回复

declare @FPJE numeric(24,8)
update @Temp set @FPJE = FPJE = JE - FP + @FPJE
--写错了
-狙击手- 2010-09-23
  • 打赏
  • 举报
回复
值传递

自己写一个测试应该明白了
declare @temp table(fpje numeric(24,8),je numeric(24,8),fp numeric(24,8))
declare @U_FPJE numeric(24,8)
set @U_FPJE = 20.00
insert into @temp select 0,10.00,20.00

update @Temp set @U_FPJE = FPJE = JE - FP + @U_FPJE

select * from @temp
select @U_FPJE as [@u_fpje]
/*
fpje je fp
--------------------------------------- --------------------------------------- ---------------------------------------
10.00000000 10.00000000 20.00000000

(1 行受影响)

@u_fpje
---------------------------------------
10.00000000

(1 行受影响)

*/
claro 2010-09-23
  • 打赏
  • 举报
回复
@U_FPJE 没有用?

22,210

社区成员

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

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