Solved

Create a Trigger in SQL

Posted on 2010-11-30
3
302 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
  • 2
3 Comments
 
LVL 142

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 142

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql Audit table 3 67
Not selecting duplicate data 6 55
kill process lock Sql server 9 54
convert null in sql server 12 34
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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