Solved

Create a Trigger in SQL

Posted on 2010-11-30
3
300 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now