| Author |
Topic |
|
upani
Starting Member
12 Posts |
Posted - 2010-04-25 : 01:36:33
|
| Hi all,I want to get the value of a variable that i created in a trigger but i couldn't.Is there any way to do that?Please tell me!Thanks! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-25 : 02:03:25
|
| just like you get variable value in a procedure. But if you're trying to get value from INSERTED/DELETED tables onto table then keep in mind that it wont work for bulk insert/delete/update operations. so you may be better off using a table variable instead------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
upani
Starting Member
12 Posts |
Posted - 2010-04-25 : 03:20:34
|
Please tell me the wrong of this statement, how to bug? quote: if((select A from addBook)='abcd' where B='fs') print('ok')else print('not ok')
With A, B are two columns of a table I don't understand why the result is:Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'where'.Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'else'.Thank you! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-25 : 05:00:55
|
| [code]declare @Aval varchar(10)select @Aval=A from addBook where B='fs'if(@Aval='abcd' )print('ok')elseprint('not ok')[/code]hope it will return only 1 record for condition where B='fs'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
upani
Starting Member
12 Posts |
Posted - 2010-04-25 : 06:19:44
|
quote: Originally posted by upani Thanks visakh16!But i still encounter when i write a simple statement like that I have two table Book and borrow Book has two columns are: code((primary key) and statusborrow has one column code_book: (references to code of book)I want to create a trigger that if i insert a recode into borrow, it will check if status of Book is 'exist' or 'not exist'. If it doesn't exist, rollback transactionBut when i test, transaction still executes although the status responsibles the code is 'not exits'. I don't know why. Please show me!Here's my codecreate table Book(code varchar(9) primary key,status varchar(15));create table borrow(code_book varchar(9) references Book(code))create trigger check_for_borrowon borrow for insert as declare @val varchar select @val = status from Book Where (Select count(*) from Book,inserted where Book.code=inserted.code_book)=1 if(@val='not exist') begin print('This book doesnt exist') rollback transaction end else print('ok')
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-25 : 11:10:58
|
why not make it a instead of trigger then?create trigger check_for_borrowon borrow instead of insert asbeginif exists (select 1 from inserted i join book b on b.code=i.code_book where b.status='exist')begininsert into borrowselect i.code_bookfrom inserted ijoin book bon b.code=i.code_bookwhere b.status='exist'endend ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
upani
Starting Member
12 Posts |
Posted - 2010-04-25 : 13:58:06
|
quote: Originally posted by visakh16 why not make it a instead of trigger then?create trigger check_for_borrowon borrow instead of insert asbeginif exists (select 1 from inserted i join book b on b.code=i.code_book where b.status='exist')begininsert into borrowselect i.code_bookfrom inserted ijoin book bon b.code=i.code_bookwhere b.status='exist'endend ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks visakh16! I want to ask one more question, please.I create a trigger on Book for deleted that borrow refers to it will be deleted when i delete it but the result returns errorHere's my codeselect * from borrowcreate trigger check_for_del_bookon book for delete as begin delete borrow from borrow,deleted where borrow.code_book = deleted.code endex: delete from Book where code='A111'=> The DELETE statement conflicted with the REFERENCE constraint "FK__borrow__code_boo__30C33EC3". The conflict occurred in database "LibraryManagement", table "dbo.borrow", column 'code_book'.The statement has been terminated.Why that? Please show me how to solve this problem.Thanks so much!!!!!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-25 : 14:58:11
|
| seems like you've a fk relationship from borrow to book on code_book column. so before you delete a book record, you need to make sure no reference of book exists on borrow. so whenever you need to delete a book, first step is to look for borrow records that reference it delete it and then delete actual book record. in such case you dont need last trigger at all as you always do deletion from borrow before you delete the master book record. Alternatively you could set ON DELETE CASCADE option while creating FK from borrow -> book on which case all borrow records automatically gets deleted when the book record they refer is deleted.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-04-25 : 15:02:42
|
| The error means that you are trying to delete parent data but the child rows are still there. You have to delete the child data first and then the parent. From the error, it looks like you'll need to delete from whatever table is linked to code_book before you can delete from borrow table.Sort of like this:delete cfrom ChildTable cjoin borrow bon c.LinkColumn = b.code_bookjoin deleted don b.code_book = d.codedelete bfrom borrow bjoin deleted don b.code_book = d.codeTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
upani
Starting Member
12 Posts |
Posted - 2010-04-25 : 20:18:29
|
| Hi!When i create borrow table as the way visakha16 tell me:create table borrow(code_book varchar(9) references Book(code)ON DELETE CASCADE)And an delete a recode from Book:delete from Book where code='A225'Then it does'n work, the result is:(0 row(s) affected)Msg 208, Level 16, State 1, Procedure checkdel, Line 3Invalid object name 'bororow'.Please tell me why? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-26 : 00:18:55
|
quote: Originally posted by upani Hi!When i create borrow table as the way visakha16 tell me:create table borrow(code_book varchar(9) references Book(code)ON DELETE CASCADE)And an delete a recode from Book:delete from Book where code='A225'Then it does'n work, the result is:(0 row(s) affected)Msg 208, Level 16, State 1, Procedure checkdel, Line 3Invalid object name 'bororow'.Please tell me why?
somewhere you've specified object name wrongits borrow not bororow------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|