Syntax for FOR LOOP

  • Dear All,

    I'm having one config table. In that table one field is called TableNames. Here i'm storing some Table Names.

    Ex: Config_Table

    ---------------------

    ConfigId TableName

    1 Table1

    2 Table2

    3 Table3.

    -----------------------

    All the tables having same fields(table1,2,3). Now in my Application (Asp.net C#) i want to update one particular field from all the tables which i stored in config table.

    I Hope u all understand this issue. Please provide me the coding for that.

    Thankx

    Jones

    jonesugu@gmail.com

  • There is no for loop in SQL. There's only the While loop.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • This how you get the tables with identical columns into a temporary table:

    /****** Object: Table [dbo].[Table_1] Script Date: 06/10/2009 11:58:22 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[Table_1](

    [F1] [nchar](10) NULL,

    [F2] [int] NULL

    ) ON [PRIMARY]

    GO

    /****** Object: Table [dbo].[Table_2] Script Date: 06/10/2009 11:58:40 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[Table_2](

    [F1] [nchar](10) NULL,

    [F2] [int] NULL

    ) ON [PRIMARY]

    GO

    --@@@@@@@@@@@@@@@@@@@@@

    SELECT *

    INTO #tmpTable

    FROM

    (

    select --t1.object_id, t1.name AS columnname,

    t2.name as Tablename from sys.columns t1

    inner join sys.objects t2

    on t1.object_id = t2.object_id

    where t1.name like 'F1'

    ) AS Tables

    SELECT * FROM #tmpTable

    DROP TABLE #tmpTable

    Then use this table to loop through, or if you feel really good, try & find a SET-based solution to update.

    Mind, I pass by all kinds of pitfalls like: are identical column names REALLY the same? Do they REALLY all need to be updated? Do they have identical datatypes?

    Greetz,
    Hans Brouwer

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply