Posts: 20
Joined: Aug 2003
Mon November 08, 2004 9:42 AM
I have a table where from 1 to 3 fields will be update (lastname,firstname,birthdate). Any combination of the 3 can be updated. I need a trigger to handle the updates which update some other db tables. I can handle the update trigger part, the ? is how to best approach the logic. I can do thee separate statements to handle an update for each of the columns, but that could cause 3 separate updates to fire. I was looking at doing something like
If UPDATE(last_name)
and UPDATE(first_name)
and update(date_of_birth)
BEGIN
...
end
else if
If UPDATE(last_name)
and UPDATE(first_name)
begin
...
end
I am dealing with two tables and I am trying to take one column from a table and match the records with another table and append the data of that column.
I used an update query that looks like this:
UPDATE Acct_table Set Acct_table.Score = (Select Score_tbl.Score from Score_tbl Where Acct_table.Acctnb = Score_tbl.Acctnb
This process has been running for over an hour and a half and is building a large log file. I am curious to know if there is a better command that I can use in order to join the tables and then just drop the column from one to the other. Both tables are indexed on Acctnb.
I am facing a small problem while updating the warehouse using SSIS package.
Here is an sample scenario. I have a staging table T1 with some important columns as stateid,ename etc .. and target table as Trg with only two columns stateid and statecnt. The Rule is, each state will be sending an excel file which basically contains state and employees belonging to the state information. For the first time when i execute the SSIS package, i need to load statetid and count_of_employees for that particular state. say for example.
SELECT stateid, count(ename) from T1 where stateid = ?
For the next time if the same state information is received then we need to update the count to the existing record in the Dataware house rather than creating a brand new record once again.
I am little bit confused of implementig this logic. Can anyone help me out in accomplishing this task. What can done or what steps is involved in achiving the task.
We have a table X with a gender column taking values M(male) or F(Female).There are 52 rows in the table X.We want to update all the rows with Gender M as F and update all remaining F as males (M) in a single Update Query.Im looking for the exact logic we would write in the single update statement. Please help me out.
hi.I am having probelms with an update statement. every timei run it, "every" row updates, not just the one(s) intended.so, here is what i have. i have tried this with both AND and ORand neither seem to work.i dont know why this is elluding me, but i'd appreciate help with thesolution.thanks.UPDATE addSET add_s = 1WHERE add.add_status = 0 and add.add_email = 'mags23@rice.edu'or add_s in(SELECT a.add_sFROM add a, edit eWHERE a.email_address = e.email_addressand e.public_name = 'professor')
i need to update for example the eployee 1111 with employee 3333 but with swap ( take the value of employee 1111 in field- fld2,fld22,fld222 and swap value between employee 3333 in field- fld2,fld22,fld222 )
Code Snippet ---update eployee 1111 with employee 3333 -so if i put the value 2 than ------------------ swap value between 2 employee set empid1= 1111 set empid2=3333 value_swap=2 if value_swap=2 than update fld2,fld22,fld222 with fld2,fld22,fld222 ------------------- take the value of employee 1111 in field- fld2,fld22,fld222 and swap value between employee 3333 --------------------in field- fld2,fld22,fld222
Code Snippet ---update eployee 2222 with employee 1111 -so if i put the value 5 than ------------------ swap value between 2 employees set empid1= 1111 set empid2=2222 value_swap=5 if value_swap=5 than update fld5,fld55,fld555 with fld5,fld55,fld555 ------------------- take the value of employee 1111 in field- fld5,fld55,fld555 and swap value between employee 3333 --------------------in field- fld5,fld55,fld555
I have got a business logic update conflict handler working, but I have had to work round what appears to be a bug.
Please can someone confirm if this is indeed a bug €“ and if it is a known bug?
My conflict handler needs to take some columns from the publisher row and some from the subscriber row in the event of conflict.
I can quite happily generate a custom dataset which contains the winning row that I want €“ I can see that because I can step through the conflict handler with debug when a conflict occurs.
However, just returning ActionOnUpdateConflict.AcceptCustomConflictData from the UpdateConflictsHandler method does not set the publisher and subscriber columns correctly. I end up with different values on the two databases.
I have found that the only way to get the correct rows on both publisher and subscriber is to create a new ADO connection to the publisher and actually perform an update €“ updating all the modified columns. This now works reliably in my testing.
Fortunately, due to business rules the frequency of update conflicts are likely to be very infrequent, but I would very much like to avoid having to do the €˜unnecessary€™ update.
Notes:
I am using column level tracking €“ but I have seen the problem with row level tracking too I have mainly been using SP1 but have repeated the test on a configuration using the SP2 CTP €“ and the problem occurs there too The problem is not due to complex logic in my code. If the method just sets customDataSet = publisherDataSet.Copy and then returns ActionOnUpdateConflict.AcceptCustomConflictData, the changed and winning publisher values are not sent to the subscriber
Are there any limitations or gotchas to updating the same table whichfired a trigger from within the trigger?Some example code below. Hmmm.... This example seems to be workingfine so it must be something with my specific schema/code. We'reworking on running a SQL trace but if anybody has any input, fireaway.Thanks!create table x(Id int,Account varchar(25),Info int)GOinsert into x values ( 1, 'Smith', 15);insert into x values ( 2, 'SmithX', 25);/* Update trigger tu_x for table x */create trigger tu_xon xfor updateasbegindeclare @TriggerRowCount intset @TriggerRowCount = @@ROWCOUNTif ( @TriggerRowCount = 0 )returnif ( @TriggerRowCount > 1 )beginraiserror( 'tu_x: @@ROWCOUNT[%d] Trigger does not handle @@ROWCOUNT[color=blue]> 1 !', 17, 127, @TriggerRowCount) with seterror, nowait[/color]returnendupdate xsetAccount = left( i.Account, 24) + 'X',Info = i.Infofrom deleted, inserted iwhere x.Account = left( deleted.Account, 24) + 'X'endupdate x set Account = 'Blair', Info = 999 where Account = 'Smith'
I have a stored proc that contains an update which utilizes a case statement to populate values in a particular column in a table, based on values found in other columns within the same table. The existing update looks like this (object names and values have been changed to protect the innocent):
UPDATE dbo.target_table set target_column = case when source_column_1= 'ABC'then 'XYZ' when source_column_2= '123'then 'PDQ'
[Code] ....
The powers that be would like to replace this case statement with some sort of table-driven structure, so that the mapping rules defined above can be maintained in the database by the business owner, rather than having it embedded in code and thus requiring developer intervention to perform changes/additions to the rules.
The rules defined in the case statement are in a pre-defined sequence which reflects the order of precedence in which the rules are to be applied (in other words, if a matching value in source_column_1 is found, this trumps a conflicting matching value in source_column_2, etc). A case statement handles this nicely, of course, because the case statement will stop when it finds the first "hit" amongst the WHEN clauses, testing each in the order in which they are coded in the proc logic.
What I'm struggling with is how to replicate this using a lookup table of some sort and joins from the target table to the lookup to replace the above case statement. I'm thinking that I would need a lookup table that has column name/value pairings, with a sequence number on each row that designates the row's placement in the precedence hierarchy. I'd then join to the lookup table somehow based on column names and values and return the match with the lowest sequence number, or something to that effect.
I am currently working with C and SQL Server 2012. My requirement is to Bulk fetch the records and Insert/Update the same in the other table with some business logic? How do i do this?
I want to retrieve the last update time of database. Whenever any update or delete or insert happend to my database i want to store and retrieve that time.
I know one way is that i have to make a table that will store the datetime field and system trigger / trigger that can update this field record whenever any update insert or deletion occur in database.
But i don't know exactly how to do the coding for this?
I have a table where table row gets updated multiple times(each column will be filled) based on telephone call in data.  Initially, I have implemented after insert trigger on ROW level thinking that the whole row is inserted into table will all column values at a time. But the issue is all columns are values are not filled at once, but observed that while telephone call in data, there are multiple updates to the row (i.e multiple updates in the sense - column data in row is updated step by step),
I thought to implement after update trigger , but when it comes to the performance will be decreased for each and every hit while row update.
I need to implement after update trigger that should be fired on column level instead of Row level to improve the performance?
Hi, I am not sure if this is the right forum to post this question. I run an update statement like "Update mytable set status='S' " on the SQL 2005 management Studio. When I run "select * from mytable" for a few seconds all status = "S". After a few seconds all status turn to "H". This is a behaviour when you have an update trigger for the table. But I don't see any triggers under this table. What else would cause the database automatically change my update? Could there be any other place I should look for an update trigger on this table? Thanks,
I have an update trigger which fires from a transactiion table to update a parent record in another table. I am getting no errors, but also no update. Any help appreciated (see script below)
create trigger tr_cmsUpdt_meds on dbo.medisp for UPDATE as
if update(pstat) begin update med set REC_FLAG = 2 from deleted dt where med.uniq_id = dt.uniq_id and dt.pstat = 2 and dt.spec_flag = 'kop' end
I am trying to update a fields with an UPDATE statement but I keep getting the error message when I run the query.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I have this Update trigger that I know is causing the error message because I guess it's not built to manage multi-row updates.
Can someone help me re-write it. I also tried using the WHERE p.ID = p.ID but when I do that it modifies all rows in the modifieddate column instead of just the cells/rows that I'm updating
ALTER TRIGGER [dbo].[MultitrigCA] ON [dbo].[ProdDesc] AFTER UPDATE AS
SET NOCOUNT ON
IF UPDATE (codeabbreviation) UPDATE p sET p.ModifiedDate = GETDATE() FROM ProdDesc AS p WHERE p.ID = (SELECT ID FROM inserted)
Hi there,I'm a little stuck and would like some helpI need to create an update trigger which will run an update query onanother table.However, What I need to do is update the other table with the changedrecord value from the table which has the trigger.Can someone please show me how this is done please??I can write both queries, but am unsure as to how to get the value ofthe changed record for use in my trigger???Please helpM3ckon*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
i have a database with about 20 tables. i appended to each table a column "UpdatedOn", and i want to write a trigger to set the date of the update date in that column, using a trigger.
i want to avoid the trigger launching for the last column (UpdatedOn).
how can i detect the rows that changed, and modify only the update date/time? i read something about TableName_Inserted and TableName_Deleted, but i would prefer to copy as generic as possible the data from there, meaning, not to write column names in my script.
another idea i thought about was to prevent the trigger executing if no other column except for UpdatedOn changed, but... i encounter some trouble, when i try to pass column name (as string) to UPDATE() function.(Error: Expecting ID or QUOTED_ID)
Hello All, I've been adviced without much detail that the best way to handle updates is with an Update Trigger to write to an AuditTable. I always thought it was better to avoid triggers. I'm getting confused very fast in reading about triggers. Could someone please point me in the right direction?
i am using address table to store the address of employees, agents, students; so can't use cascade update and delete. so i decided to use trigger but i got stuck here as well. Here employeeid is the PK of employee id. so when it is updated, i want to update that id in corresponding address table as well. so what i need is to find out the previous value of EmployeeID before updating to use in WHERE clause and new EmployeeID to use in SET clause.
<code> CREATE TRIGGER [AddressUpdate] ON [dbo].[MCS_Employee] FOR UPDATE AS IF UPDATE(EmployeeID) BEGIN UPDATE MCS_Address SET EmployeeID = 'i am stuck here; how to know the previous value? WHERE EmployeeID = ??? END </end>
ironically, PK in my table can be updateable as well.
Hello all. I have a table which is the exact replication of a different table. Now if an update takes place in the original table I want to copy that row into the new table without the update.I have this but I am not sure if it works.Create Trigger Content_Archive on content For Update As Insert Into content_audit Select * From DeletedI dont know if Updated exists so I am using Deleted. It is returning both rows (before and after update). And the insert should take place as soon as an update takes place in any field in the content table.
THREAD ------- THREAD_ID TOPIC_ID STATUS_ID THREAD THREAD_DATE NAME
I want to update STATUS_ID in TOPIC when I post a new THREAD. I guess the best would be to use a trigger? Don't know much how you write them so if someone please could help or point me in right direction. My plan is to always show the updated STATUS_ID in TOPIC while I have history in the THREAD TBL. I am using MS SQL 7.0
I am new to this and on my way to learning. I hope someone can help me with a trigger. I want to update status of one table when I insert a new threat in another. This is what I have come up with so far.
CREATE TRIGGER [trg_Update_Status] ON tForumThread FOR INSERT AS BEGIN UPDATE tJournalTopic (STATUS_ID) SELECT STATUS_ID FROM Inserted
I need to be able to select the row or specific field that was updated in an update trigger. I dont have any time-stamp or before-after value columns to compare. Please help!
I HAVE TWO TABLES IN THE DATABSE T1 AND THE SECOND TABLE(T2) IS FOR AUDITING. THE STRUCTURE OF BOTH THE TABLES IS SIMILAR WITH ONE MORE COLUMN 'STATUS' IN THE T2. I HAVE ALREADY WRITTEN THE TRIGGERS FOR THE INSERT AND DELETE TRIGGERS SO THAT I CAN SAVE THE RECORD WITH STATUS OF 'NEW INSERT' OR 'DELETE' OR 'UPDATE' IN THE AUDIT TABLE(T2). HELP ME IN WRITING THE TRIGGER FOR UPDATE. MY PROBLEM IS I DON'T KNOW HOW TO STORE THE UPDATED COLUMNS VALUES INTO VARIABLES AND SO THAT I CAN STORE THE OLD VALUES INTO THE AUDIT TABLE(T2). I M USING SQL SERVER 6.5, SO WE CAN NOT USE COLUMNS_UPDATED() IN THE TRIGGER. PLEASE SUGGEST ME THE CODE SO THAT I CAN STORE THE OLD ROW IN THE TABLE(T1) TO TABLE (T2) WITH STATUS 'UPDATE' IN THE T2.
PLEASE SUGGEST ME..ITS URGENT.
THANKS IN ADVANCE HARISH
============================= /*test trigger for insert status */ if exists (select * from sysobjects where id = object_id('dbo.tri_t1_insert') and sysstat & 0xf = 8) drop trigger dbo.tri_t1_insert GO
CREATE TRIGGER tri_t1_insert ON dbo.t1 FOR INSERT AS declare @v1 binary(20), @v2 varchar(255) Begin select @v1=stamp,@v2=name from inserted insert into t2(stamp,name,status) values(@v1,@v2,'NEW INSERT') end GO ======================================== /*test trigger for delete status */ if exists (select * from sysobjects where id = object_id('dbo.tri_t1_delete') and sysstat & 0xf = 8) drop trigger dbo.tri_t1_delete GO
CREATE TRIGGER tri_t1_delete ON dbo.t1 FOR delete AS declare @v1 binary(20), @v2 varchar(255) Begin select @v1=stamp,@v2=name from deleted insert into t2(stamp,name,status) values(@v1,@v2,'DELETE')
I have an update trigger on a table on my transactional database that inserts a row of data into another database (audit database)for any modification made on the transactional database. So if i modify a row on tran db it will write the data modified as a new row in the audit db.
This works fine if I am updating only 1 row with each Update statement. However if I update more than 1 row (multiple rows) with the same Update statement, the update trigger only inserts the last row modified in the audit database. So I lose record of any other rows modified with the same update statement.
Is there a way by which i can change my Update trigger or something, so I get all the rows updated by 1 update statement inmy audit database???
these columns can allow nulls. What i whant to do with my trigger is do a after trigger and check after the load if a certain column is NULL place a X instead like a flag but only on the columns that are NULL how would i do this.
Is there a way I can get the old value of a specific field when using an update trigger? e.g. I want to use the condition 'If Update(Column) ' in odrer to create a logfile which stores the old and new value of a field. The new value can be get from a Select from Inserted table. Is there a way I can get the old value (before update) also??
I have created a table with the following columns Jobnumber varchar(20), weight real(4), freightcost money(8), trackingnumber vchar(50), comments varchar(2000) and voidid varchar(3)
I wrote a trigger that updates this data based on the voidid to update the package table as followed: CREATE TIGGER [UPS] ON dbo.UPSSHIPMENT FOR INSERT AS DECLARE @JOBNUMBER CHAR(20) DECLARE @WEIGHT REAL(4) DECLARE @FREIGHTCOST MONEY(8) DECLARE @TRACKINGNUMBER CHAR(25) DECLARE @SHIPMETHOD CHAR(50) DECLARE @voidid char(2)
SELECT @JOBNUMBER=JOBNUMBER, @WEIGHT=WEIGHT, @FREIGHTCOST=FREIGHTCOST, @TRACKINGNUMBER=TRACKINGNUMBER, @SHIPMETHOD=SHIPMETHOD, @VOIDID=VOIDID FROM INSERTED
UPDATE PACKAGE SET PACKAGE.WEIGHT = @WEIGHT, PACKAGE.FREIGHTCOST = @FREIGHTCOST, PACKAGE.TRACKINGNUMBER = @TRACKINGNUMBER, PACKAGE.COMMENTS = @SHIPMETHOD WHERE PACKAGE.JOBNUMBER = @JOBNUMBER AND @VOIDID = 'N'
UPDATE PACKAGE SET PACKAGE.WEIGHT = '', PACKAGE.TRACKINGNUMBER = '', PACKAGE.COMMENTS = 'UPS VOID', PACKAGE.FREIGHTCOST = '' WHERE PACKAGE.JOBNUMBER = @JOBNUMBER AND @VOIDID = 'Y'
I am getting the following error see attached. Any help would be great Thank you!
I'm totally new to writing triggers, but managed to get a trigger to update a specific column (ufINCTcost) on change of another column (ufINCKm)
My problem is that the trigger performs this update on ALL rows, which makes it very slow. How can I get it to only update the column on the row where the change was made ?
My trigger looks as follows:
CREATE TRIGGER updateincidents ON [dbo].[_rtblIncidents] FOR UPDATE, INSERT AS IF UPDATE (ufINCKm) BEGIN UPDATE dbo._rtblIncidents SET ufINCTcost=dbo._rtblIncidents.ufINCKm+dbo._rtblInc idents.ufINCToll+dbo._rtblIncidents.ufINCParking+d bo._rtblIncidents.ufINCFlight+dbo._rtblIncidents.u fINCRental