[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Updating a Stored Procedure that is using a CURSOR

Posted on 2016-09-29
2
Medium Priority
?
65 Views
Last Modified: 2016-09-29
I have a Stored Procedure that uses a CURSOR.  How could this be changed to not use a CURSOR?

The attached is the test code for the Stored Procedure.
DeletePDQUsers.sql
0
Comment
Question by:wdbates
2 Comments
 
LVL 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 41822243
Caveat: You can do it without CURSOR. But this means one batch and on transaction instead of one transaction per row in the cursor.

DECLARE @Sql NVARCHAR(MAX);
DECLARE @p_UserID INT = 0;

WITH    Data
          AS ( SELECT DISTINCT
                        PDQTableName
               FROM     sPDQDATA.LTrack L
                        INNER JOIN sPDQDATA.Ements E ON L.LTID = E.LTID
                                                        AND E.UserID = @p_UserID
               WHERE    PDQTableName IS NOT NULL
             )
    SELECT  @Sql = ( SELECT 'DELETE FROM sPDQData.' + QUOTENAME(PDQTableName)
                            + ' WHERE UserID = '
                            + CAST(@p_UserID AS VARCHAR(12)) + '; '
                     FROM   Data D
    FOR     XML PATH('') ,
                TYPE
 ).value('.', 'NVARCHAR(MAX)');    

PRINT @Sql;
--EXECUTE (@Sql);

Open in new window

0
 

Author Closing Comment

by:wdbates
ID: 41822273
Great work!
Thank you for the quick response.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question