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)
 Using a Parameter in the FROM clause

Author  Topic 

jtrirogoff
Starting Member

4 Posts

Posted - 2007-03-29 : 13:11:31
I was wondering if this is possible:

CREATE FUNCTION myFunction
(
@TableName varchar(255)
)
Returns TABLE
AS
BEGIN
SELECT ID, LastName, FirstName
FROM @TableName
RETURN
END

I want this function to select from the TableName that I pass in. Is this possible? I receive the following error when I try to save this:
"Must declare the variable '@TableName'"

- Jonny

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-29 : 13:14:37
No..neither it's not possible inside function, nor advisable.

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

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-29 : 13:21:11
You can do this with stored procedure + dynamic SQL.
BUT Why do you want to do this ?


KH

Go to Top of Page
   

- Advertisement -