經過MSSQL中的用戶自定義表類型能夠快速將須要處理的數據存儲起來,生成新的臨時表(這裏使用變量表),而後根據表中字段進行批處理替代遊標。spa
用戶自定義表類型code
0 --建立用戶自定義表類型
1 Create Type [dbo].[type_XXXTable] As Table( 2 [Item1] [Varchar](255) Null, 3 [Item2] [Varchar](255) Null, 4 [Item3] [Varchar](255) Null, 5 [Item4] [Varchar](255) Null, 6 [Item5] [Varchar](255) Null, 7 [Item6] [Varchar](255) Null, 8 [Item7] [Varchar](255) Null, 9 [Item8] [Varchar](255) Null, 10 [Item9] [Varchar](255) Null, 11 [Item10] [Varchar](255) Null, 12 [ItemInt1] [Int] Null, 13 [ItemInt2] [Int] Null, 14 [ItemInt3] [Int] Null, 15 [ItemInt4] [Int] Null, 16 [ItemInt5] [Int] Null, 17 [ItemFloat1] [Decimal](28, 8) Null, 18 [ItemFloat2] [Decimal](28, 8) Null, 19 [ItemFloat3] [Decimal](28, 8) Null, 20 [ItemFloat4] [Decimal](28, 8) Null, 21 [ItemFloat5] [Decimal](28, 8) Null, 22 [ItemDateTime1] [DateTime] Null, 23 [ItemDateTime2] [DateTime] Null 24 ) 25 Go
0 --引用新數據表類型存儲t_XXX表的FieldName1
1 Declare @XXX type_XXXTable 2 Insert Into @XXX(Item1) --Item1爲剛纔建立表類型的第一個字段 3 Select FIeldNmae1 From t_XXX
1 --將遊標動做改寫爲表處理 2 eg: 3 Select FieldName1 Into #t_XXX
2018-07-14 14:34:38 blog