Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: Numeric variables in SQL WHERE clause in ASP


Message #1 by "Jim Andrews" <jim@t...> on Wed, 20 Jun 2001 22:58:07
With your help, I was able to get the WHERE clause working for selecting 

records where a field was equal to a text variable. But now I need to 

select records where a field is equal to a numeric variable from another 

database. Logically, here's what I want:

I = dbname1.fields(2) 'which is a long integer

SELECT * FROM dbname2 WHERE field_name = I

I get "not enough parameters," so I guess the variable name "I" needs some 

kind of help in being interpreted. Can you help?

By the way, I also need to select on a date field, so I'll probably need 

help with that, too.
Message #2 by "Ken Schaefer" <ken@a...> on Thu, 21 Jun 2001 10:42:04 +1000
Jim



I don't really understand what you're asking. If you are query a field that

is numeric, then the following will work:



strInt = 5



strSQL = _

    "SELECT * " & _

    "FROM table1 " & _

    "WHERE SomeNumericField = " & strInt



Response.Write(strSQL)





If you need to query a date field you need to put # around it (for Access),

or ' around it (for SQL Server).

You might want to also look at this page:

http://www.adopenstatic.com/faq/80040e10.asp

I suggest you Response.Write(strSQL) so you can see exactly what you are

trying to send to the database.



Cheers

Ken



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

----- Original Message -----

From: "Jim Andrews" <jim@t...>

To: "ASP Databases" <asp_databases@p...>

Sent: Wednesday, June 20, 2001 10:58 PM

Subject: [asp_databases] Numeric variables in SQL WHERE clause in ASP





: With your help, I was able to get the WHERE clause working for selecting

: records where a field was equal to a text variable. But now I need to

: select records where a field is equal to a numeric variable from another

: database. Logically, here's what I want:

: I = dbname1.fields(2) 'which is a long integer

: SELECT * FROM dbname2 WHERE field_name = I

: I get "not enough parameters," so I guess the variable name "I" needs some

: kind of help in being interpreted. Can you help?

: By the way, I also need to select on a date field, so I'll probably need

: help with that, too.





Message #3 by Gee Vee <happygv@y...> on Wed, 20 Jun 2001 21:51:28 -0700 (PDT)
Hi,



Pls use the following if you are trying to query SQL

database from an ASP page.



I = dbname1.fields(2) 'which is a long integer



conn.execute("SELECT * FROM dbname2 WHERE field_name="

& I)



pls let me know if it worked.



Regards

Vijay.G



Message #4 by "Jim Andrews" <jim@t...> on Fri, 22 Jun 2001 16:47:46
I would not have believed that it could be that simple! I guess it's the 

old "thinking outside the box" problem. I always assumed that all the 

parameters for the WHERE clause had to be within the quote marks. Yet it 

is not so. Thanks for the help...it worked perfectly. 

What amazes me is that my 800+ page "Beginning ASP Databases" book never 

mentions the use of variables in a WHERE clause.



> Jim

> 

> I don't really understand what you're asking. If you are query a field 

that

> is numeric, then the following will work:

> 

> strInt = 5

> 

> strSQL = _

>     "SELECT * " & _

>     "FROM table1 " & _

>     "WHERE SomeNumericField = " & strInt

> 

> Response.Write(strSQL)

> 

> 

> If you need to query a date field you need to put # around it (for 

Access),

> or ' around it (for SQL Server).

> You might want to also look at this page:

> http://www.adopenstatic.com/faq/80040e10.asp

> I suggest you Response.Write(strSQL) so you can see exactly what you are

> trying to send to the database.

> 

> Cheers

> Ken

> 

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> ----- Original Message -----

> From: "Jim Andrews" <jim@t...>

> To: "ASP Databases" <asp_databases@p...>

> Sent: Wednesday, June 20, 2001 10:58 PM

> Subject: [asp_databases] Numeric variables in SQL WHERE clause in ASP

> 

> 

> : With your help, I was able to get the WHERE clause working for 

selecting

> : records where a field was equal to a text variable. But now I need to

> : select records where a field is equal to a numeric variable from 

another

> : database. Logically, here's what I want:

> : I = dbname1.fields(2) 'which is a long integer

> : SELECT * FROM dbname2 WHERE field_name = I

> : I get "not enough parameters," so I guess the variable name "I" needs 

some

> : kind of help in being interpreted. Can you help?

> : By the way, I also need to select on a date field, so I'll probably 

need

> : help with that, too.

> 

> 


  Return to Index