Solved

Problem with a trigger?

Posted on 2006-07-18
8
432 Views
Last Modified: 2008-02-01
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)


0
Comment
Question by:lordicarus
  • 4
  • 3
8 Comments
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 500 total points
ID: 17130946
Add  this line:
SET NOCOUNT ON
Right after:
CREATE TRIGGER UpdateStationSlots ON [dbo].[ss_stn]
FOR INSERT, UPDATE
AS

set rs = db.execute("SET NOCOUNT ON; insert into ss_stn (stn_name, stn_slt, cnl_id) values ('test', '1', 'test'); select SCOPE_IDENTITY() as id")

I think the execute might return more than one RS, so you can't find 'id' on the first one.

0
 
LVL 12

Expert Comment

by:Preece
ID: 17130976
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
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17131049
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...
0
 
LVL 2

Author Comment

by:lordicarus
ID: 17131229
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17131312
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 :)
0
 
LVL 2

Author Comment

by:lordicarus
ID: 17131331
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?
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17131359
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 :)
0
 
LVL 2

Author Comment

by:lordicarus
ID: 17131684
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now