论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Sql Server教程
Tag:注入,存储过程,分页,安全,优化,加密,索引,日志,压缩,base64,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,触发器,socket,安装,sqlserver2000,sqlserver2005,sqlserver2008,视频教程

SQL SERVER中的赋值次序

文章类别:Sql Server | 发表日期:2009-8-5 10:48:49

SQL SERVER中的赋值次序

1) 先变量再字段

SET NOCOUNT ON;

DECLARE @i INT, @j INT
DECLARE @Table TABLE
(
 Id1 INT,
 Id2 INT
);

INSERT @Table(Id1, Id2)
SELECT 1, 10
UNION ALL
SELECT 2, 20
UNION ALL
SELECT 3, 30;

SELECT @i = 1, @j = 0
UPDATE @Table SET Id1 = @i, Id2 = Id1, @i = @i + 1
SELECT * FROM @Table

SELECT @i = 1, @j = 0
UPDATE @Table SET Id1 = @i, Id2 = @j, @j = @i + 10, @i = @i + 1
SELECT * FROM @Table

SET NOCOUNT OFF;
结果:
Id1         Id2        
----------- -----------
2           1
3           2
4           3

Id1         Id2        
----------- -----------
2           11
3           12
4           13


2) 变量之间, 从左到右

SET NOCOUNT ON;

DECLARE @i INT, @j INT
DECLARE @Table TABLE
(
    Id1 INT,
    Id2 INT
);

INSERT @Table(Id1, Id2)
SELECT 1, 10;

SELECT @i = 1, @j = 0
UPDATE @Table SET @j = @i, @i = @i + 1
PRINT '@i = ' + CAST(@i AS VARCHAR) + ', @j = ' + CAST(@j AS VARCHAR)

SELECT @i = 1, @j = 0
UPDATE @Table SET @i = @i + 1, @j = @i
PRINT '@i = ' + CAST(@i AS VARCHAR) + ', @j = ' + CAST(@j AS VARCHAR)

SELECT @i = 1, @j = 0
UPDATE @Table SET @i = @j + 1, @j = @i
PRINT '@i = ' + CAST(@i AS VARCHAR) + ', @j = ' + CAST(@j AS VARCHAR)

SELECT @i = 1, @j = 0
UPDATE @Table SET @i = @j, @j = @i
PRINT '@i = ' + CAST(@i AS VARCHAR) + ', @j = ' + CAST(@j AS VARCHAR)

SET NOCOUNT OFF;
结果:
@i = 2, @j = 1
@i = 2, @j = 2
@i = 1, @j = 1
@i = 0, @j = 0


3) 字段之间, 并行执行

SET NOCOUNT ON;

DECLARE @Table TABLE
(
 Id1 INT, Id2 INT
);

INSERT @Table(Id1, Id2)
SELECT 1, 10
UNION ALL
SELECT 2, 20
UNION ALL
SELECT 3, 30;

UPDATE @Table SET Id1 = Id2, Id2 = Id1
SELECT * FROM @Table

SET NOCOUNT OFF;
结果:
Id1         Id2        
----------- -----------
10          1
20          2
30          3

视频教程列表
文章教程搜索
 
Sql Server推荐教程
Sql Server热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058