Showing posts with label alter. Show all posts
Showing posts with label alter. Show all posts

Tuesday, March 27, 2012

DELETE and UPDATE Trigger question

Hello
I have a Trigger on a table. Here is the code
ALTER TRIGGER [dbo].[OnOrderDelete]ON [dbo].[orders] AFTERDELETE,UPDATEAS BEGINSET NOCOUNT ON;DECLARE @.idsint;SELECT @.ids =(SELECT idfrom DELETED);DELETE FROM filesWHERE OrderId = @.ids;END

Actually the UPDATE event handler is not wanted here, but why when I leave him I have a following behaviour:
When orders table is updated, the

"SELECT @.ids =(SELECT idfrom DELETED);DELETE FROM filesWHERE OrderId = @.ids;"

part is executed, and the program recognizes DELETED as UPDATED! (Like " SELECT @.ids =(SELECT idfromUPDATED) ")

Is this right? And how can I part UPDATED and DELETED ?

Thanks
Artashes

Because an update is logically a delete and insert combined into one atomic transaction.

And just an FYI - your trigger will fail if you ever try to delete multiple rows with a single statement. You should try:

DELETE FROM files where OrderID IN (SELECT ID FROM deleted)

|||

Thanks for information!

Your way of deleting seems to be more correct!

But you know, my query works!!

Is there any explanation?

|||

Motley has given the explanation. In SQL Server there is no actually 'UPDATE' as you may expect just some modification on a row, instead an UPDATE consists an INSERT followed by a DELETE, so that's why you felt that DELETED is treated as UPDATED.

|||

Iori_Jay, about DELETE and INSERT everything is clear.

I mean why works my query

DECLARE @.idsint;SELECT @.ids =(SELECT idfrom DELETED);DELETE FROM filesWHERE OrderId = @.ids;

Which as Motley said, should not work? (Because I try to delete multiple rows with a single statement)
As he said, I must write

DELETE FROM fileswhere OrderIDIN (SELECT IDFROM deleted);
Confused|||

Try executing DELETE FROM files.

With your trigger, it will fail.

|||

Motley, I don't understand you!

And what am I executing now?

Or you mean only just "DELETE FROM files;" (delete all rows in files?)

|||Yes, if you used the trigger you originally had and tried to delete all the rows in files, the trigger would throw an error. Any delete that caused multiple rows to be deleted would fail.|||

I don't know about all files, but there were situations, when my query deleted 3 rows!

|||

Not within a single statement.

DELETE from files where fileid=1

DELETE from files where fileid=2

would work, but...

DELETE from files where fileid=1 or fileid=2

would fail.

|||

Thanks for info!

But there is no statement like "DELETE from files where fileid=1 or fileid=2" in my query? Am I right?

|||

artashes:

Iori_Jay, about DELETE and INSERT everything is clear.

I mean why works my query

DECLARE @.idsint;SELECT @.ids =(SELECT idfrom DELETED);DELETE FROM filesWHERE OrderId = @.ids;

Which as Motley said, should not work? (Because I try to delete multiple rows with a single statement)
As he said, I must write

DELETE FROM fileswhere OrderIDIN (SELECT IDFROM deleted);

Confused

I can't understand why such query works even when you're trying to delete multiple rows--the subquery will return multiple rows and "SELECT @.ids =(SELECT idfrom DELETED)" command will fail as it is trying to assign multiple values to a single variable.However the following query will work as it assigns the last id from deleted table to the variable:

DECLARE @.idsint;
SELECT @.ids =idfrom DELETED;
DELETE FROM filesWHERE OrderId = @.ids;

|||Smile If you wan't I can send you a project where it works, and deletes NOT only the last deleted id.

Wednesday, March 7, 2012

Defining Alert for System Errors

Hi,
I want to define alert for system error messages but it seems that we cannot
alter these messages like SQL Server 2000 to be logged in Windows events.
For example error #208, is it possible?
Thanks in advance,
LeilaNo, we cannot change whether system errors are written to eventlog or not in 2005, I'm afraid. I
guess you have to look for some external utility which monitors the eventlog. The good news is that
the messages in EventLog now has the same Event number as the SQL Server error number (which makes
it easier to write code that reads the eventlog and acts on certain errors).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Leila" <Leilas@.hotpop.com> wrote in message news:Ov$2kalOHHA.1248@.TK2MSFTNGP02.phx.gbl...
> Hi,
> I want to define alert for system error messages but it seems that we cannot alter these messages
> like SQL Server 2000 to be logged in Windows events. For example error #208, is it possible?
> Thanks in advance,
> Leila
>|||Whay if an application is upgraded from SQL Server 2000 to 2005! The
backward compatibility is missed!? It was very easy to define this alert in
previous version..
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:eEExMvlOHHA.4244@.TK2MSFTNGP04.phx.gbl...
> No, we cannot change whether system errors are written to eventlog or not
> in 2005, I'm afraid. I guess you have to look for some external utility
> which monitors the eventlog. The good news is that the messages in
> EventLog now has the same Event number as the SQL Server error number
> (which makes it easier to write code that reads the eventlog and acts on
> certain errors).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Leila" <Leilas@.hotpop.com> wrote in message
> news:Ov$2kalOHHA.1248@.TK2MSFTNGP02.phx.gbl...
>> Hi,
>> I want to define alert for system error messages but it seems that we
>> cannot alter these messages like SQL Server 2000 to be logged in Windows
>> events. For example error #208, is it possible?
>> Thanks in advance,
>> Leila
>|||I didn't check what happens with the "always log to eventlog" for system error on an upgraded SQL
Server, but most probably, this setting will not be upgraded (since the system part of sys.messages
isn't really a table anymore), so that will be lost.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Leila" <Leilas@.hotpop.com> wrote in message news:ee5LeBmOHHA.324@.TK2MSFTNGP06.phx.gbl...
> Whay if an application is upgraded from SQL Server 2000 to 2005! The backward compatibility is
> missed!? It was very easy to define this alert in previous version..
>
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
> news:eEExMvlOHHA.4244@.TK2MSFTNGP04.phx.gbl...
>> No, we cannot change whether system errors are written to eventlog or not in 2005, I'm afraid. I
>> guess you have to look for some external utility which monitors the eventlog. The good news is
>> that the messages in EventLog now has the same Event number as the SQL Server error number (which
>> makes it easier to write code that reads the eventlog and acts on certain errors).
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> "Leila" <Leilas@.hotpop.com> wrote in message news:Ov$2kalOHHA.1248@.TK2MSFTNGP02.phx.gbl...
>> Hi,
>> I want to define alert for system error messages but it seems that we cannot alter these
>> messages like SQL Server 2000 to be logged in Windows events. For example error #208, is it
>> possible?
>> Thanks in advance,
>> Leila
>>
>

Deferring constraint checking in a transaction

Hello,
Is there a way to defer constraint checking during a sql server database
data import transaction?
I can turn constraints off in sql with an alter table command, but this
requires adding the user to the db_ddladmin group which I'd rather they
didn't belong to.
Hi,
you can disable all / certain constraint defined on tables:
http://sqljunkies.com/WebLog/roman/a...1/30/7037.aspx
-- Disable all table constraints
ALTER TABLE MyTable NOCHECK CONSTRAINT ALL
-- Enable all table constraints
ALTER TABLE MyTable CHECK CONSTRAINT ALL
-- Disable single constraint
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint
-- Enable single constraint
ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
"shmeian" <shmeian@.discussions.microsoft.com> schrieb im Newsbeitrag
news:BC396313-25DB-45FA-A3D2-265109246024@.microsoft.com...
> Hello,
> Is there a way to defer constraint checking during a sql server database
> data import transaction?
> I can turn constraints off in sql with an alter table command, but this
> requires adding the user to the db_ddladmin group which I'd rather they
> didn't belong to.
>

Deferring constraint checking in a transaction

Hello,
Is there a way to defer constraint checking during a sql server database
data import transaction?
I can turn constraints off in sql with an alter table command, but this
requires adding the user to the db_ddladmin group which I'd rather they
didn't belong to.Hi,
you can disable all / certain constraint defined on tables:
http://sqljunkies.com/WebLog/roman/...01/30/7037.aspx
-- Disable all table constraints
ALTER TABLE MyTable NOCHECK CONSTRAINT ALL
-- Enable all table constraints
ALTER TABLE MyTable CHECK CONSTRAINT ALL
-- Disable single constraint
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint
-- Enable single constraint
ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"shmeian" <shmeian@.discussions.microsoft.com> schrieb im Newsbeitrag
news:BC396313-25DB-45FA-A3D2-265109246024@.microsoft.com...
> Hello,
> Is there a way to defer constraint checking during a sql server database
> data import transaction?
> I can turn constraints off in sql with an alter table command, but this
> requires adding the user to the db_ddladmin group which I'd rather they
> didn't belong to.
>