Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trigger update if exists else insert

Posted on 2014-10-20
6
Medium Priority
?
246 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 70

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 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