Click here to Skip to main content
11,481,404 members (59,416 online)
Rate this: bad
good
Please Sign up or sign in to 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 Smile | :)
Posted 29-Dec-12 11:05am
Comments
Mika Wendelius at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Ok, so if I understood your question correctly, then you could use dynamic sql, something like following:
declare @name nvarchar(50)
set @name = (select Name from Lookups where Lookups.ID = 4)
execute ('select * from ' + @name)
For more information, see EXECUTE[^]
  Permalink  
v2
Comments
Espen Harlinn at 30-Dec-12 8:10am
   
5'ed!
Mika Wendelius at 4-Jan-13 15:12pm
   
Thank you :D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks firstly Smile | :)

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:

select * from @name

OR

using @name in any join statement

Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanks a lot Smile | :) , this working correctly
  Permalink  
Comments
Mika Wendelius at 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 at 29-Dec-12 16:31pm
   
Thanks :) the next time i will use it :)
Mika Wendelius at 29-Dec-12 16:34pm
   
No problem at all :)

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



Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100