Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2004, 10:53 AM
Authorized User
 
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Raif
Default Parameter for tablename?

Hi, I have datareader useing this objectcommand and sqlstring. When I hard code the name of the table it works with the param "@puser" but when I use as below I get an error that says I must declare the variable @ptable.
Can you not use a parameter for the table name?

 Dim strSQL As String = "SELECT email From @ptable Where fullname = @pUser "
        Dim objCMD As New SqlCommand(strSQL, objConn)
        With objCMD.Parameters
            .Add(New SqlParameter("@ptable", strTable))
            .Add(New SqlParameter("@puser", strUser))
        End With

Thanks for the help
Raif

 
Old August 6th, 2004, 11:15 AM
Authorized User
 
Join Date: Apr 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It may depend on which database you are using in the back end, but as a general rule, no, you cannot pass table names as variables.

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
 
Old August 7th, 2004, 02:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Raif,

When is that you decide the table name? If you have the table name decided before you construct this select statement, you can probably use this way, which is done in the frontend, well before it hits the DB for executing the query.

suppose strTblName contains the table name that you wanted to query.

Dim strSQL As String = "SELECT email From " & strTblName & " Where fullname = @pUser"

You cannot pass table name as parameter with the kind of approach you have here. Not sure if you are using SQL server/any other DB. If SQL server, then you can create stored proc that constructs the sql string and use exec() function to run that.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO - SELECT FROM [Tablename] WHERE [MyDate] < ... Fabietto Excel VBA 1 February 23rd, 2007 01:31 PM
Selecting records from a dynamic tablename flyin SQL Server 2000 3 September 30th, 2004 08:05 AM
CR8 - change location of database file / tablename meenakshikalera Crystal Reports 0 June 23rd, 2004 11:12 AM
Tablename argument chloe Access VBA 7 March 24th, 2004 12:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.