Understanding Classic ASP

485 Views Asked by At

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
4

There are 4 best solutions below

8
On BEST ANSWER

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.

0
On

I would rewrite this using parameterized ADO query. The method being used has an unnecessary SELECT, which makes the INSERT slower.

That code seems a bit obscure, but all they are doing is creating an empty instance of a recordset row so the values can be set and the recordset resaved. This is bound to be much slower than doing a straight INSERT.

0
On

I would utilize an insert statement. The above mentioned code seems a little bit "lazy"... as in "let ADO do the work for me". However, there is nothing really wrong with it. The where 1=2 part was there to return an "empty table"... um... I mean recordset.

0
On

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.