Solved

Updating a Stored Procedure that is using a CURSOR

Posted on 2016-09-29
2
38 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 33

Accepted Solution

by:
ste5an earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Designer 19 47
SQL Server - Set Field Values ito Zero Based on Related Table 4 29
performance query 4 24
MSSQL Import .SQL files - Increase Import Speed 9 44
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

825 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