473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to make an Update trigger capture the foreign key text value

I have an update trigger on a table called 'tbl_br' that is used to audit column changes and populate those changes into an audit table called 'tbl_audit'. See an excerpt of the code below:

Expand|Select|Wrap|Line Numbers
  1. Declare @LoginId as nvarchar (8)
  2. set @LoginId = dbo.fnGetLoginId()
  3.  
  4. Declare @status_id_new int
  5.  
  6. Declare @id_old as int
  7. Declare @status_id_old int
  8.  
  9. Select  @id_old  = (Select id from Deleted)
  10. Select  @status_id_new  = (Select status_id from tbl_br where id = @id_old)
  11.  
  12. Select  @status_id_old  = (Select status_id from Deleted)
  13.  
  14. if isnull(@status_id_new,0) <> isnull(@status_id_old,0) 
  15. begin
  16. Insert into dbo.tbl_Audit(changed_on, changed_by, table_name, table_Record_id,  Field_Name, Old_Value, new_Value)
  17. values(Getdate(),@LoginId,'tbl_br', @id_old, 'status_id',  @status_id_old,  @status_id_new)
  18. end
The problem with this code is that it inserts the numeric value of 'status_id' instead of the actual descriptive status text value from the table 'tbl_status'. Since audit reports will be run regularly by end-users for various reasons, we would like to show the text value of the foreign key instead of the id. I know there needs to be a join to the foreign key table (ON tbl_status.id = tbl_br.status_id) to get the text value, but I'm not certain how exactly this should be written.

Any insight is greatly appreciated!
Jun 19 '07 #1
0 1082

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Galina | last post by:
Hello I work with Oracle 9 database. I want to create a trigger using 2 tables: KEY_SKILLS_STUDENT and KEY_SKILLS. There are fields in KEY_SKILLS_STUDENT: KEY_SKILLS_ID, PORTFOLIO_RESULT and...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
7
by: Justin | last post by:
I am extremely new at SQL Server2000 and t-sql and I'm looking to create a simple trigger. For explanation sake, let's say I have 3 columns in one table ... Col_1, Col_2 and Col_3. The data type...
7
by: robert | last post by:
i need to update a column which is a member of the PK on this table. there are some thousands of rows to be updated, many more thousand already in the table. so, i get a constraint violation...
1
by: Trent | last post by:
Hello,everyone. I'm setting a db2 replication environment using UDB version 8.1.5 running on Windows 2000 servers. The source server is on a Windows server with the capture program running while...
33
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
by: Joerg Gempe | last post by:
Hello, I've a problem where some data gets updated but I don't know which process (SP) is responsible for it (it's an old installation which I've taken over). Is it somehow possible to know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.