4 Replies - 5513 Views - Last Post: 14 January 2013 - 06:30 AM

#1 AhmedYehia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 8
  • Joined: 25-September 11

Get Table as a Variable

Posted 29 December 2012 - 02:09 PM

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 :)/>


Is This A Good Question/Topic? 0
  • +

Replies To: Get Table as a Variable

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,318
  • Joined: 18-April 07

Re: Get Table as a Variable

Posted 02 January 2013 - 03:00 PM

I am not sure what you mean by placing it in a "variable". You can assign table columns/names as aliases. Then the alias can be used throughout the rest of the query to shorten joins. Aliases are good for tables which you want to link to themselves (unary relationships) but treat as if they were two separate tables.

select sometable.ProductID, sometable.VendorID
from sometable.ProductVendor pv1 //<-- Notice alias
inner join sometable.ProductVendor pv2 // Notice other alias (both to same table column)
on pv1.ProductID = pv2.ProductID // Can use aliases in joins etc
and pv1.VendorID <> pv2.VendorID



I think this is what you might mean when you are saying variable. :)
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: Get Table as a Variable

Posted 13 January 2013 - 10:12 PM

That is what Entity Framework does. It creates a strongly typed Class that is your table and properties that are your fields. It encompasses all constraints and rules that are applied in the DB. You can use Stored Procedures and Views as well. So, your DB looks and feels like variables. Excellent for LINQ and lambda expressions. Better binding with ASPX imo!
Was This Post Helpful? 0
  • +
  • -

#4 AhmedYehia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 8
  • Joined: 25-September 11

Re: Get Table as a Variable

Posted 14 January 2013 - 05:58 AM

Thanks for all :smile2:

assume that i have the following snippet:

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

Thanks all for your time :smile:

if found the solution:
declare @name nvarchar(50)
set @name = (select Name from Lookups where Lookups.ID = 4)
execute ('select * from ' + @name)



Read about EXECUTE
Was This Post Helpful? 0
  • +
  • -

#5 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 498
  • View blog
  • Posts: 3,317
  • Joined: 12-January 10

Re: Get Table as a Variable

Posted 14 January 2013 - 06:30 AM

y not just join the tables instead of doing it like that?


select a.column1, a.column1
from table b
inner join table a on table b.something = table a.something



working example

select employee_detail.address, employee_detail.state
from employee
inner join employee_detail on employee_detail.employee_id = employee.employee_id

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1