Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Table name is variable - Syntax?

Posted on 2006-11-02
3
Medium Priority
?
405 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 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