SQL Table name is variable - Syntax?
Posted on 2006-11-02
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.