首页 > 代码库 > SqlServer中临时表的使用

SqlServer中临时表的使用

 

http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html

 

--存储过程中将多表连接结果写入到临时表中,然后通过游标查询临时表内容  --判断临时表是否存在IF    OBJECT_ID(tempdb..#TmpTable‘) IS NOT NULLDROP TABLE #TmpTableSELECT a.[a1],a.[a1],b.[b1],b.[b2]INTO #TmpTableFROM A aLEFT JOIN B b ON a.a1 = b.a1

 

操作游标

DECLARE @orderId NVARCHAR(50),@customerId NVARCHAR(50),@employeeId NVARCHAR(50)DECLARE myCursor CURSOR FORSELECT o.OrderID,o.CustomerID,o.EmployeeID FROM Orders AS oOPEN myCursorfetch next from myCursor into @orderId,@customerId,@employeeIdwhile @@fetch_status=0 BEGINPRINT @orderIdPRINT @customerIdPRINT @employeeIdfetch next from myCursor into @orderId,@customerId,@employeeIdENDclose myCursor deallocate myCursor