?
Solved

Combine View and Select into one INSERT statement

Posted on 2016-10-12
2
Medium Priority
?
44 Views
Last Modified: 2016-10-12
Hi all.

I have the following INSERT statement that uses a VIEW (named ActiveAGs), if any record in the AGmembership table is not present in the ActiveAGs VIEW then insert into the AG table. So, the records might not be in the AG table at all OR they might be in the AG table but with a status of INACTIVE which means they have to be entered into the AG table (the default value when inserting a record is ACTIVE).

Any idea how I can do this without having to create a view and then use that view in the INSERT statement, is there a way to do it in just one statement?

Thank you in advance.

INSERT INTO AG([GName],AG)
SELECT DISTINCT AGmembership.[GName], AGmembership.AG
FROM         AGmembership LEFT OUTER JOIN
                      ActiveAGs ON AGmembership.[GName] = ActiveAGs.[GName] AND AGmembership.AG = ActiveAGs.AG
WHERE     (ActiveAGs.[GName] IS NULL) AND (ActiveAGs.AG IS NULL)

Open in new window


Here is the ActiveAGs View:
SELECT     [GName], AG, Status
FROM         dbo.AG
WHERE     (Status = 'Active')

Open in new window

0
Comment
Question by:printmedia
2 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 41840921
INSERT INTO AG([GName],AG)
SELECT DISTINCT AGm.[GName], AGm.AG
FROM         AGmembership AGm
WHERE
    NOT EXISTS (
        SELECT 1
        FROM ActiveAGs AAG
        WHERE AAG.GName = AGm.GName AND AAG.AG = AGm.AG AND AAG.Status = 'Active'
    )
0
 

Author Closing Comment

by:printmedia
ID: 41840933
Worked great. Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

807 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