Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: Variable to reference Table name


Message #1 by "Eddie Villarreal" <evillarreal@d...> on Fri, 23 Mar 2001 14:33:29
I would also be interested in a more simple method. But for now, your work 
around example was helpful, thanks john
eddie
 
> This is a common problem.  You can dynamically build a sql statement, 
but you can't
> get a value back from it.  So, usually i'll just put that value into a 
temp table
> and read from it immediately afterwards:
> 
> CREATE PROCEDURE sp_GetStuff @tblName VarChar(100)
> AS
> Begin
> 
> declare @sql varchar(200)
> declare @retval varchar(20)
> 
> set @sql = 'SELECT Field1 INTO #myTempTable FROM ' + @tblName + ' WHERE 
Field2 = 4'
>  
> EXEC(@sql)
> 
> SELECT @retval = Field1
> FROM #myTempTable
> 
> End --stored proc
> 
> hope this helps,
> 
> john
> 
> PS if you find another way to do this, i'd be interested in seeing it.
> 
> 
> --- Eddie Villarreal <evillarreal@d...> wrote:
> > Any ideas?
> > I would like to pass a parameter called @tblName to a function where 
the 
> > parameter references a table name and can be used as follows.
> > 
> > SET @Value = (SELECT field1 From @tblname WHERE field2 = 1)
> > 
> > Passing the table name as a varchar type results in the following 
error.
> > "Must declare the variable '@t...'"
> > 
> > Thanks, Eddie
> 
> John Pirkey 
> MCSD 
> John@S... 
> http://www.stlvbug.org

  Return to Index