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.
Hi,I need to create a cursor that will loop through my customer database to return matching rows of data based on my select statement criteria. I have written most of it based on what I remember from my limited SQL exposure at a previous job afew years ago, but I can't remember how to make the @cust_id varaible increment by 1 and loop to the end of the customer table.Can anyone steer me in the right direction here please?DECLARE @cust_id INTSET @cust_id = 371DECLARE my_cursor CURSOR FORSELECT CUSTOMER_ID, FULL_NAME, ADDRESS_LINE1, SUBURB, STATE, POSTCODE FROM CUSTOMER_LANGUAGE_DETAILS WHERE POSTCODE IN (SELECT POSTCODE FROM CUSTOMER_LANGUAGE_DETAILS WHERE CUSTOMER_ID = @cust_id AND INACTIVE = 0 ) AND CUSTOMER_ID <> @cust_idSELECT CUSTOMER_ID, FULL_NAME, ADDRESS_LINE1, SUBURB, STATE, POSTCODE FROM CUSTOMER_LANGUAGE_DETAILS WHERE CUSTOMER_ID = @cust_id AND INACTIVE = 0OPEN my_cursor--SET @cust_id = @cust_id + 1FETCH NEXT FROM my_cursorWHILE @@FETCH_STATUS = 0BEGIN FETCH NEXT FROM my_cursorENDCLOSE my_cursorDEALLOCATE my_cursor
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2007-07-09 : 19:23:42
You don't need to use cursor at all.Can you explain what are you trying to do with your query here ?KH[spoiler]Time is always against us[/spoiler]
haydn
Starting Member
5 Posts
Posted - 2007-07-09 : 19:37:02
I have a database with about 1500 customers in it and some are duplicate customers with the same or similar name and address. The select query that I have written returns rows of data that are likely to be matching customers based on their identical post codes. I am manually keying in each customer number from 1 to 1500 to return the required data. Once I find a match I am merging the duplicate customers with another script that I have. I thought it would be much quicker if I could use a cursor to return all of the rows of data and then I could visually scan and look for the duplicates and then run my merge script once I have indentified the duplicates.Any suggestions for me?Haydn.