From: LUIS on
I know if I want to write a SELECT statement which TABLE could be "variable"
I should use "Dynamic Syntax"... but

Is there other way to do so? I mean, something in which TABLE in SELECT
works like a "@Table" variable.

Beforehand, thank you very much.

--
Luis Garcia
IT Consultant
From: ML on
Please, explain in more details what you want to achieve.

If you want to supply the table name dynamically then you could use dynamic
SQL, however, that's not the only way. But I think this is enough guessing
from me for now.


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
From: Adi on
On Jul 3, 10:21 am, LUIS <L...(a)discussions.microsoft.com> wrote:
> I know if I want to write a SELECT statement which TABLE could be "variable"
> I should use "Dynamic Syntax"... but
>
> Is there other way to do so? I mean, something in which TABLE in SELECT
> works like a "@Table" variable.
>
> Beforehand, thank you very much.
>
> --
> Luis Garcia
> IT Consultant

You can only do it as dynamic SQL (which can cause security problems)
or use if statements that check the value of @table and then the
table's name in a static way.

Adi
From: Erland Sommarskog on
LUIS (LUIS(a)discussions.microsoft.com) writes:
> I know if I want to write a SELECT statement which TABLE could be
> "variable" I should use "Dynamic Syntax"... but
>
> Is there other way to do so? I mean, something in which TABLE in SELECT
> works like a "@Table" variable.

DECLARE @table TABLE (a int NOT NULL)
INSERT @table (a) VALUES(12)
SELECT a FROM @table

Oh, so you wanted @table to hold a table name? Since each table should model
a different entity, that is not a particularly meaningful operation in a
well-designed database.

This article explains more of how you could do it - and why most probably
shouldn't: http://www.sommarskog.se/dynamic_sql.html.

--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
From: siva.k on
declare @tabs varchar(50)
set @tabs = 'table_name'
execute('select * from ' + @tabs)