Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everyone,

I have a table with name "test" and i want to get table entity itself in a variable so i can use it in join statement

Note:
i have table name in run-time and i want to get the table itself in a variable.

Thanks in Advance :)
Posted
Comments
Wendelius 29-Dec-12 16:12pm    
Could you explain more, what are you actually trying to do? Do you mean you want to dynamically construct an SQL statement or something else?

Thanks a lot :) , this working correctly
 
Share this answer
 
Comments
Wendelius 29-Dec-12 16:28pm    
You're welcome. You can use the "Have a Question or Comment?" button to send a comment directly to a specific solution. This way the person who wrote the solution gets notified, just like with this message :)
AhmedYehiaK 29-Dec-12 16:31pm    
Thanks :) the next time i will use it :)
Wendelius 29-Dec-12 16:34pm    
No problem at all :)
Ok, so if I understood your question correctly, then you could use dynamic sql, something like following:
SQL
declare @name nvarchar(50)
set @name = (select Name from Lookups where Lookups.ID = 4)
execute ('select * from ' + @name)

For more information, see EXECUTE[^]
 
Share this answer
 
v2
Comments
Espen Harlinn 30-Dec-12 8:10am    
5'ed!
Wendelius 4-Jan-13 15:12pm    
Thank you :D
Thanks firstly :)

SQL
declare @name nvarchar(50)
set @name = (select Name from Lookups where Lookups.ID = 4)


NOW [@name = MyTableName say "Items"]

then i want to do something like:

SQL
select * from @name


OR

using @name in any join statement

Thanks
 
Share this answer
 
This makes no sense at all. Your question is a SQL question. You can use the table in a join statement, there's no issue there. If you have an actual issue, it's not clear at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900