1) 先变量再字段 SET NOCOUNT ON; DECLARE @i INT, @j INT INSERT @Table(Id1, Id2) SELECT @i = 1, @j = 0 SELECT @i = 1, @j = 0 SET NOCOUNT OFF; Id1 Id2 SET NOCOUNT ON; DECLARE @i INT, @j INT INSERT @Table(Id1, Id2) SELECT @i = 1, @j = 0 SELECT @i = 1, @j = 0 SELECT @i = 1, @j = 0 SELECT @i = 1, @j = 0 SET NOCOUNT OFF; SET NOCOUNT ON; DECLARE @Table TABLE INSERT @Table(Id1, Id2) UPDATE @Table SET Id1 = Id2, Id2 = Id1 SET NOCOUNT OFF;
DECLARE @Table TABLE
(
Id1 INT,
Id2 INT
);
SELECT 1, 10
UNION ALL
SELECT 2, 20
UNION ALL
SELECT 3, 30;
UPDATE @Table SET Id1 = @i, Id2 = Id1, @i = @i + 1
SELECT * FROM @Table
UPDATE @Table SET Id1 = @i, Id2 = @j, @j = @i + 10, @i = @i + 1
SELECT * FROM @Table
结果:
Id1 Id2
———– ———–
2 1
3 2
4 3
———– ———–
2 11
3 12
4 13
2) 变量之间, 从左到右
DECLARE @Table TABLE
(
Id1 INT,
Id2 INT
);
SELECT 1, 10;
UPDATE @Table SET @j = @i, @i = @i + 1
PRINT @i = + CAST(@i AS VARCHAR) + , @j = + CAST(@j AS VARCHAR)
UPDATE @Table SET @i = @i + 1, @j = @i
PRINT @i = + CAST(@i AS VARCHAR) + , @j = + CAST(@j AS VARCHAR)
UPDATE @Table SET @i = @j + 1, @j = @i
PRINT @i = + CAST(@i AS VARCHAR) + , @j = + CAST(@j AS VARCHAR)
UPDATE @Table SET @i = @j, @j = @i
PRINT @i = + CAST(@i AS VARCHAR) + , @j = + CAST(@j AS VARCHAR)
结果:
@i = 2, @j = 1
@i = 2, @j = 2
@i = 1, @j = 1
@i = 0, @j = 0
3) 字段之间, 并行执行
(
Id1 INT, Id2 INT
);
SELECT 1, 10
UNION ALL
SELECT 2, 20
UNION ALL
SELECT 3, 30;
SELECT * FROM @Table
结果:
Id1 Id2
———– ———–
10 1
20 2
30 3
sql server update的赋值次序_数据库技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » sql server update的赋值次序_数据库技巧
相关推荐
-      sql语句中的判断功能的使用方法
-      sql语句中的判断功能的使用方法
-      SQL语言中去掉小数点有效数字后面的所有0
-      ASP连接各种数据库的代码
-      在sql语句中实现md5功能
-      给access数据库减肥
-      asp连接access数据库代码(2)
-      更改 SQL Server 登录模式