Link to home
Start Free TrialLog in
Avatar of lordicarus
lordicarusFlag for United States of America

asked on

Problem with a trigger?

Below is a bunch of information relating to an error I am receiving and I can not figure out why.

It is essentially a very basic table setup, a pretty simple trigger, and a really simple piece of
ASP code.  For some reason the ASP code is not actually returning me the information I am looking
for and I don't understand why it won't work.  Before I created this trigger that block of ASP code
worked perfectly well.  This is why I am confused.  Running the same exact query that is on the
first line of the ASP code in SQL Query Analyzer gives me the SCOPE_IDENTITY() value as it should.  In ASP
though for some reason this is not the case.  I can not figure out why.

In SQL Query Analyzer, I also know that using @@IDENTITY instead of SCOPE_IDENTITY() works as well, but it also does not work in ASP.

What's going on here?  How do I fix this and get the id value?



Table ss_stn
------------------
stn_id            int            primarykey
stn_name            varchar(50)
stn_slt            int
cnl_id            int






Trigger On ss_stn
------------------
CREATE TRIGGER UpdateStationSlots ON [dbo].[ss_stn]
FOR INSERT, UPDATE
AS

-- Create a temporary table for the slot information to be stored
CREATE TABLE #t ( stn_id int, stn_slt int )

-- Declare a variable to use for updating the slots sequentially
DECLARE @SLOT_NUM int
SET @SLOT_NUM = 1

-- Get the id and slot of the inserted/updated record
DECLARE @INS_ID int, @INS_SLT int, @INS_CNL int
SELECT @INS_ID = stn_id, @INS_SLT = stn_slt, @INS_CNL = cnl_id FROM inserted

-- Insert all the information into the temporary table
INSERT INTO #t
      SELECT stn_id, stn_slt FROM ss_stn WHERE cnl_id = @INS_CNL
      UNION
      SELECT inserted.stn_id, inserted.stn_slt FROM inserted WHERE inserted.cnl_id = @INS_CNL
            ORDER BY stn_slt ASC

-- Start iterating through temporary table data
DECLARE @tmp_id int, @tmp_slt int
DECLARE curTEMP CURSOR FOR SELECT stn_id, stn_slt from #T
OPEN curTEMP
FETCH NEXT FROM curTEMP INTO @tmp_id, @tmp_slt
WHILE @@FETCH_STATUS = 0
BEGIN
      IF @SLOT_NUM = @INS_SLT AND @tmp_id <> @INS_ID SET @SLOT_NUM = @SLOT_NUM + 1

      IF @tmp_id <> @INS_ID
      BEGIN
            UPDATE ss_stn SET stn_slt = @SLOT_NUM WHERE stn_id = @tmp_id
            SET @SLOT_NUM = @SLOT_NUM + 1
      END
      
      
      FETCH NEXT FROM curTEMP INTO @tmp_id, @tmp_slt
END
CLOSE curTEMP
DEALLOCATE curTEMP







ASP CODE Test 1
-----------------------------
set rs = db.execute("insert into ss_stn (stn_name, stn_slt, cnl_id) values ('test', '1', 'test'); select SCOPE_IDENTITY() as id")
response.write rs(0)  'Note: Trying this as rs("id") also fails with the same error below.
 

ASP CODE Test 2
-----------------------------
set rs = db.execute("insert into ss_stn (stn_name, stn_slt, cnl_id) values ('test', '1', 'test'); select SCOPE_IDENTITY() as id")
dim trs
set trs = rs.nextrecordset
response.write rs(0)  'Note: Trying this as rs("id") also fails with the same error below.
set trs = nothing


 

Error
-----------------------------
ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/betatest.asp, line (references the lines above with "response.write rs(0)" on it in either test)


ASKER CERTIFIED SOLUTION
Avatar of DireOrbAnt
DireOrbAnt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wonder if it would help to separate the sql commands:

db.execute("insert into ss_stn (stn_name, stn_slt, cnl_id) values ('test', '1', 'test') )
sSQL = "select SCOPE_IDENTITY() as id"

SET oRS = server.CreateObject("ADODB.Recordset")
oRS.Open strSQL, db

response.write rs(0)  'Note: Trying this as rs("id") also fails with the same error below.

Preece
Avatar of DireOrbAnt
DireOrbAnt

Right Preece. I was hoping the SET NOCOUNT would remove any extra RS, we'll see.

lordicarus, also make sure id is like this [id] as in:
select SCOPE_IDENTITY() as [id]

lordicarus, another way to do this, is to make a stored procedure like this:

CREATE PROCEDURE spInsertSS
@stn_name VARCHAR(50),
@stn_slt INT,
@cnl_id INT
AS
SET NOCOUNT ON
INSERT INTO ss_stn (stn_name, stn_slt, cnl_id) values (@stn_name, @stn_slt, @cnl_id) )
SELECT SCOPE_IDENTITY() AS [id]

And then call the proc. This will give you a lot more and protect your from SQL injection attack and also avoid the need to deal qith quotes in the name and such...
Avatar of lordicarus

ASKER

Ok so the first solution actually works, but why?

It doesn't make sense to me because after i did the db.execute I tried doing response.write rs.bof & "::" & rs.eof and this says that the object is already closed implying that there are zero records.  With the NOCOUNT ON it returns the value.  This confuses me and if you can offer up an explanation I would greatly appreciate it.

Seperating the commands isn't really an option because that leaves a greater possibility of crossing inserts and getting the wrong ID.

Using a stored procedure was my original idea, but this ASP code is in about 50 different places(not my choice) and would have to be rewritten in every place, beyond that it also is a dynamically generated insert based on other information in the existing database so the insert text would have to be included as a parameter and then executed from within the sproc.

Thanks for all the help... hopefully someone can explain why NOCOUNT ON makes this work?
Without the SET NOCOUNT you get the RS like this:
(1 row(s) affected)

[id]
--------
12344
(1 row(s) affected)

With the trigger on top of that, all inserts will cause these lines to show up.
I bet in your original code you could call:
rs.NextRecordset() until you find the one with the ID :)
FYI... the reason it doesn't make sense that setting nocount on works is because when I do the same thing but with an update there is no problem.  Updates should still return the affected rows just like the insert does if nocount on is not set.


The only logical explanation I can come up with is that Query Analyzer is smart enough to see that there were two commands in the query and that I was looking for the return value of scope_identity, whereas ASP and the ADODB object isn't smart enough to realize this....?  Possibility?
How do you get a SCOPE_IDENTITY() of of an update?
As far as Query analyzer smart enough, nope, YOU are smart enough to see the result you want :)
I see what you are saying.  And I wasn't thinking about it, but there definitely is not a reference to scope_identity so that is that.  Thanks for keeping on this and helping me to understand the solution instead of just solving it.  Most people dont actually wait around to explain after they get the points!  Thanks again.