Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi when to use temp tables in sql and please give me an example while updating and deleting
Posted

Does msdn site is closed? No.

See this: http://msdn.microsoft.com/en-us/library/ms175010%28v=sql.100%29.aspx[^] and this: http://msdn.microsoft.com/en-us/library/ms174979.aspx[^]
There you'll find interesting examples.
 
Share this answer
 
SQL
DECLARE @WorkCode VARCHAR(25)
SET @WorkCode='61163201328'

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp

CREATE TABLE #Temp(WorkId VARCHAR(50),TLabourAmt decimal(18,2),TMaterialAmt decimal(18,2)
)

DELETE FROM #Temp

INSERT INTO #Temp SELECT WME.workID,ISNULL(SUM(WME.labourAmt_1),0),ISNULL(SUM(WME.materialAmt_1),0)
FROM dbo.nregs_worksMeasurement WME INNER JOIN dbo.nregs_worksMaster WM ON WM.workID=WME.workID
WHERE WM.measurementCount=1
GROUP BY WME.workID

INSERT INTO #Temp SELECT WME.workID,ISNULL(SUM(WME.labourAmt_1),0),ISNULL(SUM(WME.materialAmt_1),0)
FROM dbo.nregs_worksMeasurement WME INNER JOIN dbo.nregs_worksMaster WM ON WM.workID=WME.workID
WHERE WM.measurementCount=2 
GROUP BY WME.workID

INSERT INTO #Temp SELECT WME.workID,ISNULL(SUM(WME.labourAmt_1),0),ISNULL(SUM(WME.materialAmt_1),0)
FROM dbo.nregs_worksMeasurement WME INNER JOIN dbo.nregs_worksMaster WM ON WM.workID=WME.workID
WHERE WM.measurementCount=3 
GROUP BY WME.workID

INSERT INTO #Temp SELECT WME.workID,ISNULL(SUM(WME.labourAmt_1),0),ISNULL(SUM(WME.materialAmt_1),0)
FROM dbo.nregs_worksMeasurement WME INNER JOIN dbo.nregs_worksMaster WM ON WM.workID=WME.workID
WHERE WM.measurementCount=4 
GROUP BY WME.workID

SELECT * FROM #Temp order by WorkId



Here i am using temp table for inserting based on measurementCount
 
Share this answer
 
Comments
surendranew 22-Mar-13 1:42am    
Thank you Gopi can i use the temporary table concept while updating the table,i'm not getting the idea when to use them exactly
Gopinath_Rajan 22-Mar-13 6:04am    
After creating temp table its like a normal table only u can do any changes to using this temp table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900