Loop and Temp Table usage in SQL SERVER
来源:互联网 发布:淘宝拒签申请哪种退款 编辑:程序博客网 时间:2024/05/22 04:26
Loop and Temp Table usage in SQL SERVER
Target :
To use Loop(may also use nested loop when needed) to fetch data then save into result table ,return result table
steps :
1.declare a result table
2.finish the data source query and save into #temptable
3.while(#temptable have data ) delete one row from #temptable
4.fetch data ,then fill in result table
sample codes :
Target :
To use Loop(may also use nested loop when needed) to fetch data then save into result table ,return result table
steps :
1.declare a result table
2.finish the data source query and save into #temptable
3.while(#temptable have data ) delete one row from #temptable
4.fetch data ,then fill in result table
sample codes :
BEGINDECLARE @FrmMstId UNIQUEIDENTIFIERset @FrmMstId ='0E38D831-AC9D-4DEC-9422-11998A834269'DECLARE @FieldsDataTable TABLE(SetID int,FieldName varchar(200),Value varchar(max))DECLARE @SetID AS int = 1DECLARE @Item AS nvarchar(20)DECLARE @Matter AS nvarchar(200)DECLARE @CostClaimed AS float = 0DECLARE @IsAgree AS int = 0DECLARE @ProposedAmt AS float = 0DECLARE @BocBreakdown AS nvarchar(200)--fetch boc itemIF Object_id('tempdb..#T1') IS NOT NULL DROP TABLE #T1 SELECT fbi.Id ,bi.Item,bi.Matter ,fbi.CostClaimed,fbi.IsAgree,fbi.ProposedAmtINTO #T1FROM CM.FrmMst fm JOIN HM.FrmBoc fb ON fm.Id = fb.FrmMstFKJOIN HM.FrmBocItem fbi ON fbi.FrmBocFK = fb.IdJOIN HM.BocItem bi ON bi.Id = fbi.BocItemFKWHERE fm.Id = @FrmMstId AND (bi.ExpiryDate > getdate() AND bi.effectiveDate <= getdate())AND fb.DeletedBy IS NULL ORDER BY CASE WHEN IsNumeric(bi.Item) = 1 THEN Right('0000000000' + bi.Item + '0', 10) ELSE Right('0000000000' + bi.Item, 10) END--process DECLARE @FrmBocItemId AS UNIQUEIDENTIFIERWHILE (SELECT Count(*) FROM #T1) > 0BEGIN SELECT TOP 1 @FrmBocItemId = Id, @Item = Item, @Matter = Matter, @CostClaimed = CostClaimed, @IsAgree = IsAgree, @ProposedAmt = ProposedAmtFROM #T1INSERT INTO @FieldsDataTable values(@SetID, 'Item',ISNULL(@Item,''));INSERT INTO @FieldsDataTable values(NULL, 'Matter',ISNULL(@Matter,''));INSERT INTO @FieldsDataTable values(NULL, 'CostClaimed',@CostClaimed);INSERT INTO @FieldsDataTable values(NULL, 'IsAgree',isNULL(@IsAgree,0));INSERT INTO @FieldsDataTable values(NULL, 'Proposed Amount',isNULL(@ProposedAmt,0));--process break down data--step 1: check temp table is not existIF Object_id('tempdb..#T2') IS NOT NULLDROP TABLE #T2--step 2:fetch data into temp tableDECLARE @BreakdownId uniqueidentifier;SELECT Id,BocItemBreakdownDesc INTO #T2 FROM HM.FrmBocItemBreakdown WHERE FrmBocItemFK = @FrmBocItemId;--step 3:loop fetch dataWHILE (SELECT Count(*) FROM #T2) > 0BEGINSELECT TOP 1 @BreakdownId = Id,@BocBreakdown = BocItemBreakdownDesc FROM #T2INSERT INTO @FieldsDataTable values(NULL, '',ISNULL(@BocBreakdown,''));DELETE #T2 WHERE Id = @BreakdownIdENDSET @SetID = @SetID + 1;DELETE #T1 WHERE Id = @FrmBocItemIdENDEND
- Loop and Temp Table usage in SQL SERVER
- SQL Collation issue in temp table.
- Temp Table in Oracle
- DROP TABLE IF EXISTS” in Oracle and SQL Server
- DROP TABLE IF EXISTS” in Oracle and SQL Server
- “DROP TABLE IF EXISTS” in Oracle and SQL Server
- SQL server Profiler and excution plan usage for CPU usage high analysis
- Implementing Table Inheritance in SQL Server
- Special Table Types In SQL Server
- Iteration operation to records in temp table
- SQL in ORACLE and SQL Server
- Function in loop and closure
- sql: table,view,function, procedure created MS_Description in sql server
- sql: table,view,function, procedure created MS_Description in sql server
- Comparing cursor vs. WHILE loop performance in SQL Server 2008(ZThttp://stackoverflow.com/questions)
- SQL Server Temporary Table
- SQL Server -- ALTER TABLE
- Demo of use SQL (Insert logs in system resource usage table)使用sql 的一个示例(插入系统使用日志数据)
- [AC自动机][fail树][BZOJ 3172][TJOI2013]单词
- android 源码学习经验分享
- mysql 5.6 Replication 参数
- 阅读文献的三大问题:坐不住,记不住,想不开
- 【翻译】仿射变换
- Loop and Temp Table usage in SQL SERVER
- java ServerSocket and Socket programming
- 继续说invoke伪指令
- 普通Jquery的ajax判断重复和formvalidator的ajaxValidator区别
- 软件测试几个等级
- uva 10401 - Injured Queen Problem
- jQuery formValidator表单验证插件一些摘要和例子
- Javascript继承机制(call、apply、prototype)
- google 技巧