Solved

SQL Table name is variable - Syntax?

Posted on 2006-11-02
3
370 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
3 Comments
 
LVL 142

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 250 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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