Solved

SQL Table name is variable - Syntax?

Posted on 2006-11-02
3
346 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 69

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on 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.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now