|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
any1 knows how to code it.
lets say now i have a SELECT statement tat chooses a number back based on a condition...
say mayb the number is SNO.. if there is such an number(does not matter wat is the number) then i wan to do sumthing say task A. if there is no SNO tat match the condition... then i wana to do TASK B how can i use if else to code it? wat does the database return to my asp page? |
|
#2
|
|||
|
|||
|
Depends on what the task A and B are. Do you want to do these "tasks" within a SQL statement or within code. For some "tasks", it is not possible (or inefficient) to do it in SQL and you may need to do it in your application code (ASP).
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. |
|
#3
|
|||
|
|||
|
hmm..
ok let me describe u the prob...
i wana to prevent a duplicate insertion of a tuple. but i need to know b4 i insert another tuple if there is 1 tat is already inside... i know its quite dumb cos it is not suppose to store duplicate but trust me... this prob is too much too tink logically... so i will do a search and find... if yes then i will stop the insertion and mayb link to an error page... if no such tuple then i will insert the new tuple how? |
|
#4
|
|||
|
|||
|
Ok, this can be done in a SQL statement then
Code:
IF NOT EXISTS ( SELECT field1 FROM tablename WHERE field1 = value1 AND field2 = value2 ) BEGIN INSERT INTO tablename (field1, field2, field3) VALUES (value1, value2, value3) END The other way is to exploit the "Easier to ask forgiveness than permission" principle. You can create a unique index on the field names. Then try inserting a row directly. If the insert fails, simply suppress the error message on your application and proceed as though nothing happened .Last edited by Scorpions4ever : January 28th, 2005 at 02:30 AM. |
|
#5
|
|||
|
|||
|
thks but sori
sori pal...
i am rather new to all this and i have no idea how i shd insert the above code into my asp page i have set up the connection string but still i dun realli understand can u help? |
|
#6
|
|||
|
|||
|
You should really get a book and study the basics of how to execute SQL statements yourself. That is the best way to do it. You already know how to make a database connection. The next thing to read about is how to execute SQL statements.
|
|
#7
|
||||
|
||||
|
You could do it very easy, like this
Create a stored procedure in SQL Server Code:
Create Procedure stp_MyProcedure @value1 As datatype, @value2 As datatype, @value3 As datatype As IF NOT EXISTS ( SELECT field1 FROM tablename WHERE field1 = @value1 AND field2 = @value2 ) BEGIN INSERT INTO tablename (field1, field2, field3) VALUES (@value1, @value2, @value3) END Then on the asp page. Code:
value1 = Request.Form("Field1")
value2 = Request.Form("Field2")
value3 = Request.Form("Field3")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Your ConnectionString"
This assumes the 3 values are strings.
Conn.Execute("Exec stp_MyProcedure @value1 = '" & value1 & "', @value2 = '" & value2 & "', @value3 = '" & value3 & "'")
Conn.Close
Set Conn = Nothing
|
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > any1 knows how to code it. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|