When I want to make an update on a table which make a trigger action, it
gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
already exist"
Can anyone please give me more info on msg 16915 or how to solve it? Thanks.Possibly the trigger creates a cursor without deallocating it. Check the trigger code.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
""Allen Iverson"" <no_spam@.bk.com> wrote in message news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
> When I want to make an update on a table which make a trigger action, it
> gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
> already exist"
> Can anyone please give me more info on msg 16915 or how to solve it? Thanks.
>|||In fact, this is the code, can anyone help:
if exists (select id from sysobjects where name = 'apple.A4000_ins_upd_trg'
and
type = 'TR')
begin
drop trigger apple.A4000_ins_upd_trg
print 'A4000_ins_upd_trg is dropped.'
end
go
create trigger apple.A4000_ins_upd_trg on apple.APRN4000
for update, insert
as
declare @.schoolyear varchar(1),
@.UNID varchar(20),
@.OBJECT_TYPE char(1),
@.SCHOOLNUM varchar(3),
@.STULINK numeric(5),
@.SEQUENCE numeric(5),
@.STATUS char(1),
@.RELATION varchar(2),
@.LASTNAME varchar(20),
@.FIRSTNAME varchar(20),
@.MIDDLENAME varchar(13),
@.SALUTATION varchar(4),
@.EMPLOYER varchar(40),
@.OCCUPATION varchar(30),
@.WRKTEL numeric(9),
@.WRKEXTN varchar(4),
@.ALTTEL numeric(9),
@.TELEPHONE numeric(9),
@.USERSTAMP varchar(10),
@.PERMNUM varchar(12),
@.GRADE varchar(2),
@.FAMILYNUM numeric(5),
@.ADDRESS1 varchar(50),
@.ADDRESS2 varchar(40),
@.AREA varchar(30),
@.TERRITORY varchar(25),
@.FAXNO varchar(11),
@.ATTENTION varchar(30),
@.REFCODE varchar(30),
@.counter int,
@.rec_count_before int,
@.rec_count_after int
-- initialization
select @.schoolyear = '4'
select @.counter = 1
-- count how many records in inserted table
select @.rec_count_after = count(STULINK)
from inserted
-- count how many records in before table
select @.rec_count_before = count(STULINK)
from deleted
-- define cursor
declare inserted_curs cursor for
select SCHOOLNUM,
STULINK,
SEQUENCE,
STATUS,
RELATION,
LASTNAME,
FIRSTNAME,
MIDDLENAME,
SALUTATION,
EMPLOYER,
OCCUPATION,
WRKTEL,
WRKEXTN,
ALTTEL,
TELEPHONE
from inserted
open inserted_curs
fetch inserted_curs
into @.SCHOOLNUM,
@.STULINK,
@.SEQUENCE,
@.STATUS,
@.RELATION,
@.LASTNAME,
@.FIRSTNAME,
@.MIDDLENAME,
@.SALUTATION,
@.EMPLOYER,
@.OCCUPATION,
@.WRKTEL,
@.WRKEXTN,
@.ALTTEL,
@.TELEPHONE
if @.rec_count_before = 0
-- insert action
begin
print 'insert action'
print @.STULINK
print @.sequence
insert into dbo.EAI_ADM_PARENT
(STULINK,
SEQUENCE)
VALUES
(@.STULINK,
@.SEQUENCE)
end
else
-- update action
-- we can assume the record exists in EAI_A4000
begin
print 'update action start'
while @.counter <= @.rec_count_after
begin
update dbo.EAI_ADM_PARENT
set STATUS = @.STATUS,
RELATION = @.RELATION,
LASTNAME = @.LASTNAME,
FIRSTNAME = @.FIRSTNAME,
MIDDLENAME = @.MIDDLENAME,
SALUTATION = @.SALUTATION,
EMPLOYER = @.EMPLOYER,
OCCUPATION = @.OCCUPATION,
WRKTEL = @.WRKTEL,
WRKEXTN = @.WRKEXTN,
ALTTEL = @.ALTTEL,
TELEPHONE = @.TELEPHONE
from apple.APRN4000 P
where P.STULINK = @.STULINK
and P.SEQUENCE = @.SEQUENCE
-- get next record
select @.counter = @.counter + 1
if @.counter <= @.rec_count_after
begin
fetch inserted_curs
into @.SCHOOLNUM,
@.STULINK,
@.SEQUENCE,
@.STATUS,
@.RELATION,
@.LASTNAME,
@.FIRSTNAME,
@.MIDDLENAME,
@.SALUTATION,
@.EMPLOYER,
@.OCCUPATION,
@.WRKTEL,
@.WRKEXTN,
@.ALTTEL,
@.TELEPHONE
end
end
end
-- ending session
close inserted_curs
deallocate inserted_curs
print 'successful'
go
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:Oo3U0$D2EHA.3708@.TK2MSFTNGP14.phx.gbl...
> Possibly the trigger creates a cursor without deallocating it. Check the
> trigger code.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
>> When I want to make an update on a table which make a trigger action, it
>> gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
>> already exist"
>> Can anyone please give me more info on msg 16915 or how to solve it?
>> Thanks.
>>
>|||Where can I check the message meaning?
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:Oo3U0$D2EHA.3708@.TK2MSFTNGP14.phx.gbl...
> Possibly the trigger creates a cursor without deallocating it. Check the
> trigger code.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
>> When I want to make an update on a table which make a trigger action, it
>> gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
>> already exist"
>> Can anyone please give me more info on msg 16915 or how to solve it?
>> Thanks.
>>
>|||Can any one please help? Thanks.
""Allen Iverson"" <no_spam@.bk.com> wrote in message
news:Oa5t1LE2EHA.1408@.TK2MSFTNGP10.phx.gbl...
> In fact, this is the code, can anyone help:
>
> if exists (select id from sysobjects where name => 'apple.A4000_ins_upd_trg' and
> type = 'TR')
> begin
> drop trigger apple.A4000_ins_upd_trg
> print 'A4000_ins_upd_trg is dropped.'
> end
> go
> create trigger apple.A4000_ins_upd_trg on apple.APRN4000
> for update, insert
> as
> declare @.schoolyear varchar(1),
> @.UNID varchar(20),
> @.OBJECT_TYPE char(1),
> @.SCHOOLNUM varchar(3),
> @.STULINK numeric(5),
> @.SEQUENCE numeric(5),
> @.STATUS char(1),
> @.RELATION varchar(2),
> @.LASTNAME varchar(20),
> @.FIRSTNAME varchar(20),
> @.MIDDLENAME varchar(13),
> @.SALUTATION varchar(4),
> @.EMPLOYER varchar(40),
> @.OCCUPATION varchar(30),
> @.WRKTEL numeric(9),
> @.WRKEXTN varchar(4),
> @.ALTTEL numeric(9),
> @.TELEPHONE numeric(9),
> @.USERSTAMP varchar(10),
> @.PERMNUM varchar(12),
> @.GRADE varchar(2),
> @.FAMILYNUM numeric(5),
> @.ADDRESS1 varchar(50),
> @.ADDRESS2 varchar(40),
> @.AREA varchar(30),
> @.TERRITORY varchar(25),
> @.FAXNO varchar(11),
> @.ATTENTION varchar(30),
> @.REFCODE varchar(30),
> @.counter int,
> @.rec_count_before int,
> @.rec_count_after int
>
> -- initialization
> select @.schoolyear = '4'
> select @.counter = 1
>
> -- count how many records in inserted table
> select @.rec_count_after = count(STULINK)
> from inserted
> -- count how many records in before table
> select @.rec_count_before = count(STULINK)
> from deleted
>
> -- define cursor
> declare inserted_curs cursor for
> select SCHOOLNUM,
> STULINK,
> SEQUENCE,
> STATUS,
> RELATION,
> LASTNAME,
> FIRSTNAME,
> MIDDLENAME,
> SALUTATION,
> EMPLOYER,
> OCCUPATION,
> WRKTEL,
> WRKEXTN,
> ALTTEL,
> TELEPHONE
> from inserted
> open inserted_curs
> fetch inserted_curs
> into @.SCHOOLNUM,
> @.STULINK,
> @.SEQUENCE,
> @.STATUS,
> @.RELATION,
> @.LASTNAME,
> @.FIRSTNAME,
> @.MIDDLENAME,
> @.SALUTATION,
> @.EMPLOYER,
> @.OCCUPATION,
> @.WRKTEL,
> @.WRKEXTN,
> @.ALTTEL,
> @.TELEPHONE
> if @.rec_count_before = 0
> -- insert action
> begin
> print 'insert action'
> print @.STULINK
> print @.sequence
> insert into dbo.EAI_ADM_PARENT
> (STULINK,
> SEQUENCE)
> VALUES
> (@.STULINK,
> @.SEQUENCE)
> end
> else
> -- update action
> -- we can assume the record exists in EAI_A4000
> begin
> print 'update action start'
> while @.counter <= @.rec_count_after
> begin
> update dbo.EAI_ADM_PARENT
> set STATUS = @.STATUS,
> RELATION = @.RELATION,
> LASTNAME = @.LASTNAME,
> FIRSTNAME = @.FIRSTNAME,
> MIDDLENAME = @.MIDDLENAME,
> SALUTATION = @.SALUTATION,
> EMPLOYER = @.EMPLOYER,
> OCCUPATION = @.OCCUPATION,
> WRKTEL = @.WRKTEL,
> WRKEXTN = @.WRKEXTN,
> ALTTEL = @.ALTTEL,
> TELEPHONE = @.TELEPHONE
> from apple.APRN4000 P
> where P.STULINK = @.STULINK
> and P.SEQUENCE = @.SEQUENCE
> -- get next record
> select @.counter = @.counter + 1
> if @.counter <= @.rec_count_after
> begin
> fetch inserted_curs
> into @.SCHOOLNUM,
> @.STULINK,
> @.SEQUENCE,
> @.STATUS,
> @.RELATION,
> @.LASTNAME,
> @.FIRSTNAME,
> @.MIDDLENAME,
> @.SALUTATION,
> @.EMPLOYER,
> @.OCCUPATION,
> @.WRKTEL,
> @.WRKEXTN,
> @.ALTTEL,
> @.TELEPHONE
> end
> end
> end
>
> -- ending session
> close inserted_curs
> deallocate inserted_curs
> print 'successful'
> go
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote
> in message news:Oo3U0$D2EHA.3708@.TK2MSFTNGP14.phx.gbl...
>> Possibly the trigger creates a cursor without deallocating it. Check the
>> trigger code.
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
>> news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
>> When I want to make an update on a table which make a trigger action, it
>> gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
>> already exist"
>> Can anyone please give me more info on msg 16915 or how to solve it?
>> Thanks.
>>
>>
>|||Actually, I doubt I found the cause of error.
Here is situation. I define trigger A in table TB1 and trigger B in table
TB2. The change in record in TB1 make trigger A act in which write record to
TB2 and make trigger B act in which write record to TB3. Much like a chain.
But then when I make change in TB1, I got error message saying cursor
already exists. Can anyone please help? Thanks.
""Allen Iverson"" <no_spam@.bk.com> wrote in message
news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
> When I want to make an update on a table which make a trigger action, it
> gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
> already exist"
> Can anyone please give me more info on msg 16915 or how to solve it?
> Thanks.
>|||Perhaps the inner trigger creates a cursor with the same name as already was created by the outer
trigger?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
""Allen Iverson"" <no_spam@.bk.com> wrote in message news:O1NItpQ2EHA.3324@.tk2msftngp13.phx.gbl...
> Actually, I doubt I found the cause of error.
> Here is situation. I define trigger A in table TB1 and trigger B in table
> TB2. The change in record in TB1 make trigger A act in which write record to
> TB2 and make trigger B act in which write record to TB3. Much like a chain.
> But then when I make change in TB1, I got error message saying cursor
> already exists. Can anyone please help? Thanks.
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
> > When I want to make an update on a table which make a trigger action, it
> > gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
> > already exist"
> >
> > Can anyone please give me more info on msg 16915 or how to solve it?
> > Thanks.
> >
>|||Can anyone please help?
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:usqoWuQ2EHA.1152@.TK2MSFTNGP14.phx.gbl...
> Perhaps the inner trigger creates a cursor with the same name as already
> was created by the outer
> trigger?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:O1NItpQ2EHA.3324@.tk2msftngp13.phx.gbl...
>> Actually, I doubt I found the cause of error.
>> Here is situation. I define trigger A in table TB1 and trigger B in table
>> TB2. The change in record in TB1 make trigger A act in which write record
>> to
>> TB2 and make trigger B act in which write record to TB3. Much like a
>> chain.
>> But then when I make change in TB1, I got error message saying cursor
>> already exists. Can anyone please help? Thanks.
>>
>> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
>> news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
>> > When I want to make an update on a table which make a trigger action,
>> > it
>> > gives me error "msg16915, level 16, State 1, Procedure ..." and
>> > "cursor
>> > already exist"
>> >
>> > Can anyone please give me more info on msg 16915 or how to solve it?
>> > Thanks.
>> >
>>
>|||You've got more problems with this trigger than just that error.
First of all, you have some serious logic bugs and some flawed usage.
1. Try to avoid cursors always, but especially inside of a trigger.
2. The INSERTED and DELETED tables will only store rows that were modified.
If Inserts, DELETED will be NULL; if Update, INSERTED and DELETED will
contain the same records; if Delete, INSERTED will be NULL. Neither of
these will have the same count as the base table unless this is the first
statement executed against the base table or was applied to the entire
table.
3. If you do use a cursor, you need to either code a GLOBAL CURSOR, and need
to check for its existence before you create it or use TEMPORARY cursors,
LOCAL.
4. The logic was a little conveluted; so, I'm not sure what the intent of
the trigger is but you could have simplified the code dramatically by using
constraints to handle most of this and then only using set-based statements
within the trigger for the specialized rules.
5. The error message you are getting is due to the trigger firing more than
once simultaneously due to concurrent users. The cursor already exists when
the second, or n th firing occurs.
Sincerely,
Anthony Thomas
""Allen Iverson"" <no_spam@.bk.com> wrote in message
news:%23fePwaD2EHA.2156@.TK2MSFTNGP10.phx.gbl...
When I want to make an update on a table which make a trigger action, it
gives me error "msg16915, level 16, State 1, Procedure ..." and "cursor
already exist"
Can anyone please give me more info on msg 16915 or how to solve it? Thanks.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment