Link to home
Start Free TrialLog in
Avatar of LennyGray
LennyGrayFlag for United States of America

asked on

Executing a stored procedure while in a cursor loop

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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

EXEC @STRSQL  must be:EXEC (@STRSQL)
ASKER CERTIFIED SOLUTION
Avatar of mcv22
mcv22
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LennyGray

ASKER

Masterful solution, Scott!

Thanks!
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
The two of you created an elegant solution. Thanks, again!