SQL Server
  Home arrow SQL Server arrow Page 2 - Execute SQL on Multiple Tables/Columns - N...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SQL SERVER

Execute SQL on Multiple Tables/Columns - New Feature in SQL Server 2000
By: Raghavendra Narayana
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2003-09-25

    Table of Contents:
  • Execute SQL on Multiple Tables/Columns - New Feature in SQL Server 2000
  • Script
  • Parameters
  • Examples
  • Expansion
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Execute SQL on Multiple Tables/Columns - New Feature in SQL Server 2000 - Script


    (Page 2 of 6 )

    This script creates objects under “DBO” owner.  If you want it under a different owner, you may change it accordingly.

    CREATE PROCEDURE SP_execSQLonDB
     (@TABLENAME VARCHAR(50),
      @COLUMNNAME VARCHAR(50),
      @SQL NVARCHAR(4000),
      @INCLUDE_NTI CHAR(1) = 'N')
    AS
    BEGIN
     --Variable Declaration
     DECLARE @strSQL NVARCHAR(4000)
     DECLARE @SQL2 NVARCHAR(4000)
     DECLARE @sTableName VARCHAR(200)
     DECLARE @sColumnName VARCHAR(200)

     --Check whether to include TEXT, NTEXT, IMAGE data types
     SET @INCLUDE_NTI = UPPER(LTRIM(RTRIM(@INCLUDE_NTI)))
     IF @INCLUDE_NTI NOT IN ('N', 'Y')
      SET @INCLUDE_NTI = 'N'

     --Construct a cursor to get the list of Table/Column Names according to the @TABLENAME and @COLUMNNAME parameters.
     SET @strSQL = N'DECLARE TabColCursor CURSOR FOR SELECT RTRIM(LTRIM(SU.NAME)) + ''.'' + LTRIM(RTRIM(SO.NAME)), SC.NAME FROM SYSOBJECTS SO INNER JOIN SYSCOLUMNS SC ON SO.ID = SC.ID INNER JOIN SYSUSERS SU ON SO.UID = SU.UID WHERE SO.XTYPE = ''U'' '

     --Filter out Text/NText/Image data types if it is not included
     IF @INCLUDE_NTI = 'N'
      --In SysColumns sytem table XTYPE column corresponds to Column Data Type
      SET @strSQL = @strSQL + ' AND SC.XTYPE NOT IN (35, 99, 34) '

     --Add the TABLE(S) name i.e. filter if it is supplied
     IF @TABLENAME IS NOT NULL AND ltrim(rtrim(@TABLENAME)) <> ''
     BEGIN
      SET @TABLENAME = REPLACE(@TABLENAME, ', ', ',')
      SET @strSQL = @strSQL + ' AND (SO.NAME LIKE ''' + REPLACE(@TABLENAME, ',', ''' OR SO.NAME LIKE ''') + ''')'
     END

     --Add the COLUMN(S) name i.e. filter if it is supplied
     IF @COLUMNNAME IS NOT NULL AND ltrim(rtrim(@COLUMNNAME)) <> ''
     BEGIN
      SET @COLUMNNAME = REPLACE(@COLUMNNAME, ', ', ',')
      SET @strSQL = @strSQL + ' AND (SC.NAME LIKE ''' + REPLACE(@COLUMNNAME, ',', ''' OR SC.NAME LIKE ''') + ''')'
     END

        --Execute the constructed "Cursor Declaration" string
     EXECUTE sp_executesql @strSQL
     IF @@ERROR > 0
     BEGIN
      PRINT 'Error while declaring the Cursor.  Please check out the parameters supplied to the Procedure'
      RETURN -1
     END 

     --Database Transaction.
     BEGIN TRANSACTION gDatabaseTrans

     --Open the cursor
     OPEN TabColCursor     

     --Fetch the Table, Column names to variables
     FETCH NEXT FROM TabColCursor
     INTO @sTableName, @sColumnName

     --Execute the SQL statement supplied in @SQL parameter on every row of Cursor's data
     WHILE @@FETCH_STATUS = 0
     BEGIN
      --Construct SQL2 to Execute supplied @SQL
      --by replacing @TABLENAME, @COLUMNNAME with running Table Name, Column Name of Cursor's data
      SET @SQL2 = @SQL
      SET @SQL2 = REPLACE(@SQL2,
    '@TABLENAME', @sTableName)
      SET @SQL2 = REPLACE(@SQL2,
    '@COLUMNNAME', @sColumnName)

      --Execute the constructed SQL2
      EXECUTE sp_executesql @SQL2


      --Check for errors
      IF @@ERROR <> 0
      BEGIN
       --On Error, Destroy objects, Rollback transaction
       --Return -1 as UNSUCCESSFUL flag
       PRINT 'Error occurred'
       DEALLOCATE tabcolcursor
       ROLLBACK TRANSACTION gDatabaseTrans
       RETURN -1
      END

      --Process Next Row of Cursor
      FETCH NEXT FROM TabColCursor
      INTO @sTableName,@sColumnName
     END

     --Destroy CURSOR object
     DEALLOCATE tabcolcursor

     --Procedure executed properly. Commit the Transaction.
     --Return 0 as SUCCESSFUL flag
     COMMIT TRANSACTION gDatabaseTrans
     RETURN 0
    END

    More SQL Server Articles
    More By Raghavendra Narayana


     

    SQL SERVER ARTICLES

    - Executing SQL Server Stored Procedure from P...
    - How to Search for Date and Time Values Using...
    - Replication: SQL Server 2000 - Part 2
    - Replication: SQL Server 2000 - Part 1
    - SQL Sever: Storing Code in Binary or Text Fi...
    - Execute SQL on Multiple Tables/Columns - New...
    - How to Connect to a SQL Server from Visual F...
    - SQL Server Hardware Tuning and Performance M...
    - Primary Key on Multiple Tables – New RDBMS C...
    - Migrating from Sybase to SQL Server
    - What's Best for DBAs? GUI or T-SQL Comma...
    - How to Perform a SQL Server Performance Audit
    - An Introduction To The Bulk Copy Utility
    - SQL Server Stored Procedures 101
    - Building Your First SQL Server 2000 Database







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek