Combine View and Select into one INSERT statement

Posted on 2016-10-12
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.

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

Question by:printmedia
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
LVL 69

Accepted Solution

Scott Pletcher earned 500 total points
ID: 41840921
FROM         AGmembership AGm
        SELECT 1
        FROM ActiveAGs AAG
        WHERE AAG.GName = AGm.GName AND AAG.AG = AGm.AG AND AAG.Status = 'Active'

Author Closing Comment

ID: 41840933
Worked great. Thanks!

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
section a string 5 52
How can I modify my merge data stored procedure? 3 33
Begin Transaction 12 23
SQL Simple Query Taking a Very Long Time 11 29
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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