Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting variable into SQL statement????

Status
Not open for further replies.

pilg

Programmer
Feb 16, 2001
82
GB
Hi,

I've captured a variable from another page but now I need to insert that variable into my SQL statement which looks like this:

"select subcategory from subcategory where table="MYVARIABLE"

how do I do this, help please.
 
try this:

Code:
"select subcategory from subcategory where table=" & MYVARIABLE & ";"
Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
SQL Strings are just like any other strings. You can do any of your normal string operations on them, including concatenation.

Code:
mySQLStr = "select subcategory from table where field='" & MYVARIABLE & "'"

If you print this out afterwards you will have(pretend the word value is the value of MYVARIABLE):
select subcatagory from table where field='value'


I corrected your syntax a little for the statement as well. When you do a select stmt like this you are generally selecting a field or fields from a table or tables where the values of certain fields meet certain criteria.


A simple example where you have a table named table1 with fields fieldname1 and fieldname2, in this example fieldname2 is a text field in your table.

simple: SELECT fieldname1 FROM table1 WHERE fieldname2 = 'somevalue'

A more complex example where you have two tables (table1, table2), table 1 has two fields (fieldname1,2) and table2 has 2 fields(fieldname3,4,5). We are looking for fieldname1 and 3. The two tables are related one to many (one record in table1 could match many records in table2) and that fieldname4 is the key from table1 (fieldname2). Pretend we know a value for fieldname5(text field).

complex: SELECT table1.fieldname1, table2.fieldname3 FROM table1, table2 WHERE table2.fieldname4=table1.fieldname2 AND fieldname5='ourvalue'

-Tarwn ------------ My Little Dictionary ---------
Reverse Engineering - The expensive solution to not paying for proper documentation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top