SQL SERVER UPDATE的赋值次序
2007-05-31 15:22:44 来源:WEB开发网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 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
更多精彩
赞助商链接