?
Solved

SQL Table name is variable - Syntax?

Posted on 2006-11-02
3
Medium Priority
?
397 Views
Last Modified: 2008-02-26
I need to query SQL DB using table name that is variable retrieved from code.
The variable is String.

Dim strInvestor As String = Trim(txtINV_CODE.Text) // This is table name in SQL DB
Dim strProd_Code As String = Trim(txtProd_Code.Text)

Dim strInvSQL As String = "SELECT TERM, CODE FROM" + strInvestor + " WHERE code = '" & strProd_Code & "'"

strInvestor table can be : GAP, GOAL, GMAC, etc

I tried above statement and it returns an error: Invalid column name TERM.
TERM exists in all possible "strInvestor" tables, so I am not sure what the problem is - I assume my syntax

It does not return error like table does not exists, so I am assuming it is finding right table based on above syntax. Maybe I am wrong. Maybe it assumes some wierd table, but prompts error that table field is non exisiting.

What is right syntax in VB.NET so my SQL query will work?
P.S. I could probably use stored procedure, but would like to try direclty from VB.NET code.

Thanks
0
Comment
Question by:Terrace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17859946

Dim strInvSQL As String = "SELECT [TERM], [CODE] FROM " + strInvestor + " WHERE [code] = '" & strProd_Code & "'"
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 17859969
Hi Terrace,

I think you only need a space after the FROM keyword:
Dim strInvSQL As String = "SELECT TERM, CODE FROM " + strInvestor + " WHERE code = '" & strProd_Code & "'"


Cheers!
0
 

Author Comment

by:Terrace
ID: 17860045
Good catch emoreau.

angelIII suggestion would probably work too (since it has that required space after FROM) - maybe I should have split points?
angelIII I am sure you will answer some of my questions soon, and will award you extra points then.

Thank you both for fast response.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question