Solved

Create a Trigger in SQL

Posted on 2010-11-30
3
305 Views
Last Modified: 2012-06-21
Hi

I have one table (Customers) with the following details
Customercode, Name, Address, Phone

another table Requests
ReqID, ReqDescription, CustomerCode, Freefield1, Freefield2

I need to create a trigger in Microsoft SQL 2005 that when a new record is created or updated in Requests Table the following actions updated

Requests.Freefield1 is updated with the value Customers.Address
Requests.Freefield2 is updated with the value Customers.Phone

Can You Help me creating this trigger
 
0
Comment
Question by:nadermik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34237786
this should do:
create trigger trg_requests_get_address
on Requests
for insert, update
as
  if @@rowcount > 0
  begin
  update r
      set r.FreeField1 = c.Address
         , r.FreeField2 = c.Phone
    from Requests r
    join Customers c
      on c.CustomerCode = r.CustomerCode
    join inserted i
      on i.ReqID = r.ReqID
    left join deleted d
      on d.ReqID = i.ReqID
    where isnull(d.CustomerCode, 0) <> i.CustomerCode
  end
 

Open in new window

0
 

Author Comment

by:nadermik
ID: 34245469
Hi
I am getting the following error when running the trigger
Msg 206, Level 16, State 2, Procedure trg_requests_get_address, Line 7
Operand type clash: int is incompatible with uniqueidentifier

Please note that the field r.customercode is varchar and the field c.customercode is char field

can you please help me further
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34245644
ok, let's fix that:
create trigger trg_requests_get_address
on Requests
for insert, update
as
  if @@rowcount > 0
  begin
  update r
      set r.FreeField1 = c.Address
         , r.FreeField2 = c.Phone
    from Requests r
    join Customers c
      on c.CustomerCode = r.CustomerCode
    join inserted i
      on i.ReqID = r.ReqID
    left join deleted d
      on d.ReqID = i.ReqID
    where ( d.CustomerCode IS NULL AND i.CustomerCode IS NOT NULL )
       or d.CustomerCode <> i.CustomerCode
  end

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question