Solved

Trigger update if exists else insert

Posted on 2014-10-20
6
213 Views
Last Modified: 2014-10-21
Hi guys,

Ok I had trigger that fired on insert, this worked fine...

USE [TestingArea]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[Syspro]
AFTER UPDATE

AS BEGIN

   INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
     SELECT
         CustName, Active
     FROM
         Syspro
END

Open in new window


I then realised I needed to see if the name already existed, if so then update the existsing entry, if not then insert. This is my syntax so far....

USE [TestingArea]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[Syspro]
AFTER INSERT, UPDATE AS

If Exists (Select * from SysproBase WHERE  SysproName =  CustName)

BEGIN

    UPDATE dbo.SysproBase
    SET
        [SysproActive] = Active WHERE SysproName = CustName
END
ELSE
BEGIN
   INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
     SELECT
         CustName, Cool
     FROM
         Syspro
END

Open in new window


I am basically trying to see if the name already exists in SysproBase by looking up the CustName, this is erroring on CustName. I thought it would be able to find this as it had no problems when it was just an insert statement. Could someone provide the correct syntax? btw 'Syspro' is the table where the trigger sits.

Thanks,
Dean
0
Comment
Question by:deanlee17
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40392381
When you check, the trigger doesn't know at that point, what CustName is.

USE [TestingArea]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[Syspro]
AFTER INSERT, UPDATE AS
declare @CustName nvarchar(100) -- or whatever datatype it is
     SELECT @CustName = CustName
     FROM Syspro -- This assumes that there is only one CustName in this table. If there is more than one you will need  a where clause

If Exists (Select * from SysproBase WHERE  SysproName =  @CustName)
BEGIN

    UPDATE dbo.SysproBase
    SET
        [SysproActive] = Active WHERE SysproName = CustName
END
ELSE
BEGIN
   INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
     SELECT
         CustName, Cool
     FROM
         Syspro
END

Open in new window

0
 

Author Comment

by:deanlee17
ID: 40392392
Hi Lee,

Yes of course. Thanks for this, will test it tomorrow morning and get back to you.

Thanks.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40393068
1) To get the row(s) that were just INSERTed or UPDATEd and thus cause the trigger to fire, you look at the "inserted" (pseudo)table, not the original table name.
2) The trigger will only fire once per statement, not per row.  That is, if 50 rows are INSERTed, the trigger will get fired only once, and the inserted table will contain 50 rows.  Thus, you can't use variables to do the processing, you need to do set-based processing instead.


ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[Syspro]
AFTER INSERT, UPDATE
AS
UPDATE sb
    SET
        [SysproActive] = Active
FROM dbo.SysproBase sb
INNER JOIN inserted i ON
    i.CustName = sb.SysproName

INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
    SELECT
         i.CustName, i.Cool
    FROM
         inserted
    LEFT OUTER JOIN dbo.SysproBase sb ON
        i.CustName = sb.SysproName
    WHERE
        sb.SysproName IS NULL
GO
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:deanlee17
ID: 40393816
Ok so ive tried running:

USE [TestingArea]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[Syspro]
AFTER INSERT, UPDATE
AS
UPDATE sb
    SET
        [SysproActive] = Cool
FROM dbo.SysproBase sb INNER JOIN inserted i ON i.CustName = sb.SysproName

INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
    SELECT
         i.CustName, i.Cool
    FROM
         inserted LEFT OUTER JOIN dbo.SysproBase sb ON i.CustName = sb.SysproName
    WHERE
        sb.SysproName IS NULL
GO 

Open in new window


Error:

Msg 4104, Level 16, State 1, Procedure InsertTest, Line 15
The multi-part identifier "i.CustName" could not be bound.
Msg 4104, Level 16, State 1, Procedure InsertTest, Line 13
The multi-part identifier "i.CustName" could not be bound.
Msg 4104, Level 16, State 1, Procedure InsertTest, Line 13
The multi-part identifier "i.Cool" could not be bound.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40394760
--I accidentally omitted the alias name "i" after "inserted":
...
INSERT INTO [dbo].[SysproBase](SysproName, SysproActive)
    SELECT
         i.CustName, i.Cool
    FROM
         inserted AS i LEFT OUTER JOIN dbo.SysproBase sb ON i.CustName = sb.SysproName
...
0
 

Author Comment

by:deanlee17
ID: 40394785
Spot on Scott,

Thanks very much for that.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

10 Experts available now in Live!

Get 1:1 Help Now