Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get the value of a variable in a trigger

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

Posted - 2010-04-25 : 01:41:34
Do you mean you want it returned to the application?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 1
Incorrect syntax near the keyword 'where'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'else'.

Thank you!
Go to Top of Page

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')
else
print('not ok')
[/code]

hope it will return only 1 record for condition where B='fs'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 status
borrow 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 transaction
But 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 code

create table Book(
code varchar(9) primary key,
status varchar(15));

create table borrow(
code_book varchar(9) references Book(code)
)

create trigger check_for_borrow
on 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')



Go to Top of Page

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_borrow
on borrow
instead of insert as
begin
if exists (select 1 from inserted i
join book b
on b.code=i.code_book
where b.status='exist')
begin
insert into borrow
select i.code_book
from inserted i
join book b
on b.code=i.code_book
where b.status='exist'
end
end


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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_borrow
on borrow
instead of insert as
begin
if exists (select 1 from inserted i
join book b
on b.code=i.code_book
where b.status='exist')
begin
insert into borrow
select i.code_book
from inserted i
join book b
on b.code=i.code_book
where b.status='exist'
end
end


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://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 error
Here's my code

select * from borrow
create trigger check_for_del_book
on book for delete as
begin
delete borrow from borrow,deleted
where borrow.code_book = deleted.code
end

ex: 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!!!!!!!!
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 c
from ChildTable c
join borrow b
on c.LinkColumn = b.code_book
join deleted d
on b.code_book = d.code

delete b
from borrow b
join deleted d
on b.code_book = d.code

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 3
Invalid object name 'bororow'.

Please tell me why?
Go to Top of Page

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 3
Invalid object name 'bororow'.

Please tell me why?



somewhere you've specified object name wrong
its borrow not bororow

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -