Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
[code]DECLARE CSR CURSOR FORSELECT A.ID,B.ITEM,B.SALES_IDFROM TABLE1 AINNER JOIN TABLE 2 B ON A.ID = B.IDWHERE A.DATE BETWEEN @DATE_FROM AND @DATE_TOOPEN CSRFETCH NEXT FROM CSRINTO@ID,@ITEM@SALES_IDWHILE @@FETCH_STATUS = 0BEGIN SELECT @PURCHASE_PRICE = PURCHASE_PRICE FROM TABLE3 WHERE ITEM_CODE = @ITEM_CODE SELECT @SALES_PRICE = SALES_PRICE FROM TABLE4 WHERE SALES_ID = @SALES_ID SET @ENDING_BALANCE = @PURCHASE_PRICE - @SALES_PRICE INSERT INTO TABLE5 VALUES ( @ID, @ITEM, @PURCHASE_PRICE, @SALES_PRICE, @ENDING_BALANCE )FETCH NEXT FROM CSR@ID,@ITEM,@SALES_IDENDCLOSE CSRDEALLOCATED CSR[/code]In table1 and table2 have about 5000 data.This is sample for my code. I have a 9 or 10 queries in 1 stored procedure using cursor like this. And the result really take a lot of times (about 15-20 minutes).I need help to make my queries faster.Thank you for your help.
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2014-06-17 : 04:56:37
See if this works
INSERT INTO TABLE5SELECT t1.ID, t1.ITEM, t3.PURCHASE_PRICE, t4.SALES_PRICE, t3.PURCHASE_PRICE-t4.SALES_PRICE, (SELECT A.ID,B.ITEM,B.SALES_IDFROM TABLE1 AINNER JOIN TABLE 2 B ON A.ID = B.IDWHERE A.DATE BETWEEN @DATE_FROM AND @DATE_TO) as t1 left join TABLE3 as t3 on t2.ITEM_CODE=t3.ITEM_CODEleft join TABLE4 as t4 on t2.SALES_ID =t4.SALES_ID
MadhivananFailing to plan is Planning to fail
stepson
Aged Yak Warrior
545 Posts
Posted - 2014-06-17 : 04:56:39
[code]INSERT INTO TABLE5SELECT A.ID,B.ITEM,C.PURCHASE_PRICE,D.SALES_PRICE,C.PURCHASE_PRICE - D.SALES_PRICE AS ENDING_BALANCEFROM TABLE1 AINNER JOIN TABLE 2 B ON A.ID = B.IDINNER JOIN TABLE3 C ON C.ITEM_CODE = B.ITEMINNER JOIN TABLE4 D ON D.SALES_ID = B.SALES_IDWHERE A.DATE BETWEEN @DATE_FROM AND @DATE_TO[/code]sabinWeb MCP