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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Table name as parameter of the function

Author  Topic 

piermorosini
Starting Member

1 Post

Posted - 2007-08-14 : 00:38:28
Hello,
I am trying to make a query like this :

SELECT field
FROM @tablename <---||
WHERE ...

In any way I have an error

[@tablename]
'@tablename'
"@tablename"

Sql server does not recognize the object name.

Thank you
Bye Pier

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2007-08-14 : 00:55:16

If you are using a variable then you can use EXEC method to execute statement dynamically.

For Example:

DECLARE @Table_name varchar(50)
SET @Table_name ='Emp'
--It should work
exec('SELECT * FROM ' + @Table_name)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-14 : 01:57:18
Why do you want to pass table name as parameter?

Madhivanan

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

- Advertisement -