時間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)
文章主要描述的是sql server UPDATE的賦值次序,假如你對SQL Server UPDATE的賦值次序?qū)嶋H操作感到十分頭疼?如果是這樣子的話,以下的文章將會給你相應(yīng)的解決方案,以下就是相關(guān)內(nèi)容的具體描述。
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;
結(jié)果:
- 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;
結(jié)果:
- @i = 2, @j = 1
- @i = 2, @j = 2
- @i = 1, @j = 1
- @i = 0, @j = 0
3) 字段之間, 并行執(zhí)行
- SET NOCOUNT ON;
- DECLARE @Table TABLE
- 關(guān)鍵詞標(biāo)簽:SQL Server
相關(guān)閱讀
熱門文章 SqlServer2005對現(xiàn)有數(shù)據(jù)進行分區(qū)具體步驟sql server系統(tǒng)表損壞的解決方法MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫用戶Access、SQL Server、Oracle常見應(yīng)用的區(qū)別
人氣排行 如何遠程備份(還原)SQL2000數(shù)據(jù)庫SQL2000數(shù)據(jù)庫遠程導(dǎo)入(導(dǎo)出)數(shù)據(jù)配置和注冊O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程SQL2000和SQL2005數(shù)據(jù)庫服務(wù)端口查看或修改SQL Server 2005降級到2000的正確操作步驟修改Sql Server唯一約束教程淺談JSP JDBC來連接SQL Server 2005的方法SQL Server創(chuàng)建表語句介紹