Solved

Combine View and Select into one INSERT statement

Posted on 2016-10-12
2
22 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 69

Accepted Solution

by:
ScottPletcher earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now