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
 General SQL Server Forums
 New to SQL Server Programming
 Selecting Variable as Table

Author  Topic 

ero-sennin26
Starting Member

7 Posts

Posted - 2007-07-05 : 11:37:18
Good Day guys, sorry i'm just new in SQL Server.

My problem is like this:

I want to pass Table Object to a declared variable and make View.

Usually we make like this:

"Select * from Table1"

I want like this:

"Select * from @Table1"

I want to pass Table1 to a variable "@Table1" which the variable
reads as Table.

Please help me guys, i really appriciated everything.

Thanks,

Ero-Sennin26

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-07-05 : 11:39:11
[code]Declare @sql varchar(8000)
Set @sql = 'Select * from ' + @Table1
Exec(@sql)[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

ero-sennin26
Starting Member

7 Posts

Posted - 2007-07-05 : 11:52:06
Mr. Harsh Athalye,

It works, thank you very much.
I hope i'll be professional like you someday.
Thanks again.

Regards,
Ero-Sennin26
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-05 : 11:55:53
Also make sure you read this fully
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-07-05 : 11:57:32
Madhi,

I bet myself that you are going to post that link!

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-05 : 12:06:50
quote:
Originally posted by harsh_athalye

Madhi,

I bet myself that you are going to post that link!

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"


I didnt want to disappoint you

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -