Home Forums SQL Server 2008 T-SQL (SS2K8) Need to pull the data out of a row and into a comma separated string referencing the columnID RE: Need to pull the data out of a row and into a comma separated string referencing the columnID

  • I saw that, but it does not work for my application.

    I need to create a adaptive insert statements transferring data in one table to a table on a different server. SSIS is not allowed.

    In order to do this, I need to access the data in the table via the column ID of the columns.

    This is how I get the column Names

    declare @count INT, @columncount int, @dataOut varchar(8000), @tablename varchar(200)

    select @tablename = 'Patient'

    select @columncount = COUNT(*) from Sys.columns where OBJECT_ID = OBJECT_ID(@TableName)

    select @count = 1

    select @dataOut = ''

    while @count < @columncount + 1

    Begin

    select @dataout = @dataout + (Select NAME from sys.columns WHERE OBJECT_ID = OBJECT_ID(@TableName) and column_id = @count)+','

    Select @count = @count +1

    Continue

    End

    select @dataOut

    Now I need to pair the actual data from the row to the columns

    This is for a adpatively generated Insert statement