Here is my RunSQL function that allows me to run SQL statement anywhere easily. My problem is the way to identify the statement type i.e. SELECT, UPDATE or INSERT. Because I want to return the affected records if it's update or insert, and also I want to return the recordset and CLOSE the recordset if it's select. If I close the recordset if it's UPDATE it returns an error and doesn't update anything. I wonder if there is a way to check it at object level. Right now it works 99% accuracy, but it looks for substrings. I have to name the stored procedures like "sp_select..." even if it updates something and returns a resultset.
ps. "if (rs.eof and rs.bof) then" doesn't work either to identify if it's an UPDATE.
function RunSQL(SQL)
dim rs,affected, conn
set conn = server.createobject("adodb.connection"
conn.Open CONN_STRING
select case left(lcase(SQL),3)
case "sel"
set rs = conn.execute(SQL)
if not (rs.eof and rs.bof) then
RunSQL = rs.getrows()
else
RunSQL = null
end if
case "exe"
if inStr(SQL,"sp_select"
> 0 then
set rs = conn.execute(SQL)
if not (rs.eof and rs.bof) then
RunSQL = rs.getrows()
else
RunSQL = null
end if
else
set rs = conn.execute(SQL,affected)
RunSQL = affected
end if
case "ins","upd","del"
set rs = conn.execute(SQL,affected)
RunSQL = affected
end select
set rs = nothing
conn.close : set conn = nothing
end function
ps. "if (rs.eof and rs.bof) then" doesn't work either to identify if it's an UPDATE.
function RunSQL(SQL)
dim rs,affected, conn
set conn = server.createobject("adodb.connection"
conn.Open CONN_STRING
select case left(lcase(SQL),3)
case "sel"
set rs = conn.execute(SQL)
if not (rs.eof and rs.bof) then
RunSQL = rs.getrows()
else
RunSQL = null
end if
case "exe"
if inStr(SQL,"sp_select"
set rs = conn.execute(SQL)
if not (rs.eof and rs.bof) then
RunSQL = rs.getrows()
else
RunSQL = null
end if
else
set rs = conn.execute(SQL,affected)
RunSQL = affected
end if
case "ins","upd","del"
set rs = conn.execute(SQL,affected)
RunSQL = affected
end select
set rs = nothing
conn.close : set conn = nothing
end function