all groups > dotnet academic > april 2004 >
You're in the

dotnet academic

group:

TABLE NAME AS A VARIABLE IN SQL



TABLE NAME AS A VARIABLE IN SQL Gina L. Hernandez
4/29/2004 3:28:27 PM
dotnet academic: Could somebody tell me what is the syntax for reading the name of a tabla
from a variable??. For example



declare @tabla varchar(10)

set @tabla = 'prompts'

select *
from @tabla

Thanks


Re: TABLE NAME AS A VARIABLE IN SQL Giacomo
4/29/2004 8:24:23 PM
--SELECT name from sysobjects where name=@tabla

Can you explain your situation a bit more? I'm not sure I understand your
question.

[quoted text, click to view]

Re: TABLE NAME AS A VARIABLE IN SQL Peter van der Goes
5/1/2004 9:55:01 AM

[quoted text, click to view]
I believe I know what the OP wants to do, and I believe it can be done in
ADO.NET.
She wants to create a parameterized SELECT query where the table name for
the query can be supplied as a parameter.
Write a general query.
SELECT * FROM "tablename"
where "tablename" is supplied as an argument just before the query runs.

As SQL commands are set up as strings in ADO.NET, then attached as
properties of data adapter objects (SQLDataAdapter, etc.). It would be
possible to prompt the user for the desired table name (probably from a
list), then incorporate the selection in the cmdSelect.CommandText property.
Sort of like this:
mconCurrent.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=..\..\..\Data\Factory.mdb"

Dim dtmtblFactory As DataTableMapping

dtmtblFactory = modbdaCurrent.TableMappings.Add("Table", "tblFactory")

' Build the SelectCommand to select all of the records from the table

' named tblFactory.

modbcmdSelect.CommandText = "SELECT * FROM tblFactory "

modbcmdSelect.Connection = mconCurrent

modbdaCurrent.SelectCommand = modbcmdSelect

Prompt the user (or obtain from another source) for the String containing
the desired table and substitute for the literals in the code.

Caveat: I have not tried this, so it should be regarded as an experiment.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

AddThis Social Bookmark Button