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.
| Author |
Topic |
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-12-21 : 13:20:48
|
| Hi,I have a requirement wherein I need to use cursor to update destination table. The reason I need to use cursor is we want to have row level insertion happen in the dest table. The query that I am using does gives me error. Please let me know the correct syntax to use cursor to update the table:DECLARE cur CURSOR FOR BEGIN UPDATE FRU.dbo.ContactSET contact_Updated_ind = 'Y' , datetime_updated = getdate() , last_action_cd = 'U' , last_program = 'Fru_Loop' , last_source_system = 'StoreComplaince' , user_updated = USERFROM FRU.dbo.Contact C INNER JOIN Fru_Address WA ON WA.Contact_ID = C.Contact_ID INNER JOIN CIF.dbo.Address_Type A ON WA.Contact_ID = A.Contact_ID AND WA.Address_Type = A.Address_TypeWHERE WA.Contact_ID IS NOT NULL AND WA.Address_ID IS NOT NULL AND WA.Address_Type_ID IS NULL OPEN cur FETCH NEXT FROM cur WHILE @@FETCH_STATUS = 0 END CLOSE cur DEALLOCATE curPlease advice.Dp |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-12-21 : 14:25:58
|
quote: I have a requirement wherein I need to use cursor to update destination table. The reason I need to use cursor is we want to have row level insertion happen in the dest table
This makes absolutely NO sense. What is a "row-level insertion"? All INSERT operations in SQL Server insert rows. And are you updating the table or inserting new rows? You'll have to provide more details (table structure, sample data).Also, the UPDATE statement you've written will perform an UPDATE without the need for a cursor at all (a cursor will only slow it down). If you need UPDATE rows that match, and INSERT rows that do not match, you can use individual INSERT and UPDATE statements in a transaction, or the MERGE statement if you're using SQL Server 2008. Again, we'll need more specific details before we can help. |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-12-21 : 15:10:31
|
| Hi robvolk,Below is the re-worked version of the Cursor Query :DECLARE @ContactId int, @address_id int, @Address_Type varchar(50)DECLARE cur CURSOR FORSelect fru_address.contact_id, fru_address.address_id, fru_address.address_Typefrom fru_addressOPEN cur FETCH NEXT FROM cur into @ContactId,@address_id,@Address_TypeWHILE @@FETCH_STATUS = 0 BEGINUPDATE FRU.dbo.ContactSET contact_Updated_ind = 'Y' , datetime_updated = getdate() , last_action_cd = 'U' , last_program = 'Fruloop' , last_source_system = 'StoreCompliance' , user_updated = USERFROM FRU.dbo.Contact C INNER JOIN fru_address WA ON WA.Contact_ID = C.Contact_ID INNER JOIN FRU.dbo.Address_Type A ON WA.Contact_ID = A.Contact_ID AND WA.Address_Type = A.Address_TypeWHERE @ContactId IS NOT NULLAND @address_id IS NOT NULLAND @Address_Type IS NOT NULL-- WA.Contact_ID IS NOT NULL-- AND-- WA.Address_ID IS NOT NULL-- AND-- WA.Address_Type_ID IS NULLFETCH NEXT FROM cur into @ContactId,@address_id,@Address_TypeEND CLOSE cur DEALLOCATE curBased on the contact_id and address_type in the staging table this particular cursor query will just update the contact_Updated_ind flag. I undestand that there is absolute no need to use cursor for this updating task, but my management wants to have this way. No Choice. I need to guidance as to whether the join condition that the destination table is trying to do will remain as it is while using cursor or it requires any changes. Please let me know on this.Thank You,dpDp |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-12-21 : 16:13:27
|
You probably need to change the WHERE clause like so:WHERE WA.Contact_ID=@ContactId AND WA.Address_ID=@address_id AND WA.Address_Type=@Address_Type |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-12-21 : 17:58:14
|
| Thank You Brett and robvolk,After changing the code it worked : and I was able to verify that DECLARE @ContactId int,@address_id int,@Address_Type varchar(50)DECLARE cur CURSOR FORSelect fru_address.contact_id, fru_address.address_id,fru_address.address_Typefrom fru_addressOPEN cur FETCH NEXT FROM cur into @ContactId,@address_id,@Address_TypeWHILE @@FETCH_STATUS = 0 BEGINUPDATE FRU.dbo.ContactSETcontact_Updated_ind = 'Y', datetime_updated = getdate(), last_action_cd = 'U', last_program = 'Fruloop', last_source_system = 'StoreCompliance', user_updated = USERFROM FRU.dbo.Contact CINNER JOINfru_address WAON WA.Contact_ID = C.Contact_IDINNER JOINFRU.dbo.Address_Type AON WA.Contact_ID = A.Contact_IDAND WA.Address_Type = A.Address_TypeWHERE@ContactId IS NOT NULLAND @address_id IS NOT NULLAND @Address_Type IS NOT NULLAND WA.Contact_ID=@ContactId AND WA.Address_ID=@address_id AND WA.Address_Type=@Address_Type-- WA.Contact_ID IS NOT NULL-- AND-- WA.Address_ID IS NOT NULL-- AND-- WA.Address_Type_ID IS NULLFETCH NEXT FROM cur into @ContactId,@address_id,@Address_TypeEND CLOSE cur DEALLOCATE curThe piece I am working on was developed by somebody else and this was a last minute TR change :(.......I am still learning the Business. This system captures a contact information from the other source systems and then updates the information on itself if there is a match.I also have another query : Can we issue commit transaction statement after every row is updated using the cursor. In that way we can come to know how many rows were written before the job failed . I tried with begin transaction and close transaction statements but was not able to successfully plant it on the cursor. Please let me kow if there is a way to commit every update that happens through now using cursor.Thank youDp |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-12-21 : 18:59:43
|
can you tell me what the difference between what you have and this?UPDATE c SET contact_Updated_ind = 'Y' , datetime_updated = getdate() , last_action_cd = 'U' , last_program = 'Fruloop' , last_source_system = 'StoreCompliance' , user_updated = USER() FROM FRU.dbo.Contact CINNER JOIN fru_address WA ON WA.Contact_ID = C.Contact_IDINNER JOIN FRU.dbo.Address_Type A ON WA.Contact_ID = A.Contact_ID AND WA.Address_Type = A.Address_Type Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-12-22 : 09:33:45
|
| Hi X002548,There is no difference between the normal update statement you posted and the one with cursor that I have made. The reason that I am using cursor is beacuase of some error showing up when the SQL Agent job runs. The job keeps failing at the same step. The step at which it fails is the one which updates reacords on the dest table. With using cursor it will have row-level insertion and page level locks which might help me identify what kind of deadlock is occuring as the job keeps failing. Dp |
 |
|
|
|
|
|
|
|