| Author |
Topic |
|
swoozie
Starting Member
25 Posts |
Posted - 2007-10-26 : 14:59:23
|
| I have a stored procedure that I am trying to write. I want it to Grab a list of ids from one table, then loop through that list and select a group of records from a second table based on the first list. The 2nd list can have 1 + records per item from the first list. With in that record, I need to check the values in 2 fields, if the values = something I update a third field and move on to the next. So I need to be able to loop thru the second set also. Currently I have a cursor with in a cursor, but was told that was slow and not a good idea, so I am trying to figure out what other options I have.Thanks in AdvanceSwoozie |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 15:44:17
|
| Pick up a book called learn TSQL in 21 days, its a really, really good book for beginners, you'll like it and there are many examples in it. There’s a whole lot more than just learning how to write an SP when it comes to good optimization and recompiling. |
 |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 15:46:31
|
quote: Originally posted by richard75013 Pick up a book called learn TSQL in 21 days, its a really, really good book for beginners, you'll like it and there are many examples in it. There’s a whole lot more than just learning how to write an SP when it comes to good optimization and recompiling.
crap, posted this to the wrong thread, On this one you want to insert the data from the 1st select into a # temp table, from there insert the 2nd query into another temp table from there update the data. It might be a little more involved than that but I dont have enough detail. Post your cursor code for more detailed info. |
 |
|
|
swoozie
Starting Member
25 Posts |
Posted - 2007-10-26 : 16:12:35
|
I think I got all company info out of this--CREATE PROCEDURE sp_RemoveDups--AS--Begin Declare @ID varchar(11), @Bad varchar(8), @Good varchar(8), @BadID varchar(8)--Create temptable--GOIF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'temp_RemoveDups')DROP TABLE temp_RemoveDups --GOCREATE TABLE temp_RemoveDups ([ID] [varchar] (11) COLLATE SQL_Latin1_General_CP1_CI_AS NULL )ON [PRIMARY]--Add new Data to table. This Data is the ID of the duplicates in the Db.TblINSERT INTO temp_RemoveDups SELECT ID--, count(Fld) FROM TblWHERE ID in (SELECT FldFROM db..tbl --change to db for productionGROUP BY FldHAVING count(tbl_id) = 1)GROUP BY IDHAVING count(Fld) > 1--Declare a cursorDECLARE temp_RemoveDups_cursor CURSOR-- FOR SELECT ID FROM temp_RemoveDups WHERE ID IN ( 'Item','item') FOR SELECT ID FROM temp_RemoveDupsOPEN temp_RemoveDups_cursor FETCH temp_RemoveDups_cursor INTO @Id WHILE @@FETCH_STATUS = 0 BEGINset @Good = (SELECT this FROM db..tbl WHERE fld = @ID)--Place the code for looping the recordset DECLARE MoreThanOne Cursor FOR SELECT Fld FROM Tbl WHERE ID = @ID AND Fld <> @Good ORDER BY Fld OPEN MoreThanOne FETCH MoreThanOne INTO @BadID WHILE @@Fetch_STATUS = 0 BEGIN set @Bad = @BadID END --Change/Switch the numbers EXEC ghpsp_change_Fld @Bad, @Good FETCH MoreThanOne INTO @BadID End Close MoreThanOne Deallocate MoreThanOne--set @Bad = (SELECT Fld FROM Tbl WHERE ID = @ID AND Fld <> @Good)FETCH temp_RemoveDups_cursor INTO @ID --END CLOSE temp_RemoveDups_cursor DEALLOCATE temp_RemoveDups_cursor--END--GO |
 |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 16:24:10
|
| ok, looking over now. |
 |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 16:44:22
|
| crap, had this half way typed up and IE died. hold on a sec. |
 |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 16:53:31
|
| ok create the work table but add a new field to house the main tables primary key data to uniquly ident it.CREATE TABLE temp_RemoveDups (PK_FIELD_HERE, [ID] [varchar] (11) )ON [PRIMARY]populate the table, only need a part of what you had.insert into temp_RemoveDups ([ID])SELECT FldFROM db..tbl --change to db for productionGROUP BY FldHAVING count(tbl_id) = 1update the temp wotking table with the pk from the parent.update tmpset tmp.PK_FIELD_HERE = master.PK_Fieldfrom temp_RemoveDups tmpjoin master_table_name_here masteron master.[ID]= tmp.[ID]to delete ONLY the dup records from the main table run this:begin trandelete masterfrom master_table_name_here masterjoin temp_RemoveDups tmpon master.[ID]= tmp.[ID]where master.PK_Field not in (select tmp.PK_FIELD_HERE from temp_RemoveDups tmp)run select statement here to make sure it all looks good here, just as a precaution. if you like what you got run COMMIT.to update the main table and set some field to indicate its a dup run this:begin tranupdate masterset master.status_field_name_here = X from master_table_name_here masterjoin temp_RemoveDups tmpon master.[ID]= tmp.[ID]where master.PK_Field not in (select tmp.PK_FIELD_HERE from temp_RemoveDups tmp)THIS IS GOING OFF OF THE ASSUMPTION THAT YOU DONT HAVE ANY OTHER QUALIFIERS SUCH AS KEEP THE LATEST DUP OR SOMETHING TO THAT AFFECT IN WHICH CASE YOU WILL NEED TO PUT THAT LOGIC IN THE STATEMENTS. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-10-26 : 18:09:20
|
| At first glance it appears that you can perform the update with just one query without using temp tables or cursors. Do you have some sample DDL and data you can provide? Matybe I am missing something, but it seems you are over complicating things. If you can provide sample inputs and outputs I bet someone can come up with good solution. Or perhaps Richards solution works just fine...? |
 |
|
|
richard75013
Starting Member
19 Posts |
Posted - 2007-10-26 : 20:59:13
|
| With this code you can dedup the table in 1 shot. Its not over complicating and I gave you all the code you need unless you have some other requirements. I normally dont participat in Forums just because I dont care to spend the time on them. I am a seasoned DBA and have designed OLTP systems for Radio Shack, 2 seperate 5TB data wearhouses for 2 different companies with ETL that loads 14million rows per night and the other that loaded 2.4 billion rows every 2 weeks. each of them contained over 12 billion rows and 1 of them was written up on a MS case Study. There isnt much I havent done, dont get me wrong, I still learn nearly everyday but I have a considerable amount of experience with MS SQL since version 6.5.if you have any questions on the code I provided I will answer, just let me know.Thanks..Richard |
 |
|
|
swoozie
Starting Member
25 Posts |
Posted - 2007-10-30 : 15:03:08
|
| Thanks Richard. I am sorry it took me so long to get back to the forum, I had forgotten to book mark it. It is so funny the way you write, reminds me of a guy I used to work with named Richard too. You all must think a like :) |
 |
|
|
|