Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Classic ASP

I am looking through some old code and found a piece that i cant' seem to understand the point of....As far as i can tell, it's just a simple insert. but why did they do it this way? would it be okay to rewrite into an insert, or could i potentially break something?

please see below:

Set TextRS = Server.CreateObject("ADODB.RecordSet")
 Set TextRS.ActiveConnection = Conn
 TextRS.Source = "SELECT IDX,TIMESTAMP,CURRENTFLAG,TEXT FROM " & TextTable & " WHERE 1=2 FOR UPDATE"
 TextRS.CursorLocation = 2
 TextRS.CursorType = 3
 TextRS.Open ,,,3

 TextRS.AddNew
 TextRS(0).Value = IDX
 TextRS(1).Value = Timestamp
 TextRS(2).Value = "Y"
  TextRS(3).AppendChunk TextPiece
 TextRS.Update

 TextRS.Close
like image 459
Madam Zu Zu Avatar asked Feb 28 '26 17:02

Madam Zu Zu


2 Answers

This part of the source confused me a bit.... where 1 = 2??? Apparently it had a purpose to ensure no match.

Anyway this style of programming is fairly old using ADO technology and people coming from DAO to ADO would often open up a cursor to iterate over the database this way... it does not follow modern best practices on how to do things, you can and should replace it with an insert statement!

It is possible that it was written pre jet4.0/access 2000 in which case it was an attempt to simulate a parameterized stored procedure. Though if the system is at all more modern than that I would strongly recommend using a stored procedure as it has multiple benefits. Cached Execution Plans, Parameters to reduce the chances of SQL injection

I actually used to write code very much like that 12 years ago or so :p Mostly because I just didn't know better, regardless of the tech in use.

like image 63
Jordan Avatar answered Mar 03 '26 09:03

Jordan


Ah, good old classic ASP ;)

The 1 = 2 forces the sql to never return a match. It's basically a way of building up the command (?) so that you can then "conveniently" change the values and then an update will store it.

I've seen it done before, but never did it that way myself. As others have said, a simple paremetised INSERT statement will be better, IMO.

like image 36
Matt Roberts Avatar answered Mar 03 '26 09:03

Matt Roberts