Loop and Temp Table usage in SQL SERVER
2013-06-21 12:27
447 查看
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 :
BEGIN DECLARE @FrmMstId UNIQUEIDENTIFIER set @FrmMstId ='0E38D831-AC9D-4DEC-9422-11998A834269' DECLARE @FieldsDataTable TABLE ( SetID int, FieldName varchar(200), Value varchar(max) ) DECLARE @SetID AS int = 1 DECLARE @Item AS nvarchar(20) DECLARE @Matter AS nvarchar(200) DECLARE @CostClaimed AS float = 0 DECLARE @IsAgree AS int = 0 DECLARE @ProposedAmt AS float = 0 DECLARE @BocBreakdown AS nvarchar(200) --fetch boc item IF Object_id('tempdb..#T1') IS NOT NULL DROP TABLE #T1 SELECT fbi.Id ,bi.Item, bi.Matter ,fbi.CostClaimed,fbi.IsAgree,fbi.ProposedAmt INTO #T1 FROM CM.FrmMst fm JOIN HM.FrmBoc fb ON fm.Id = fb.FrmMstFK JOIN HM.FrmBocItem fbi ON fbi.FrmBocFK = fb.Id JOIN HM.BocItem bi ON bi.Id = fbi.BocItemFK WHERE 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 UNIQUEIDENTIFIER WHILE (SELECT Count(*) FROM #T1) > 0 BEGIN SELECT TOP 1 @FrmBocItemId = Id, @Item = Item, @Matter = Matter, @CostClaimed = CostClaimed, @IsAgree = IsAgree, @ProposedAmt = ProposedAmt FROM #T1 INSERT 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 exist IF Object_id('tempdb..#T2') IS NOT NULL DROP TABLE #T2 --step 2:fetch data into temp table DECLARE @BreakdownId uniqueidentifier; SELECT Id,BocItemBreakdownDesc INTO #T2 FROM HM.FrmBocItemBreakdown WHERE FrmBocItemFK = @FrmBocItemId; --step 3:loop fetch data WHILE (SELECT Count(*) FROM #T2) > 0 BEGIN SELECT TOP 1 @BreakdownId = Id,@BocBreakdown = BocItemBreakdownDesc FROM #T2 INSERT INTO @FieldsDataTable values(NULL, '',ISNULL(@BocBreakdown,'')); DELETE #T2 WHERE Id = @BreakdownId END SET @SetID = @SetID + 1; DELETE #T1 WHERE Id = @FrmBocItemId END END
相关文章推荐
- [优秀英文技术文章转载]Table Value Parameters in SQL Server 2008 and .NET (C#)
- 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
- Using NOLOCK and READPAST table hints in SQL Server
- Top 10 steps to optimize data access in SQL Server: Part V (Optimize database files and apply partitioning)
- sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code
- 错误:"SQL Server Error:Table unknow tb_temp At line 1,column 30"之解决
- Temporary Tables vs. Table Variables and Their Effect on SQL Server Performance
- SQL in ORACLE and SQL Server
- Get XML tree format based on the tree data in SQL server table
- Copy a table from one database to another in SQL Server 2005
- (转载)The shortest, fastest, and easiest way to compare two tables in SQL Server: UNION
- Partitioned Tables and Indexes in SQL Server 2005
- An Introduction to Dynamic Management Views and Functions in SQL Server 2005
- add column for a table if this column not exists using T-SQL in SQLServer
- More with SQL Server 2005 : Top n Per Group, Paging, and Common Table Expressions
- How to monitor blocking in SQL Server 2005 and in SQL Server 2000 [ZT-MS]
- SQL Server error "Xml data type is not supported in distributed queries" and workaround for it
- [翻译-收藏] Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005