• Hi RKS,

    quote:


    I have a table that has fields 'ColPosition'(int) and 'ColName'(char200) and a 'Jobid'(int). I will not know how many 'ColPosition' there are. From this I would like to create a table with as many fields as there are ColPositions for a particular Jobid, then parse the ColName and insert it into the ColPosition field. My hang up is in creating a table with an unknown amount of fields. Here is what I currently have for code. Any help would be appreciated. Thanks!

    create proc Weblayout

    @jobid int

    as

    declare @colnames as varchar(1000)

    declare @firstloop as int

    declare @secondloop as int

    declare @splitstring as varchar(200)

    declare @counter as int

    drop table Webtable

    create table Webtable

    (

    colname VARCHAR(1000)

    )

    set @counter = 1

    set @firstloop = 'select max(ColPosition) from frtabledef where jobid ='& @jobid

    while @firstloop > 0

    begin

    set @splitstring = 'select colname from frtabledef where jobid ='& @jobid & ' and colposition ='& @counter

    --select dbo.ufn_parsestring('Recall 1 | Total Completes | Rotation A 431 1st/825 2nd'

    insert into Webtable(colname) values (dbo.ufn_parsestring(@splitstring,'|',@counter))

    set @counter = @counter + 1

    end

    select * from WebTable


    maybe I am missing something, but webtable has only one field!

    What about making your CREATE TABLE statement dynamic like this?

    SET @stmt = N'CREATE TABLE ' + @tname +

    ..and then loop through your string?

    EXEC sp_executeSQL @stmt

    Is this going your direction?

    Cheers,

    Frank

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]