Scripting column names dynamically

  • I'm writting a query that get column name for selected tables.

    SELECT

    (SELECT c.column_name FROM information_schema.tables T

    JOIN information_schema.columns C

    ON t.table_name = c.table_name

    WHERE t.table_type = 'base table' and t.table_name like 'L_%' )

    INTO #TempTable FROM TableA A LEFT JOIN [Server-Name].DBName.dbo.TableB B ON A.ID = B.ID

    How do I seperate the column name in my query so that they can have ',':D

  • DECLARE @Line VARCHAR(1000)

    SET @Line = ''

    SELECT @Line = @Line + c.column_name + ' , ' FROM information_schema.tables T

    JOIN information_schema.columns C

    ON t.table_name = c.table_name

    WHERE t.table_type = 'base table' and t.table_name like 'L_%'

    SELECT SUBSTRING(@Line, 1, LEN(@Line) -1)

    It will give you a string of columns in the form of 'col1, col2, col3'

  • Thankx Loner. Its worked perfectly fine.

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

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