Solved

SQL Table name is variable - Syntax?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 41
How can i determine which dropdownlist caused the postback on Page Load event ? 1 40
Name space syntax error 12 44
Replace &lt; with < 14 56
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

15 Experts available now in Live!

Get 1:1 Help Now