Sql-server – Create table from stored procedure result

sql server

I am new to this field if you can please tell me if it is possible to do this it will be very help full. I have SQL Server 2008 R2.

What I want to do is from my table I want to take first row and try to use it as a column name to create another table in a stored procedure. The reason why I want to use those particular column name is which will come in as data in first row is, it will keep changing every week since those are dates.

So my plan is if it is possible to extract that first line from data which is ([Employee Name], [10/18/2013], [10/19/2013]..) and create another table which will use that extracted data as "column name" and create another SP will fill data as required. But I have no clue if SQL will create table and use "column name" which extracted from another data.


Further information in response to the answer:

What I am trying to do is. I have data coming in as time sheet every week. my plan is once I will get time sheet in every week as excel I will some how import into SQL as Import/export. the only problem is that Time Sheet have columns start from [Last Name], [First Name], [10/21/2013],[10/22/2013],[10/23/2013],…,[10/24/2013]. and I need those date columns in my data so I can use those in my later store procedure. in order for it do, I have included those columns in my data and not in headers when I import, so I can use those data. after doing that i have wrote one simple store procedures, which only read first row which will be first name, last name, 10/21/2013, 10/22/2013 and so on to extract header from the data. and from their i want to execute a store procedure to create new table every week(since date will be change every week) and will have first name, last name, 10/21/2013, hours of work, Taxonomy( will have 3 characters from fist name, last name, date and proj name) in different table. once the time push for that week at the end i will delete that table.

I apologize if that sounds confusing. Do you think I am going on right direction. if you know any links that explain this type of case, it will be so helpful since i am new to this field.

Best Answer

If you're really committed to this type of activity then there is a lot of dynamic sql in your future. Everything you describe is possible. But tread very lightly as dynamic sql opens some doors that you may wish later on had stayed closed.

One of the most classic articles in the history of dynamic sql can be found here. It's called the blessing and curse of dynamic sql. You give that a thorough review and you should understand your options better.

http://www.sommarskog.se/dynamic_sql.html

This specific section is very pertinent to your question.

http://www.sommarskog.se/dynamic_sql.html#Cre_tbl

Note that the author identifies a key point. If you're doing this your violating some fundamentals of relational database design. Now, I'm all for breaking the rules, as long as you understand the tradeoff and the purpose of the rules being broken. But, the question is valid. What are you really trying to accomplish?