Solved

Trigger update if exists else insert

Posted on 2014-10-20
6
236 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

710 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