Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor from variable


Cursor from variable

Author
Message
danielfountain
danielfountain
Right there with Babe
Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)

Group: General Forum Members
Points: 793 Visits: 888
Hi all,

I am using a cursor (i know - but this is actually something that is a procedural loop).

So effectively i have a table of names of stored procedures. I now have a store proc that loops around these procs and runs each one in order.

Now i am thinking i would like to be able to set the table it loops around in a variable at the start - is it possible to do this? So effectively use a tablename in a variable to use in the sql to define a cursor?

Many thanks

Dan
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6836 Visits: 17771
danielfountain (5/8/2014)
Hi all,

I am using a cursor (i know - but this is actually something that is a procedural loop).

So effectively i have a table of names of stored procedures. I now have a store proc that loops around these procs and runs each one in order.

Now i am thinking i would like to be able to set the table it loops around in a variable at the start - is it possible to do this? So effectively use a tablename in a variable to use in the sql to define a cursor?

Many thanks

Dan


Passing the table name in a variable means that you will have to do this with dynamic sql. No problems there and straight forward really. Complexities start to pop up though if the tables have different structure, column names, data types etc..
Cool
danielfountain
danielfountain
Right there with Babe
Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)Right there with Babe (793 reputation)

Group: General Forum Members
Points: 793 Visits: 888
Hi,

To answer your question - the tables will all be identical, just with different procedures within.

I have done dynamic SQL before with sp_executesql however i cant work out the way to use that as a cursor?

Thanks for your advice.

Dan
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6836 Visits: 17771
To get you started
Cool

Sample data
USE tempdb;
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'TBL_STOREDPROC_001' AND TABLE_SCHEMA = N'dbo')
DROP TABLE dbo.TBL_STOREDPROC_001;
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'TBL_STOREDPROC_002' AND TABLE_SCHEMA = N'dbo')
DROP TABLE dbo.TBL_STOREDPROC_002;

CREATE TABLE dbo.TBL_STOREDPROC_001
(
STOREDPROC_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,STOREDPROC_NAME NVARCHAR(1024) NOT NULL
);

CREATE TABLE dbo.TBL_STOREDPROC_002
(
STOREDPROC_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,STOREDPROC_NAME NVARCHAR(1024) NOT NULL
);

INSERT INTO dbo.TBL_STOREDPROC_001 (STOREDPROC_NAME)
VALUES
('STORED PROC 1 FROM TABLE 1')
,('STORED PROC 2 FROM TABLE 1')
,('STORED PROC 3 FROM TABLE 1');

INSERT INTO dbo.TBL_STOREDPROC_002 (STOREDPROC_NAME)
VALUES
('STORED PROC 1 FROM TABLE 2')
,('STORED PROC 2 FROM TABLE 2')
,('STORED PROC 3 FROM TABLE 2');




Simple code example
USE tempdb;
GO

DECLARE @TABLE_NAME NVARCHAR(1024) = N'dbo.TBL_STOREDPROC_002';

DECLARE @SQL_STR NVARCHAR(MAX) = REPLACE(N'

DECLARE @PROC_NAME NVARCHAR(MAX) = N'''';
DECLARE R_SET CURSOR FAST_FORWARD FOR
SELECT
N''EXEC '' + TP.STOREDPROC_NAME + NCHAR(59)
FROM {{@TABLE_NAME}} TP
ORDER BY TP.STOREDPROC_ID;
OPEN R_SET;
FETCH NEXT FROM R_SET INTO @PROC_NAME;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @PROC_NAME;
FETCH NEXT FROM R_SET INTO @PROC_NAME;
END

CLOSE R_SET;
DEALLOCATE R_SET;
',N'{{@TABLE_NAME}}',@TABLE_NAME);

EXEC (@SQL_STR);


Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16671 Visits: 17031
danielfountain (5/8/2014)
Hi all,

I am using a cursor (i know - but this is actually something that is a procedural loop).

So effectively i have a table of names of stored procedures. I now have a store proc that loops around these procs and runs each one in order.

Now i am thinking i would like to be able to set the table it loops around in a variable at the start - is it possible to do this? So effectively use a tablename in a variable to use in the sql to define a cursor?

Many thanks

Dan


You might be surprised. From your description I don't think you need a cursor. Unless there are parameters derived from another source you could do this using dynamic sql in a single step. It might be possible even if the parameters do come from elsewhere. Without some details around what you are doing though it is hard to say.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3956 Visits: 6692
You can use a GLOBAL cursor and use dynamic SQL just to declare the cursor. You can process it in static code, including deallocating the cursor at the end.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45442 Visits: 39943
Just remember that if you use a GLOBAL cursor, there could be a serious amount of contention between concurrent runs.

p.s. I love the tagline in your signature! :-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search