Forum Moderators: open

Message Too Old, No Replies

ASP within an SQL string

         

webboy1

4:01 pm on Nov 12, 2002 (gmt 0)

10+ Year Member



Can someone show me how to put ASP within SQL?

I think i almost have it. I think i am just getting a couple of things the wrong way round.

My string looks like this:

sql="UPDATE my_table SET "
if request("fullname") <> 0 then
"fullname = '" & fullname & "'"

Basically what i am trying to do is give my UPDATE Certain criteria. If the user puts in a new name, then update it. If the user does not put in a name at all, then do not update what is already there. Thus the: if request("fullname") <> 0 then....... etc

Not sure exactly what it is i am getting wrong.

Any ideas?

Webboy

korkus2000

4:04 pm on Nov 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would do it this way

If request("fullname") <> 0 Then
sql="UPDATE my_table SET fullname = '" & fullname & "'"
Else
sql="UPDATE my_table SET fullname = 'joe schmoe'"
End If

You could also set a variable for the dynamic part of the statement.

ukgimp

4:06 pm on Nov 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have loads of " all over the show which from my experience cause greif and woe.

I would have thought it would be better to do the conditional bits outside of the actaul SQL you are going to use. If present and correct assign to a variable and then do insert.

Cheers

andreasfriedrich

4:07 pm on Nov 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I canīt imagine why you would want to put ASP code within an SQL string. SQL servers do not understand ASP.

As for your example I would execute the SQL statement only if a valid name was given.

Andreas

korkus2000

4:10 pm on Nov 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I made a mistake

If request("fullname") <> 0 Then
sql="UPDATE my_table SET fullname = '" & request("fullname") & "'"
Else
sql="UPDATE my_table SET fullname = 'joe schmoe'"
End If

I would set request("fullname") to a variable before I got there though.

webboy1

4:38 pm on Nov 12, 2002 (gmt 0)

10+ Year Member



That is a bit spooky Corkus2000!

I have just tried that exact string a few minutes before i checked for replies to my question.......and it worked.

Cheers
Webboy

txbakers

12:22 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, you can add all sorts of dynamic elements to the SQL query. I set column headers to sort variables and alter the ORDER BY clause based on ASP code.

I have one page on which the user can select 20 or more columns to print, along with various filters for thousands of possible SQL combinations on the page eventually generated.

You just have to be careful with the SQL statement itself.