Showing posts with label triggers. Show all posts
Showing posts with label triggers. Show all posts

Wednesday, March 21, 2012

RI Question: Declarative vs Trigger

I am using SQL Server 7, 2000 and 2005.
All of my cascading RI is handled via triggers (generated by data modeller).
I only use triggers for RI and they implement simple cascading or setting
null.
From a performance point of view (100 million plus recs in a table), is it
better to use declarative RI or trigger RI?
The reason I ask is because I currently still support SQL 7 and I would like
to avoid separate app versions of the database per SQL Server version. But
if performance is better, I will consider separate app db versions.
Isaac Alexander wrote:
> I am using SQL Server 7, 2000 and 2005.
> All of my cascading RI is handled via triggers (generated by data modeller).
> I only use triggers for RI and they implement simple cascading or setting
> null.
> From a performance point of view (100 million plus recs in a table), is it
> better to use declarative RI or trigger RI?
> The reason I ask is because I currently still support SQL 7 and I would like
> to avoid separate app versions of the database per SQL Server version. But
> if performance is better, I will consider separate app db versions.
Isaac,
Usually RI performs much better than triggers. Also be aware that in
some cases triggers just do not fire, for instance
- TRUNCATE TABLE does not fire delete triggers
- nested and recursive triggers settings may cause triggers to not
fire
- triggers that work under READ COMMITTED isolation level may silently
fail under READ COMMITTED SNAPSHOT
So triggers are not 100% watertight.
On top of that, the optimizer may use RI as additional information and
come up with a better plan - it does not look into triggers.
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/
|||"Alex Kuznetsov" <AK_TIREDOFSPAM@.hotmail.COM> wrote in message
news:1165947132.769112.294310@.f1g2000cwa.googlegro ups.com...
> Isaac Alexander wrote:
> Isaac,
> Usually RI performs much better than triggers. Also be aware that in
> some cases triggers just do not fire, for instance
> - TRUNCATE TABLE does not fire delete triggers
> - nested and recursive triggers settings may cause triggers to not
> fire
> - triggers that work under READ COMMITTED isolation level may silently
> fail under READ COMMITTED SNAPSHOT
> So triggers are not 100% watertight.
> On top of that, the optimizer may use RI as additional information and
> come up with a better plan - it does not look into triggers.
>
Thanks. I was hoping that I got this answer.

RI Question: Declarative vs Trigger

I am using SQL Server 7, 2000 and 2005.
All of my cascading RI is handled via triggers (generated by data modeller).
I only use triggers for RI and they implement simple cascading or setting
null.
From a performance point of view (100 million plus recs in a table), is it
better to use declarative RI or trigger RI?
The reason I ask is because I currently still support SQL 7 and I would like
to avoid separate app versions of the database per SQL Server version. But
if performance is better, I will consider separate app db versions.Isaac Alexander wrote:
> I am using SQL Server 7, 2000 and 2005.
> All of my cascading RI is handled via triggers (generated by data modeller).
> I only use triggers for RI and they implement simple cascading or setting
> null.
> From a performance point of view (100 million plus recs in a table), is it
> better to use declarative RI or trigger RI?
> The reason I ask is because I currently still support SQL 7 and I would like
> to avoid separate app versions of the database per SQL Server version. But
> if performance is better, I will consider separate app db versions.
Isaac,
Usually RI performs much better than triggers. Also be aware that in
some cases triggers just do not fire, for instance
- TRUNCATE TABLE does not fire delete triggers
- nested and recursive triggers settings may cause triggers to not
fire
- triggers that work under READ COMMITTED isolation level may silently
fail under READ COMMITTED SNAPSHOT
So triggers are not 100% watertight.
On top of that, the optimizer may use RI as additional information and
come up with a better plan - it does not look into triggers.
--
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/|||"Alex Kuznetsov" <AK_TIREDOFSPAM@.hotmail.COM> wrote in message
news:1165947132.769112.294310@.f1g2000cwa.googlegroups.com...
> Isaac Alexander wrote:
>> I am using SQL Server 7, 2000 and 2005.
>> All of my cascading RI is handled via triggers (generated by data
>> modeller).
>> I only use triggers for RI and they implement simple cascading or setting
>> null.
>> From a performance point of view (100 million plus recs in a table), is
>> it
>> better to use declarative RI or trigger RI?
>> The reason I ask is because I currently still support SQL 7 and I would
>> like
>> to avoid separate app versions of the database per SQL Server version.
>> But
>> if performance is better, I will consider separate app db versions.
> Isaac,
> Usually RI performs much better than triggers. Also be aware that in
> some cases triggers just do not fire, for instance
> - TRUNCATE TABLE does not fire delete triggers
> - nested and recursive triggers settings may cause triggers to not
> fire
> - triggers that work under READ COMMITTED isolation level may silently
> fail under READ COMMITTED SNAPSHOT
> So triggers are not 100% watertight.
> On top of that, the optimizer may use RI as additional information and
> come up with a better plan - it does not look into triggers.
>
Thanks. I was hoping that I got this answer.

RI Question: Declarative vs Trigger

I am using SQL Server 7, 2000 and 2005.
All of my cascading RI is handled via triggers (generated by data modeller).
I only use triggers for RI and they implement simple cascading or setting
null.
From a performance point of view (100 million plus recs in a table), is it
better to use declarative RI or trigger RI?
The reason I ask is because I currently still support SQL 7 and I would like
to avoid separate app versions of the database per SQL Server version. But
if performance is better, I will consider separate app db versions.Isaac Alexander wrote:
> I am using SQL Server 7, 2000 and 2005.
> All of my cascading RI is handled via triggers (generated by data modeller
).
> I only use triggers for RI and they implement simple cascading or setting
> null.
> From a performance point of view (100 million plus recs in a table), is it
> better to use declarative RI or trigger RI?
> The reason I ask is because I currently still support SQL 7 and I would li
ke
> to avoid separate app versions of the database per SQL Server version. But
> if performance is better, I will consider separate app db versions.
Isaac,
Usually RI performs much better than triggers. Also be aware that in
some cases triggers just do not fire, for instance
- TRUNCATE TABLE does not fire delete triggers
- nested and recursive triggers settings may cause triggers to not
fire
- triggers that work under READ COMMITTED isolation level may silently
fail under READ COMMITTED SNAPSHOT
So triggers are not 100% watertight.
On top of that, the optimizer may use RI as additional information and
come up with a better plan - it does not look into triggers.
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/|||"Alex Kuznetsov" <AK_TIREDOFSPAM@.hotmail.COM> wrote in message
news:1165947132.769112.294310@.f1g2000cwa.googlegroups.com...
> Isaac Alexander wrote:
> Isaac,
> Usually RI performs much better than triggers. Also be aware that in
> some cases triggers just do not fire, for instance
> - TRUNCATE TABLE does not fire delete triggers
> - nested and recursive triggers settings may cause triggers to not
> fire
> - triggers that work under READ COMMITTED isolation level may silently
> fail under READ COMMITTED SNAPSHOT
> So triggers are not 100% watertight.
> On top of that, the optimizer may use RI as additional information and
> come up with a better plan - it does not look into triggers.
>
Thanks. I was hoping that I got this answer.

rewrite triggers as stored procedures

Could someone please help me rewrite these triggers as stored procedures.

Thanks

Trigger 1

ALTER TRIGGER trPaidInvoices

ON dbo.Invoices

FOR UPDATE

AS

IF UPDATE (InvoiceTotal)

insert into ClosedInvoices (InvoiceID,CustomerID,[Date])

SELECT InvoiceID, CustomerID, [Date]

FROM Invoices

WHERE (InvoiceTotal = 0.00)

Delete from invoices where invoiceTotal=0.00

select * from Invoices

Trigger 2

ALTER TRIGGER trInvoiceDetails_IU_UpdateMiscQty

ON dbo.InvoiceDetails

FOR INSERT, UPDATE

AS

/* IF UPDATE () ...*/

IF @.@.ROWCOUNT = 0

RETURN

IF UPDATE (Qty)

UPDATE MiscInventory

SET MiscInventory.InStock = ( MiscInventory.InStock - i.Qty )

FROM inserted i

JOIN MiscInventory

ON i.ItemID = MiscInventory.ItemID and i.ItemNumber=MiscInventory.ItemNumber

Trigger 3

ALTER TRIGGER trPayment_UD_InvoiceTotal

ON dbo.Payments

FOR INSERT, UPDATE

AS

IF UPDATE (Payment)

UPDATE Invoices

SET Invoices.InvoiceTotal= ( Invoices.InvoiceTotal - i.Payment )

FROM inserted i

JOIN Invoices

ON i.InvoiceID = Invoices.InvoiceID

Why do you want to do that?

You won't be able to access inserted/deleted in an SP but you can insert the contents into a temp table and access that.

Your trPaidInvoices is odd - it works on the whole table and does a select at the end!

Also does this work? If it's an update you should reverse the old value and add the new unless the update is always frmo 0.

Does if update work for an insert? I think it doesn't catch one of insert and delete not suer which.

Also what if someone updates to the same value?

|||

I was told it was "safer" to use sp instead of triggers.

The triggers work fine actually just want to know how to do the same with sp's

( the select on the trPaidInvoices is just my typo copied from the designer)

Cam

Saturday, February 25, 2012

returning status periodically from stored procedure

I'm fairly new at anything ambitious in SQL. I can set up tables and create
basic stored procedures, triggers and such. I know my way around ok, but
there is a lot to learn. I'm just curious about something. Nothing to show
you in particular becuase it's not been started but I want to know if 2
things are possible with stored procedures.
1. Can a stored procedure pass a result to the front end more than once
during execution? I want to write a procedure that does some processing that
might take more than just a few seconds. I want the front end to display a
status bar. If the procedure has say 5 steps in it, can I send out a result
at the end of each step so I can increment my front end status bar. If not,
other than doing the processing on the front end, what is the standard way
to accomplish this?
2. Use the same situation above. But now I'd like to have a cancel button on
a form in the front end so that if a backend process takes too long the use
can cancel it before it completes. Is there a way to cancel a stored
procedure from the front end while that procedure is executing?
Thanks,
Keith1.SP are executed as a whole or not, so a status bar is not possible, you
can fire your single queries to your db (each in a seperate SP) and use the
statusbar for that.
2. Like in the above situation, executing a procedure will force some
applications to wait for the execution until the result gets back. Sure
there is a possibilities to use different threads, but I dont think that
you mean that. There will be a chang in VS.NET 2005 to fire up queries
asynchronly but I dont know about anything in other Data Providers about
it.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Keith G Hicks" <krh@.comcast.net> schrieb im Newsbeitrag
news:uAPJ7AlWFHA.2520@.TK2MSFTNGP09.phx.gbl...
> I'm fairly new at anything ambitious in SQL. I can set up tables and
> create
> basic stored procedures, triggers and such. I know my way around ok, but
> there is a lot to learn. I'm just curious about something. Nothing to show
> you in particular becuase it's not been started but I want to know if 2
> things are possible with stored procedures.
> 1. Can a stored procedure pass a result to the front end more than once
> during execution? I want to write a procedure that does some processing
> that
> might take more than just a few seconds. I want the front end to display a
> status bar. If the procedure has say 5 steps in it, can I send out a
> result
> at the end of each step so I can increment my front end status bar. If
> not,
> other than doing the processing on the front end, what is the standard way
> to accomplish this?
> 2. Use the same situation above. But now I'd like to have a cancel button
> on
> a form in the front end so that if a backend process takes too long the
> use
> can cancel it before it completes. Is there a way to cancel a stored
> procedure from the front end while that procedure is executing?
> Thanks,
> Keith
>|||Just as I suspected. Thanks for the quick response. -keith :)
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in
message news:OEWtnKlWFHA.3140@.TK2MSFTNGP14.phx.gbl...
1.SP are executed as a whole or not, so a status bar is not possible, you
can fire your single queries to your db (each in a seperate SP) and use the
statusbar for that.
2. Like in the above situation, executing a procedure will force some
applications to wait for the execution until the result gets back. Sure
there is a possibilities to use different threads, but I dont think that
you mean that. There will be a chang in VS.NET 2005 to fire up queries
asynchronly but I dont know about anything in other Data Providers about
it.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Keith G Hicks" <krh@.comcast.net> schrieb im Newsbeitrag
news:uAPJ7AlWFHA.2520@.TK2MSFTNGP09.phx.gbl...
> I'm fairly new at anything ambitious in SQL. I can set up tables and
> create
> basic stored procedures, triggers and such. I know my way around ok, but
> there is a lot to learn. I'm just curious about something. Nothing to show
> you in particular becuase it's not been started but I want to know if 2
> things are possible with stored procedures.
> 1. Can a stored procedure pass a result to the front end more than once
> during execution? I want to write a procedure that does some processing
> that
> might take more than just a few seconds. I want the front end to display a
> status bar. If the procedure has say 5 steps in it, can I send out a
> result
> at the end of each step so I can increment my front end status bar. If
> not,
> other than doing the processing on the front end, what is the standard way
> to accomplish this?
> 2. Use the same situation above. But now I'd like to have a cancel button
> on
> a form in the front end so that if a backend process takes too long the
> use
> can cancel it before it completes. Is there a way to cancel a stored
> procedure from the front end while that procedure is executing?
> Thanks,
> Keith
>|||I don't think that this is true, not in the strictest of sense. True what
he is suggesting is not a good idea, but certainly result sets or error
messages can be returned, and parts of procs can be completed or ignored
based on what happens in the procedure.
As an example, consider error messages. They have to be answered before the
procedure continues on. Same with count messages. If you return 2 result
sets, you can stop retrieving data and never get to the second call (and
subsequently not ever run a statement that is more important, like a commit
transaction. This bit me once in a pooled connection app. Had to roll back
several hours of work!) In fact, if you don't retrieve all of the rows from
the first result set (if there are > 1) then it will never get to the
second.
Even in 2005, a single query batch will be synchronous, but you can run two
different batches asynch on the same connection (or a reasonable facimile
thereof.)
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> wrote in
message news:OEWtnKlWFHA.3140@.TK2MSFTNGP14.phx.gbl...
> 1.SP are executed as a whole or not, so a status bar is not possible, you
> can fire your single queries to your db (each in a seperate SP) and use
> the statusbar for that.
> 2. Like in the above situation, executing a procedure will force some
> applications to wait for the execution until the result gets back. Sure
> there is a possibilities to use different threads, but I dont think that
> you mean that. There will be a chang in VS.NET 2005 to fire up queries
> asynchronly but I dont know about anything in other Data Providers about
> it.
> --
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
> "Keith G Hicks" <krh@.comcast.net> schrieb im Newsbeitrag
> news:uAPJ7AlWFHA.2520@.TK2MSFTNGP09.phx.gbl...
>|||Basically, you would return multiple resultsets, and the ADO command would
need to be called asynchronously in order to update a status and allow
cancellation. Never done this before, so I'm not sure about the details.
When you get it working, then post back with your final solution.
"Keith G Hicks" <krh@.comcast.net> wrote in message
news:uAPJ7AlWFHA.2520@.TK2MSFTNGP09.phx.gbl...
> I'm fairly new at anything ambitious in SQL. I can set up tables and
create
> basic stored procedures, triggers and such. I know my way around ok, but
> there is a lot to learn. I'm just curious about something. Nothing to show
> you in particular becuase it's not been started but I want to know if 2
> things are possible with stored procedures.
> 1. Can a stored procedure pass a result to the front end more than once
> during execution? I want to write a procedure that does some processing
that
> might take more than just a few seconds. I want the front end to display a
> status bar. If the procedure has say 5 steps in it, can I send out a
result
> at the end of each step so I can increment my front end status bar. If
not,
> other than doing the processing on the front end, what is the standard way
> to accomplish this?
> 2. Use the same situation above. But now I'd like to have a cancel button
on
> a form in the front end so that if a backend process takes too long the
use
> can cancel it before it completes. Is there a way to cancel a stored
> procedure from the front end while that procedure is executing?
> Thanks,
> Keith
>