Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 can you create a table in a SP using a variable?

Author  Topic 

pizzazzz
Yak Posting Veteran

55 Posts

Posted - 2010-07-27 : 19:51:39
I am trying to create a table in my stored procedure based on a value from a CURSOR -

when I query user info i want to create and populate a table and use users lastname as a variable to create the name of the table.

my CURSOR has 2 distinct fields it returns - @email_address and @lastname.
i want to create a table = to @lastname.
then i use the @email_address in my where clause to create a dataset of all records and fields of a user and INSERT into the table I just created, named @lastname.

HOWEVER, it is complaining about using @lastname in the CREATE TABLE.

isn't this possible???

any suggestions I can get would be extremely helpful.

thanks,

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-27 : 19:53:32
You have to use dynamic SQL in order to use a dynamic name. Read up on dynamic SQL: http://www.sommarskog.se/dynamic_sql.html

But why are you creating objects like this? Why not instead properly normalize it?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

pizzazzz
Yak Posting Veteran

55 Posts

Posted - 2010-07-28 : 12:08:11
i was trying to get away from normalizing, but that too is a simplier way.

I'll read the dynamic name process. If anything this bit of knowledge will come in handy in other projects.

thank you very much Tara, the "Almightly SQL Goddess"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-28 : 12:45:48
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -