sql server 2008 - SQL TRIGGER THE MULTI-PART IDENTIFIER CANT BE BOUND -
i new programming. trying teach myself sql. made video store database customer, rental, , inventory table. looking @ examples online try learn sql, please kind. want trigger stop person renting same movie @ same time. have been reading on triggers , code below.
i getting syntax errors on last end, syntax error on raiseerror, , inserted. still getting message "trigger multipart identifier cannot bound" on i.rental.rentnum inserted i;
any appreciated!
create trigger insteadofinsert on rental instead of insert declare @rentnum int, @action varchar(60) select @rent_rentnum=i.rental.rentnum inserted i; set @action='stop rental trigger.' @rent_rentnum=(select rentnum inserted; begin begin tran set nocount on if (@rent_rentnum=rental.rentnum) begin raiseerror ('you cannot rent same move twice'); rollback end else begin insert rental(rentnum) values (@rentnum) insert rent values (rentnum); commit print 'updated' end end
your trigger has several fundamental flaws:
please never ever use
begin tran
inside trigger! trigger running in context of statement caused fire, , therefore already in context of transactionyou need aware trigger called once per statement - not once per row! if
insert
statement inserts 10 rows, trigger fired once, ,inserted
pseudo table contain 10 rows of data - 1 think picked in statementselect rentnum inserted;
one of them - 1 more or less @ random - , 9 others ignored.
so basically, need totally rewrite trigger this:
create trigger insteadofinsert on dbo.rental after insert // if 1 of rows inserted exists in rental table -> abort if exists (select * dbo.rental rentnum in (select rentnum inserted)) begin raiseerror ('you cannot rent same move twice'); rollback end
you didn't explain why had picked use instead of insert
trigger - don't see reason that, chose after insert
trigger instead (it's plain simpler write these)
Comments
Post a Comment