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
 Test inserted value for trigger

Author  Topic 

olibara
Yak Posting Veteran

94 Posts

Posted - 2011-03-03 : 17:31:30
Hello

This is the first time I'm trying to create a trigger and I'm lost
The goal is to fire the trigger if some specific values are inserted in a table column

The problem is that I can't found how to test the inserted value
This is my last unsuccesfull try

The main problem is how to test the value of the Column Tarra in the inserted record
Thank to help


USE [Clark]
GO
/****** Object: Trigger [dbo].[tg_DoTotal] Script Date: 03/03/2011 21:18:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tg_DoTotal]
ON [Clark].[dbo].[BakMove]
AFTER INSERT
@Tarra int
AS
select @Tarra =Tarra from inserted
IF @Tarra <1
BEGIN
PRINT 'Test Trigger'
END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-03 : 17:35:08
Is this what you want?

ALTER TRIGGER [dbo].[tg_DoTotal]
ON [Clark].[dbo].[BakMove]
AFTER INSERT
AS
IF EXISTS (SELECT * FROM inserted WHERE Tarra = 'Tarra')
PRINT 'Test Trigger'

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

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 17:49:03
@Tarra???

Really??

Tara less than 1????

Are you testing for multiple rows?

Or multiple Tara's

There is only one here



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2011-03-03 : 17:55:52
Thanks for Help

I will try to be more clear
I have a table BakMove with column Tarra (int), Bruto (int) and Date
If a record are inserted in BakMove having some specific value for Tarra, I want to fire a trigger !

This is what I'm trying to do !
Is it possible ?

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-03 : 17:56:10
I was tempted to switch it to Tara.

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

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 17:58:34
quote:
Originally posted by olibara

Thanks for Help

I will try to be more clear
I have a table BakMove with column Tarra (int), Bruto (int) and Date
If a record are inserted in BakMove having some specific value for Tarra, I want to fire a trigger !

This is what I'm trying to do !
Is it possible ?





I want a trigger to fire

To do what?

And yes it's very possible

And what does Tarra mean?

SQL Warrior Princess?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 18:00:40
and over 30k posts is INSANE

Never mind

You need to say waht needs to happen

1. I insert a row to table X

2. I want a trigger to fire on insert and move that value in column Y to table B

OK?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2011-03-03 : 18:13:51
Hello

Tarra is a Dutch word it means Tare (the weight of an empty truck)
quote:
The tare is used to remove the weight of any packaging material or containers, so that only the weight of the material which is packed within the container is displayed. The tare value is then deleted from the remaining weight that can be added to a scale. The word tare is also the weight of a railway car when it is not loaded


But the name of a column has nothing to do with the trigger behaviour I hope

So
- if a record is inserted in the Table BakMove
- And if that record have a value for Tarra < 0, I wand to do something

Here is a Csharp like pseudo code
if (Record is inserted in BakMove)
{
if (Record.Tarra < 1)
{
Do_something();
}
}
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 18:42:32
Do you thinking you are doing this for 1 Row or many rows???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2011-03-04 : 01:23:37
quote:
Do you thinking you are doing this for 1 Row or many rows???


I want to do it each time A row with specific value is inserted !

The do_something will be a stored proc to create a record in another table

But regarding all your question it seems very difficult to do !

Maybe Trigger can't do that ?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-04 : 05:51:22
Yes a trigger can do it. See what I posted.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -