[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Executing a stored procedure while in a cursor loop

Posted on 2010-08-20
7
Medium Priority
?
712 Views
Last Modified: 2012-05-10
I created a cursor to store script so that I could loop through all of the databases and list all of their tables. The looping script creation worked but I hit the 8,000 character limit barrier in SqlServer.

So rather than accumulate the entire script, I decided to execute a portion of the script while it was being generated. It did not work

Yet when I copy and paste the generated script and run it in a query panel, it works like a charm.

Attached is the code
CREATE TABLE #TableStructures(
	  [ApplicationCode] [varchar](50) NULL,
	  [RootFile] [varchar](255) NULL,
	  [DestinationTable] [varchar](255) NULL,
	  [Column Name] [varchar](128) NULL    
    )
    
    DECLARE @DBName VARCHAR(50)
    DECLARE @STRSQL VARCHAR(8000) 
    SET @strSQL =''
    DECLARE curDBName CURSOR  FOR SELECT name FROM sys.databases WHERE name NOT IN('CommonPrograms','ExtractorControl','master','model','msdb','tempdb')

    OPEN curDBName
    FETCH NEXT FROM curDBName INTO @DBName
    WHILE @@FETCH_STATUS=0
      BEGIN
	      SET @strSQL = @strSQL + 
        'INSERT INTO #TableStructures (ApplicationCode, RootFile, DestinationTable, [Column Name])
         SELECT ExtractorControl..FilesToProcess.ApplicationCode, ExtractorControl..FilesToProcess.RootFile, ExtractorControl..FilesToProcess.DestinationTable, '+ @DBName +'..syscolumns.name AS ''Column Name'' 
         FROM (ExtractorControl..FilesToProcess INNER JOIN '+ @DBName +'..sysobjects ON ExtractorControl..FilesToProcess.DestinationTable = '+ @DBName +'..sysobjects.name) INNER JOIN '+ @DBName +'..syscolumns ON '+ @DBName +'..sysobjects.id = '+ @DBName +'..syscolumns.id
         GROUP BY ExtractorControl..FilesToProcess.ApplicationCode, ExtractorControl..FilesToProcess.RootFile, ExtractorControl..FilesToProcess.DestinationTable, '+ @DBName +'..syscolumns.name, '+ @DBName +'..sysobjects.xtype
         HAVING ('+ @DBName +'..sysobjects.xtype)= ''U''' + CHAR(10)
        EXEC @STRSQL  
        PRINT @STRSQL    
        SET @strSQL =''    
        FETCH curDBName INTO @DBName
      END

    CLOSE curDBName
    DEALLOCATE curDBName   
    
    SELECT * FROM #TableStructures
    DROP TABLE #TableStructures

Open in new window

0
Comment
Question by:LennyGray
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33488669
EXEC @STRSQL  must be:EXEC (@STRSQL)
0
 
LVL 12

Accepted Solution

by:
mcv22 earned 1000 total points
ID: 33488696
Why not try something like this:


EXEC sp_msforeachdb
'
	USE [?]
	IF (''?'' NOT IN (''CommonPrograms'',''ExtractorControl'',''master'',''model'',''msdb'',''tempdb'')
	BEGIN
		INSERT #TableStructures (..)
		SELECT ...
	END
'

Open in new window

0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 33488817
More specifically (and hopefully more helpfully :-) ):

EXEC sp_MSforeachdb '
IF ''?'' IN (''CommonPrograms'',''ExtractorControl'',''master'',''model'',''msdb'',''tempdb'')
    RETURN
INSERT INTO #TableStructures (ApplicationCode, RootFile, DestinationTable, [Column Name])
SELECT ftp.ApplicationCode, ftp.RootFile, ftp.DestinationTable, sc.name AS ''Column Name''
FROM ExtractorControl..FilesToProcess ftp
INNER JOIN [?]..sysobjects so ON ftp.DestinationTable = so.name
INNER JOIN [?]..syscolumns sc ON so.id = sc.id
WHERE so.xtype = ''U''
GROUP BY ftp.ApplicationCode, ftp.RootFile, ftp.DestinationTable, sc.name
ORDER BY ftp.ApplicationCode, ftp.RootFile, ftp.DestinationTable, sc.name
'

Table aliases -- such as ftp, sc and so -- make code much easier to write, read and maintain.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Author Closing Comment

by:LennyGray
ID: 33500381
Masterful solution, Scott!

Thanks!
0
 
LVL 10

Author Comment

by:LennyGray
ID: 33500512
MCV22 - I apologize for awarding you the points in error.

ScottPletcher - I also apologize to you.

I have contacted EE for assistance in order to correct this error. You both are valuable assets to EE and to me. Thank you for the unselfish sharing of your time and experience.

It is a pleasure to know that the world has people like you and that you are generous-enough to share with people like me.

You are making a difference!

Lenny Gray
0
 
LVL 10

Author Closing Comment

by:LennyGray
ID: 33500627
The two of you created an elegant solution. Thanks, again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

872 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