Solved

Trigger update if exists else insert

Posted on 2014-10-20
6
223 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: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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

829 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