Solved

Updating a Stored Procedure that is using a CURSOR

Posted on 2016-09-29
2
24 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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to query date ranges with SQL 6 24
Slow SQL query 12 21
Create calculation and case in query with times 13 12
sql query Help 12 29
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now