Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Tuesday, March 27, 2012

Does SQL Server 2005 exsit the trigger ?

When the database starts,I want to startup a trigger to do something,how can I do ?

I know there are two kinds of trigger in SQL Server 2005 ,one is DML trigger and the another is DDL trigger

Could you tell me ,Is there a trigger for database level in SQL Server 2005 ?

I know there is this kind of trigger in oracle .

Sure, in SQL 2005 you can create DDL triigers on DATABASE (even SERVER), please take a look at this link:
CREATE TRIGGER (Transact-SQL)|||

Hi,friend

I just want to know whether the instance started or not.

If the instance started ,I would monitor a table ,the trigger is not the kind of DDL

|||OK, how will you monitor the table? Then you can create a sp to "monitor" the table, and configure it as startup stored procedure using?sp_procoption.?You?can take a look at this link:
Automatic Execution of Stored Procedures

Wednesday, March 21, 2012

does not match with a table name or alias name used in the query

I am trying to create a trigger on a table. The idea is
that the trigger will stop duplicates being entered but
will allow null values. I am trying to use the inserted
table but am receiving the following error.
The column prefix 'T2' does not match with a table name
or alias name used in the query.
The table and trigger creation script follows.
Thanks in advance.
========= Table
=========
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[Class]
GO
CREATE TABLE [dbo].[Class] (
[ClassID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (60) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Symbol] [T_STD_SYMBOL] NULL ,
[ClassTypeID] [int] NOT NULL ,
[Description] [varchar] (40) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
========= Trigger
=========
if exists (select 1
from sysobjects
where id = object_id('uqSymbol_class')
and type = 'TR')
drop trigger uqSymbol_class
go
/* Trigger to stop duplicate Symbols being entered in
the Symbol column in the class table. */
create trigger uqSymbol_class on Class for insert, update
as
begin
declare
@.numrows int,
@.errno int,
@.errmsg varchar(255)
select @.numrows = @.@.rowcount
if @.numrows = 0
return
/* Check to see if the inserted updated symbol
already exists */
if update(symbol)
begin
-- Check weather the Symbol value is Null.
Ignore if so.
-- if ((select symbol from inserted) != null)
begin -- IF((SELECT COUNT(*) FROM INSERTED WHERE
Symbol IS NOT NULL) > 0)
if (select count(*)
from Class t1, inserted t2
where t1.symbol = t2.symbol) >1 AND
T2.Symbol IS NOT NULL
begin
select @.errno = 30003,
@.errmsg = 'The Symbol you have
entered or just updated already exists.'
goto error
end
end
-- end
return
/* Error handling */
error:
raiserror @.errno @.errmsg
rollback transaction
end
ThanX :-)You are not giving us any code which uses t2 as an alias... please repost
complete code...
also if there are ever multiple triggers, and one of the other triggers does
a select insert update or delete, then @.@.rowcount in this trigger will not
be correct. It is probably safer to count records in inserted and deleted...
"Jamie" <jamie.downs@.risk.sungard.com> wrote in message
news:0d4101c360b0$976a42e0$a101280a@.phx.gbl...
> I am trying to create a trigger on a table. The idea is
> that the trigger will stop duplicates being entered but
> will allow null values. I am trying to use the inserted
> table but am receiving the following error.
> The column prefix 'T2' does not match with a table name
> or alias name used in the query.
> The table and trigger creation script follows.
> Thanks in advance.
> =========> Table
> =========> if exists (select * from dbo.sysobjects where id => object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id,
> N'IsUserTable') = 1)
> drop table [dbo].[Class]
> GO
> CREATE TABLE [dbo].[Class] (
> [ClassID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [varchar] (60) COLLATE
> SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [Symbol] [T_STD_SYMBOL] NULL ,
> [ClassTypeID] [int] NOT NULL ,
> [Description] [varchar] (40) COLLATE
> SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
>
> =========> Trigger
> =========> if exists (select 1
> from sysobjects
> where id = object_id('uqSymbol_class')
> and type = 'TR')
> drop trigger uqSymbol_class
> go
> /* Trigger to stop duplicate Symbols being entered in
> the Symbol column in the class table. */
> create trigger uqSymbol_class on Class for insert, update
> as
> begin
> declare
> @.numrows int,
> @.errno int,
> @.errmsg varchar(255)
> select @.numrows = @.@.rowcount
> if @.numrows = 0
> return
>
> /* Check to see if the inserted updated symbol
> already exists */
> if update(symbol)
> begin
> -- Check weather the Symbol value is Null.
> Ignore if so.
> -- if ((select symbol from inserted) != null)
>
----
--
> ThanX :-)|||You missed out the relevant bit of code.
In fact you don't need a trigger to do this. You can use an indexed view to
enforce uniqueness only for non-NULL values:
CREATE VIEW Symbols
WITH SCHEMABINDING
AS
SELECT symbol
FROM dbo.class
WHERE symbol IS NOT NULL
GO
CREATE UNIQUE CLUSTERED INDEX uclsymbol ON Symbols (symbol)
--
David Portas
--
Please reply only to the newsgroup
--|||Hi Jamie,
Posting messages with attachments usually isn't very useful because most
people do not trust them, won't open them and so don't have enough
information to answer your question. Post everything in plain text instead.
If you are on SQL Server 2000 you don't have to use a trigger to achieve
this, but you can instead create an indexed view to check for duplicates:
CREATE VIEW Class_Symbol_check
AS
SELECT Symbol FROM Class
WHERE Symbol IS NOT NULL
GO
CREATE UNIQUE INDEX ON Class_Symbol_check (Symbol )
GO
Read the topic "Indexed views" in Books online for more information.
The fact that a UNIQUE index can only have one NULL (otherwise you could
just create a unique constraint on the Symbol column in the class table) is
a problem in SQL Server and one I sincerly hope will be addressed in the
next version.
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Jamie" <jamie.downs@.risk.sungard.com> wrote in message
news:0d4101c360b0$976a42e0$a101280a@.phx.gbl...
> I am trying to create a trigger on a table. The idea is
> that the trigger will stop duplicates being entered but
> will allow null values. I am trying to use the inserted
> table but am receiving the following error.
> The column prefix 'T2' does not match with a table name
> or alias name used in the query.
> The table and trigger creation script follows.
> Thanks in advance.
> =========> Table
> =========> if exists (select * from dbo.sysobjects where id => object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id,
> N'IsUserTable') = 1)
> drop table [dbo].[Class]
> GO
> CREATE TABLE [dbo].[Class] (
> [ClassID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [varchar] (60) COLLATE
> SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [Symbol] [T_STD_SYMBOL] NULL ,
> [ClassTypeID] [int] NOT NULL ,
> [Description] [varchar] (40) COLLATE
> SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
>
> =========> Trigger
> =========> if exists (select 1
> from sysobjects
> where id = object_id('uqSymbol_class')
> and type = 'TR')
> drop trigger uqSymbol_class
> go
> /* Trigger to stop duplicate Symbols being entered in
> the Symbol column in the class table. */
> create trigger uqSymbol_class on Class for insert, update
> as
> begin
> declare
> @.numrows int,
> @.errno int,
> @.errmsg varchar(255)
> select @.numrows = @.@.rowcount
> if @.numrows = 0
> return
>
> /* Check to see if the inserted updated symbol
> already exists */
> if update(symbol)
> begin
> -- Check weather the Symbol value is Null.
> Ignore if so.
> -- if ((select symbol from inserted) != null)
>
----
--
> ThanX :-)|||> a problem in SQL Server and one I sincerly hope will be addressed in the
> next version.
Full SQL92 constraints would solve this problem and more. I hope Yukon will
support the ANSI-style constraints.
--
David Portas
--
Please reply only to the newsgroup
--|||Yeah,
In the all the info I have read and heard about Yukon there is a lot of talk
about CLR support, and only very general remarks about improvements in
T-SQL. I guess the marketing people at Microsoft think that (full ANSI
constraints etc) is "hard core SQL" and too difficult for 95% of the
developers, and they are probably right. Euan Garden (Program manager for
SQL Server) said during a presentation "that people think that Microsoft
will drop support for T-SQL now that there's CLR support, but that's not the
case", so that tells you the level they are targeting. </rant>
Of the new SQL features in Yukon that I know of I like the idea in Yukon
that you can write your own datatypes, I just don't like the idea that you
have to write them in a CLR language instead of being able to declare them
in SQL, although I expect that there will soon be a cottage industry in
datatypes for SQL Server (postcodes, telephone numbers, credit card numbers,
ISBN etc), just like there was with ActiveX controls for VB 6.
But there is little or no information on structural improvement to T-SQL and
further compliance with ANSI standards. Do we get ANSI style constraints?
row constructors? ...?
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:#ZqfnQLYDHA.1004@.TK2MSFTNGP12.phx.gbl...
> > a problem in SQL Server and one I sincerly hope will be addressed in the
> > next version.
> Full SQL92 constraints would solve this problem and more. I hope Yukon
will
> support the ANSI-style constraints.
> --
> David Portas
> --
> Please reply only to the newsgroup
> --
>
>|||Thanks for all your help. I have implemented the view as
the trigger.
Jacco, what did you mean by CLR.
Thanks again.

Sunday, February 26, 2012

Does a trigger fire once for a set statement

Does a trigger fire once for a set statement, or for every row affected? I
presume that using a client like Access and doing a line by line manual
adjustment, would fire a trigger for each transaction, but does a set
statement (like below) fire the trigger once?
eg
Table_A (column_a, column_b) with these values:
aa, 0.00
aa, 0.00
bb, 0.00
bb, 0.00
Trigger for update, insert on Table_A
if update(column_a)
update Table_A
set
column_b = 1
If I then run a sql statement to update Table_A, will the trigger fire after
each affected row, or simply fire once for the set statement below?
update table_A
set
colum_a = cc
where column_a = 'aa'
Would the trigger update the whole table once, or would it update all of
column_b twice, because of the two aa's.?
I've googled, but not very well as I haven't hit the answer yet...Any
knowledge on this?
Many thanks.
Steve.Steve'o wrote:
> Does a trigger fire once for a set statement, or for every row affected?
The trigger is fired once for each statement. This means that if your
statement affects multiple rows it is still only executed one time.
For more information look at the "CREATE TRIGGER" documentation in Books
Online.
http://msdn.microsoft.com/library/e...reate2_7eeq.asp
Aaron Weiker
http://aaronweiker.com/
http://www.sqlprogrammer.org/|||Hi
Also, look at the virtual tables INSERTED and DELETED to see which rows are
affected by your update.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Aaron Weiker" <aaron@.sqlprogrammer.org> wrote in message
news:O$vefqgDFHA.3648@.TK2MSFTNGP10.phx.gbl...
> Steve'o wrote:
> The trigger is fired once for each statement. This means that if your
> statement affects multiple rows it is still only executed one time.
> For more information look at the "CREATE TRIGGER" documentation in Books
> Online.
> http://msdn.microsoft.com/library/e...reate2_7eeq.asp
> --
> Aaron Weiker
> http://aaronweiker.com/
> http://www.sqlprogrammer.org/|||Many thanks, I think I may need to use a curosr then...
I have a table which stores a alpha+numeric sequence number. When a column
in another table is set to true, it fires a trigger to retrive the next
sequence number, then update the sequences table.
I kept it in a sequence table because there are several different sequences,
and this allows a bit of central control.
When updating a row at a time, this will work fine, but if I do a mass
update with a update table statement which affects many rows, the trigger
will only fire once... Hence why Im assuming a cursor is the way forward,
a
bit more reading to do ;)
Thanks for the link.
PS. Already using inserted and deleted in some triggers, just wasn't sure if
they fired for every line, thanks again to both replies.
"Aaron Weiker" wrote:

> Steve'o wrote:
> The trigger is fired once for each statement. This means that if your
> statement affects multiple rows it is still only executed one time.
> For more information look at the "CREATE TRIGGER" documentation in Books
> Online.
> http://msdn.microsoft.com/library/e...reate2_7eeq.asp
> --
> Aaron Weiker
> http://aaronweiker.com/
> http://www.sqlprogrammer.org/
>

Friday, February 24, 2012

Documenting triggers

I believe in avoiding triggers whenever possible. However, in the
circumstance that a trigger is the "right" solution, I'd be curious how
people document/diagram triggers so that a successor could actually find
them aside from just having a document that says "by the way, there are some
triggers on these tables ...". That's the type of document that will be
lost/forgot about in a hurry. Moreover, triggers don't fall nicely into the
graphically easy to find categories of tables, views, stored procedures,
etc..
Thanks in advance for your suggestions.
MarkThere is no way to do this in SQL Server (only through quering the
information_schmea /systables), but if you have Visio you can easily load
them from your database via Reverse Engineering. Thats the way a documenting
i use, because if something changes i just "reload" the database in Visio
and Voil.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Mark" <Mark@.nowhere.com> schrieb im Newsbeitrag
news:ue%23F8noSFHA.3188@.TK2MSFTNGP09.phx.gbl...
>I believe in avoiding triggers whenever possible. However, in the
>circumstance that a trigger is the "right" solution, I'd be curious how
>people document/diagram triggers so that a successor could actually find
>them aside from just having a document that says "by the way, there are
>some triggers on these tables ...". That's the type of document that will
>be lost/forgot about in a hurry. Moreover, triggers don't fall nicely into
>the graphically easy to find categories of tables, views, stored
>procedures, etc..
> Thanks in advance for your suggestions.
> Mark
>|||See if this helps:
Schema: How do I show all the triggers in a database?
http://www.aspfaq.com/show.asp?id=2105
AMB
"Mark" wrote:

> I believe in avoiding triggers whenever possible. However, in the
> circumstance that a trigger is the "right" solution, I'd be curious how
> people document/diagram triggers so that a successor could actually find
> them aside from just having a document that says "by the way, there are so
me
> triggers on these tables ...". That's the type of document that will be
> lost/forgot about in a hurry. Moreover, triggers don't fall nicely into t
he
> graphically easy to find categories of tables, views, stored procedures,
> etc..
> Thanks in advance for your suggestions.
> Mark
>
>