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

Friday, March 30, 2012

question with trigger

i have a table which has columns scheduledrent,actualrent beside other columns.
the actual rent is based on some calculations but uses scheduledrent for the calculations.

everytime there is a change to scheduledrent, the actualrent should also change. so if i write a trigger saying
if updated(scheduledrent)
do some calculations and change the actualrent..using an update stmt.

would it make the change only for that row or for all the rows in the table? if it does it for all the rows, how do i make it apply only to that row. how should i have my update stmt in such a case. my primarykey is a combination of 4 rows. so can i get all the 4 values into some variables and while i do the update do it on that condition ? or is there any easier way of doing it.

thanks.
D.Inside the trigger code you have access to a logical table named "inserted". You can run your update logic with that table and it will only update the rows that were updated. The logical table "inserted" is available for update triggers and, naturally enough, insert triggers.

The logical table "deleted" is available for delete triggers and shows the records to be deleted. In an update trigger the "deleted" table shows the original values before they are to be changed.|||ok it works if i change it to inserted.
have one q though. i have 10 ppl accessing the db at any time. so if all of them make an update to the db (not to the same record though) each one will have an entry in the logical inserted table. so how would the system differentiate between them, since they all login to sql server through the same asp.net account.

thanks for the tip.|||The inserted logical table will only be for the record(s) that triggered that specific update. So everyone will have their own inserted logical tables and won't be stepping on one another.|||oh so sql server creates an inserted table for each user ?
but how does the sql server differentiate between users ?

lets say userA logged onto mysite and made an update to a row. the row gets inserted into the inserted table. userB also logs in at the same time and makes an update to another row. the new row also gets into the inserted table. now both these users are logged into the sqlserver through the same ASP.NET account. so how does sql server differentiate betwen userA and userB and their updated rows?

thanks again.|||It's a "logical" table meaning that it acts like a table but isn't really one. It is there to allow the trigger code to inspect the state of the old and new records so that the trigger code can take whatever action is appropriate. The logical table inside one trigger event isn't visible or accessible to the logical table in any other trigger event, even for the same user logged on multiple times simultaneously updating different record(s).|||i dont think i have understood completely. do you know any article that xplains in detail how the inserted table actually works...?
thanks anyway. will leave it for now. hope it works. will get back incase i have any prb.

thanks McMurdoStation.sql

Friday, March 23, 2012

question on trigger/procedure plz reply?

Can it be possible to create trigger/procedure
in following case
2 server server A and server B
A has db1
db1 has tblA

B has db2
db2 has tblB

can it be possible to create trigger on server A.db1.tblA as
insert/update that trigger add/update record in server B.db2.tb2

thnks
----
Hitendra(hitendra15@.gmail.com) writes:
> Can it be possible to create trigger/procedure
> in following case
> 2 server server A and server B
> A has db1
> db1 has tblA
> B has db2
> db2 has tblB
> can it be possible to create trigger on server A.db1.tblA as
> insert/update that trigger add/update record in server B.db2.tb2

Yes, you can set up linked servers with sp_addlinkedserver, and
then use four-part notation to refer to B.db2.dbo.tb2. You will
have to have DTS running on both servers, for it two work.

Notice that this arrangement is likely to have some impact on
concurrency on A.db1.dbo.tblA, as the transaction will run
considerably longer than it would, if the trigger had updated a
local table.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns961E39EF36BBYazorman@.127.0.0.1...
> (hitendra15@.gmail.com) writes:
>> Can it be possible to create trigger/procedure
>> in following case
>> 2 server server A and server B
>> A has db1
>> db1 has tblA
>>
>> B has db2
>> db2 has tblB
>>
>> can it be possible to create trigger on server A.db1.tblA as
>> insert/update that trigger add/update record in server B.db2.tb2
> Yes, you can set up linked servers with sp_addlinkedserver, and
> then use four-part notation to refer to B.db2.dbo.tb2. You will
> have to have DTS running on both servers, for it two work.
> Notice that this arrangement is likely to have some impact on
> concurrency on A.db1.dbo.tblA, as the transaction will run
> considerably longer than it would, if the trigger had updated a
> local table.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

I think you mean you need DTC running on both servers - that one letter
makes a big difference... :-)

Simon|||Simon Hayes (sql@.hayes.ch) writes:
> I think you mean you need DTC running on both servers - that one letter
> makes a big difference... :-)

You're so right, Simon!

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp