| Author |
Topic |
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-28 : 18:03:32
|
| Can I replace a cursor that has while and if logic inside using temp tables?SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOALTER PROCEDURE dbo.sp_UPDATE_ALPHA_INDEX_PARTY @start_date datetime, @end_date datetime ASset nocount on DECLARE @instrument_id decimal(28, 0) DECLARE @party_id decimal(28, 0) DECLARE @party_data varchar(600) DECLARE @last_inst_id decimal(28, 0) DECLARE @sort_order int TRUNCATE TABLE ALPHA_INDEX_PARTY_DATA SET @sort_order = 0 SET @last_inst_id = 0 DECLARE main_Cursor CURSOR STATIC FOR SELECT I.INSTRUMENT_ID, P.PARTY_ID, '(' + CASE P.PARTY_TYPE WHEN 1 THEN 'D' WHEN 4 THEN 'I' ELSE 'N' END + ') ' + P.LAST_NAME + CASE WHEN P.FIRST_NAME IS NOT NULL AND P.FIRST_NAME <> '' THEN ', ' + P.FIRST_NAME ELSE '' END + CASE WHEN P.MIDDLE_NAME IS NOT NULL AND P.MIDDLE_NAME <> '' THEN ' ' + P.MIDDLE_NAME ELSE '' END + CASE WHEN P.NAME_SUFFIX IS NOT NULL AND P.NAME_SUFFIX <> '' THEN ' ' + P.NAME_SUFFIX ELSE '' END AS PARTY_DATA FROM INSTRUMENT I JOIN PARTY P ON I.INSTRUMENT_ID = P.INSTRUMENT_ID WHERE I.RECORDING_DATE BETWEEN @start_date AND @end_date AND I.DELETED_FLAG <> 1 OPEN main_Cursor FETCH NEXT FROM main_Cursor INTO @instrument_id, @party_id, @party_data WHILE @@FETCH_STATUS = 0 BEGIN IF @instrument_id != @last_inst_id BEGIN SET @sort_order = 0 END WHILE LEN(@party_data) > 0 BEGIN SET @sort_order = @sort_order + 1 INSERT INTO ALPHA_INDEX_PARTY_DATA (INSTRUMENT_ID, PARTY_ID, SORT_ORDER, PARTY_DATA) VALUES (@instrument_id, @party_id, @sort_order, LEFT(@party_data, 36)) IF LEN(@party_data) > 36 BEGIN SET @party_data = ' ' + LTRIM(RIGHT(@party_data, LEN(@party_data) - 36)) END ELSE BEGIN SET @party_data = '' END END SET @last_inst_id = @instrument_id FETCH NEXT FROM main_Cursor INTO @instrument_id, @party_id, @party_data END CLOSE main_Cursor DEALLOCATE main_CursorGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOhttp://www.sqlserverstudy.com |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-28 : 18:55:26
|
| Maybe but without DDL for the tables, INSERT INTO statements for sample data, expected output using that sample data, and an explanation as to what it's doing, it's very hard to help.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-28 : 19:11:41
|
| I have something like this.-------------------------------------------------------------------------- DROP TABLE TEMP1SET NOCOUNT ONTRUNCATE TABLE ALPHA_INDEX_PARTY_DATASELECT instrument_idINTO INST_TEMPFROM INSTRUMENTWHERE RECORDING_DATE BETWEEN '2007-01-01' and '2007-12-31' AND DELETED_FLAG <> 1-- select count(*) from inst_temp-- select top 1 * from inst_tempSELECT I.INSTRUMENT_ID, P.PARTY_ID, '(' + CASE P.PARTY_TYPE WHEN 1 THEN 'D' WHEN 4 THEN 'I' ELSE 'N' END + ') ' + P.LAST_NAME + CASE WHEN P.FIRST_NAME IS NOT NULL AND P.FIRST_NAME <> '' THEN ', ' + P.FIRST_NAME ELSE '' END + CASE WHEN P.MIDDLE_NAME IS NOT NULL AND P.MIDDLE_NAME <> '' THEN ' ' + P.MIDDLE_NAME ELSE '' END + CASE WHEN P.NAME_SUFFIX IS NOT NULL AND P.NAME_SUFFIX <> '' THEN ' ' + P.NAME_SUFFIX ELSE '' END AS PARTY_DATA INTO TEMP1 FROM INST_TEMP I JOIN PARTY P ON I.INSTRUMENT_ID = P.INSTRUMENT_ID-- select top 10 * from temp1 /*select instrument_id, party_id, sort_order, len(party_data) as Length, party_data from GG_ATRECORDING_QAT..ALPHA_INDEX_PARTY_DATA order by length */INSERT ALPHA_INDEX_PARTY_DATASELECT instrument_id, party_id, 1, LEFT(party_data, 36) FROM TEMP1WHERE LEN(PARTY_DATA) > 0INSERT ALPHA_INDEX_PARTY_DATASELECT instrument_id, party_id, 2, ' ' + LTRIM(RIGHT(party_data, LEN(party_data) - 36)), RIGHT(party_data, LEN(party_data) - 36), LEN(party_data) - 36, PARTY_DATA, LEN(PARTY_DATA)FROM TEMP1WHERE LEN(PARTY_DATA) > 36http://www.sqlserverstudy.com |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-28 : 19:46:18
|
| I don't really know what it's doing as I was requested to make it faster. But looks like it's getting records from instrument and party tables.http://www.sqlserverstudy.com |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-28 : 21:14:23
|
| We can't really help if you don't even have a firm grasp on it plus you didn't provide any of the requested information.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-28 : 22:06:28
|
| I'm not looking for an answer I just wanna find out if i could replace the cursor that has the while loop and if statements.http://www.sqlserverstudy.com |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-29 : 02:02:10
|
| And the answer is maybe. We can't answer the riddle unless you provide more information.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-31 : 12:44:36
|
| got some data.Party table-----------------------------------------instrument_id party_id last_name first_name middle_name name_suffix 3196655 8329324 CLUBB STACY L3318918 10350608 CROOKE,BESSIE J EST 3318918 10350858 JACKSON LLOYD D output-------------------instrument_id party_id sort_order party_data3196655 8329324 1 (I) CLUBB, STACY L3318918 10350608 2 (I) CROOKE,BESSIE J EST3318918 10350858 3 (I) JACKSON, LLOYD Dhttp://www.sqlserverstudy.com |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-31 : 13:51:57
|
| I'll qualify my answer by saying that I never use cursors. But what are cursors? They are basically a looping mechanism. So, I cannot imagine a scenario where you cannot replace a cursor with a while loop. So, I think they short answer is: Yes. You can replace a cursor with a while loop. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-31 : 14:45:17
|
| There is no point to replacing a cursor with a while loop, but I don't think that's what he is asking to do. My understanding is that he wants to lose the looping mechanism altogether and perhaps use temp tables instead.But who knows given the information that he has provided.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-31 : 15:21:20
|
Ahh, now I see. funketekun, can you provide a description of what you are trying to achomplish in words?Just going by your first sample bit of code I put this together, but I'm not sure exactly what you are trying to do: INSERT ALPHA_INDEX_PARTY_DATA ( INSTRUMENT_ID, PARTY_ID, SORT_ORDER, PARTY_DATA ) SELECT I.INSTRUMENT_ID, P.PARTY_ID, ROW_NUMBER() OVER(PARTITION BY I.INSTRUMENT_ID ORDER BY P.PARTY_ID) AS RowNum, LEFT ( '(' + CASE P.PARTY_TYPE WHEN 1 THEN 'D' WHEN 4 THEN 'I' ELSE 'N' END + ') ' + P.LAST_NAME + CASE WHEN P.FIRST_NAME IS NOT NULL AND P.FIRST_NAME <> '' THEN ', ' + P.FIRST_NAME ELSE '' END + CASE WHEN P.MIDDLE_NAME IS NOT NULL AND P.MIDDLE_NAME <> '' THEN ' ' + P.MIDDLE_NAME ELSE '' END + CASE WHEN P.NAME_SUFFIX IS NOT NULL AND P.NAME_SUFFIX <> '' THEN ' ' + P.NAME_SUFFIX ELSE '' END ,36) AS PARTY_DATAFROM INSTRUMENT I JOIN PARTY P ON I.INSTRUMENT_ID = P.INSTRUMENT_IDWHERE I.RECORDING_DATE BETWEEN @start_date AND @end_date AND I.DELETED_FLAG <> 1 |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-31 : 17:09:59
|
| It's like pulling teeth. Perhaps he doesn't want help after all.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-31 : 17:29:41
|
| i just want to know how i could eliminate the cursor. its not a hard question for experts....http://www.sqlserverstudy.com |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-31 : 17:45:09
|
| See my post on 15:21:20. Cursor eliminated. |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-03-31 : 18:43:39
|
| Server: Msg 195, Level 15, State 10, Line 12'ROW_NUMBER' is not a recognized function name.http://www.sqlserverstudy.com |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-04-01 : 13:35:48
|
| You are too funny.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
KenW
Constraint Violating Yak Guru
391 Posts |
Posted - 2008-04-01 : 13:40:39
|
quote: Originally posted by funketekun Seems like it can't be done. I wonder if there is any expert here.
Does anybody else get tired of these type questions? You know, the ones that say:quote: I have this question. I'm not going to give you any information about the data, the schema, or the expected results. I'm also not going to answer any follow-up questions you may have, although I may respond with other nonsensical posts that add nothing to the discussion. Can you help me?
And then later:quote: Isn't there an expert here who can help me? You guys must all be dummies, cuz you haven't answered me.
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2008-04-01 : 16:07:45
|
| i gave u all u want..what else do u want. |
 |
|
|
Next Page
|