Advertisement

07.18.2006 at 07:52AM PDT, ID: 21923151
[x]
Attachment Details

Problem with a trigger?

Asked by lordicarus in Active Server Pages (ASP)

Tags: error, scope_identity, 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)


Start Free Trial
[+][-]07.18.2006 at 08:13AM PDT, ID: 17130946

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Active Server Pages (ASP)
Tags: error, scope_identity, trigger
Sign Up Now!
Solution Provided By: DireOrbAnt
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.18.2006 at 08:16AM PDT, ID: 17130976

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 08:22AM PDT, ID: 17131049

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 08:45AM PDT, ID: 17131229

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 08:54AM PDT, ID: 17131312

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 08:55AM PDT, ID: 17131331

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 08:58AM PDT, ID: 17131359

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2006 at 09:35AM PDT, ID: 17131684

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32