MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old August 22nd, 2004, 01:58 PM
billmannion billmannion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 billmannion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
There has to be a better way to INSERT/UPDATE

I have a form that pulls existing information from a database and allows users to edit it. Every field can be left blank if the user wishes. I am having an issue with determining which SQL statement to run. There has to be an easier way to do this then what I am doing. For each filed I process the data this way:

SQL2="select * from 1985ClassList where MailingListID =" & Session("EID")
set aData = oConn.execute(SQL2)


SQL = "Update 1985ClassList SET "

if aData.Fields("FirstName") > " " and Request.QueryString("FirstName") > " " then
SQL = SQL & "'FirstName' = '" & Request.QueryString("FirstName") & "', "
end if

if aData.Fields("FirstName") = " " and Request.QueryString("FirstName") > " " then
strFSQL = strFSQL & "FirstName, "
strVSQL = strVSQL & Request.QueryString("FirstName") & ", "
end if


SQL = SQL & "where MailingListID =" & Session("EID")
set mData = oConn.execute(SQL)

if strFSQL > " " then
SQL1 = "Insert into 1985ClassList " & strFSQL & "VALUES " & strVSQL
end if
set mData = oConn.execute(SQL1)

It works fine for the UPDATE, but not for the INSERT.

Reply With Quote
  #2  
Old August 22nd, 2004, 07:28 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 34 sec
Reputation Power: 891
could you please echo out the sql prior to execution and display that here

i cannot see what the sql would look like after the script has finished building it
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old August 23rd, 2004, 08:52 AM
billmannion billmannion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 billmannion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is the one line I get:
Update 1985ClassList SET 'FirstName' = 'Tracy', 'LastName' = 'Bergeron', where MailingListID =10
I do not get anything for an INSERT as a new record.
The database has people's names in it already, but nothing else. I have debug code to test the address field that will echo what is going on. It can be seen at http://www.ths85.org/contact.asp. Select any name from the long list and continue as if you want to save information you edit. Don't worry I have commented out the execution line, so it won't actually go to the database.

Reply With Quote
  #4  
Old August 23rd, 2004, 09:40 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 34 sec
Reputation Power: 891
remove the quotes from the column names, and the spurious comma in front of the WHERE

Update 1985ClassList
SET FirstName = 'Tracy', LastName = 'Bergeron' where MailingListID =10

Reply With Quote
  #5  
Old August 23rd, 2004, 09:59 AM
billmannion billmannion is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 billmannion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I will make the minor syntax changes to the UPDATE, that's easy.

Any reason why the code is not generating the INSERT statement.
I think it has some thing in the way I am compairing the data from the database and the form, see this line from the code in the thread:
if aData.Fields("FirstName") = " " and Request.QueryString("FirstName") > " " then

Reply With Quote
  #6  
Old August 23rd, 2004, 10:31 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 34 sec
Reputation Power: 891
sorry, i don't read that particular code language

if you could echo the INSERT sql...

otherwise, please post in the forum for whatever language that is

Reply With Quote
  #7  
Old August 23rd, 2004, 11:44 AM
devikta devikta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Houston,TX
Posts: 3 devikta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to devikta
I just have a suggestion that could make your programming life that much easier. Use an INSTEAD OF trigger on the table you're inserting into. That way, you just use an insert statement for everything and let the trigger figure out if it should really be an update.

Reply With Quote
  #8  
Old August 23rd, 2004, 01:45 PM
PrivateJson PrivateJson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Denmark
Posts: 25 PrivateJson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 22 m 16 sec
Reputation Power: 0
What's up with the: if aData.Fields("FirstName") > " "

Try using <> "" instead, if you need to test whether or not the string is empty.

Apart from that, if you're using MS SQL, and I guessing you are, since this is the MS SQL forum, your code is one big invitation for SQL injection.

Whenever you take something from the REQUEST object (generally for user input) and use it as part of a SQL statement, validate the content. If it's supposed to be a number, cast it (CLng or CInt, etc.), if it's a string, make sure you replace single quotes with double single quotes!!

Quote:
Originally Posted by billmannion
SQL = "Update 1985ClassList SET "

if aData.Fields("FirstName") > " " and Request.QueryString("FirstName") > " " then
SQL = SQL & "'FirstName' = '" & Request.QueryString("FirstName") & "', "
end if

if aData.Fields("FirstName") = " " and Request.QueryString("FirstName") > " " then
strFSQL = strFSQL & "FirstName, "
strVSQL = strVSQL & Request.QueryString("FirstName") & ", "
end if


SQL = SQL & "where MailingListID =" & Session("EID")
set mData = oConn.execute(SQL)

if strFSQL > " " then
SQL1 = "Insert into 1985ClassList " & strFSQL & "VALUES " & strVSQL
end if
set mData = oConn.execute(SQL1)

It works fine for the UPDATE, but not for the INSERT.

Reply With Quote
  #9  
Old September 1st, 2004, 06:35 AM
ptauwhare ptauwhare is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 19 ptauwhare User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 44 sec
Reputation Power: 0
Why not your use a stored procedure and let SQL do all the hard work? This avoids horrendous looking code like you have.

Reply With Quote
  #10  
Old September 1st, 2004, 02:35 PM
PrivateJson PrivateJson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Denmark
Posts: 25 PrivateJson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 22 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by ptauwhare
Why not your use a stored procedure and let SQL do all the hard work? This avoids horrendous looking code like you have.


Agreed!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > There has to be a better way to INSERT/UPDATE


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway