Solved

Trigger update if exists else insert

Posted on 2014-10-20
6
239 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
[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
  • 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:Scott Pletcher
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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 

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:
Scott Pletcher 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

634 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