Friday, May 15, 2009

How to use SQL Server T-SQL cursors | Microsoft SQL Server Advisor
This is a brilliant little example script what absoluted HAVE TO use a cursor - my take generally on this is "If you have to use a cursor youre doing it wrong", however during some work for a client I needed to "duplicate" a hierarchy of items from different nested tables with foreign key references for a questionnaire something along the lines of
Questionnaire table links to Questions table, Questions table links to QuestionLinking table (Where a dependancy between one question and another is held) and so forth. I spent a while looking for a "clean" solution but the only option I found was to iterate through each Questionnaire, Question and Question link at a time as I needed to update the QuestionnaireId, QuestionId and QuestionLinkId from the Source Questionnaire to the Destination Questionnnaire row equivalents... if you have any patterns or approaches tot his type of thing not involving cursors I would love to hear from you!

How to use SQL Server T-SQL cursors Microsoft SQL Server Advisor

No comments: