动态地将列添加到现有表

动态地将列添加到现有表

问题描述:

我有一个包含分号分隔列表的表格。 我想在该列中获取每个值,并在表中添加一个新列以容纳一个值。 这是DDL

I have a table that holds a semi-colon seperated list.  I want to take each value in that one column and add a new column to the table to house one value.  This is DDL

CREATE TABLE #tbl (Column1 VARCHAR(15),Column2 VARCHAR(15),Column3 VARCHAR(150));
INSERT INTO #tbl VALUES
 ('ABC','123','User7;User9')  
,('nbm','qre','User1;User2;User3')  
,('POI','kjh','User1;User4;User5;User9')
,('ASWD','kjh','User1;User4;User5;User9;User20;User100;User50;User300;User400;User800;')

如何动态地改变我的表#tbl的表结构,以便依赖于Column3中存在的值的数量是多少字段将被添加到表中并且值被解析为新列?

How can I DYNAMICALLY alter the table structure of my table #tbl so that depending on the number of values exist in Column3 that many fields will be added to the table and the values parsed out to the new columns?

什么是你的sql版本?我首先要找出列中最大数量的项目(;)然后将这些列数添加到表中。
What is your sql version? I would first figure out the biggest number of the items (;) in the column and then add that number of the columns to the table.