Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combine View and Select into one INSERT statement

Posted on 2016-10-12
2
Medium Priority
?
43 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

636 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