Solved

Nesting a SELECT statement inside an INSERT statement

Posted on 2014-07-23
1
226 Views
Last Modified: 2014-07-23
I copied data from table 'Provider' with agencyID = 1 and INSERTED INTO the same table with an agencyID = 2.  This table has an ID.  I need this ID in my next INSERT INTO statement.  There are many rows for agencyID = 2 in the Provider table.  There will be a many to many relationship between the two tables.

The next table I will be using is the 'ReportingUnits' table.  I will do the same thing.  Coping the data WHERE agencyID =1 into the same table with agencyID = 2.  [rUnitProviderId] field is the primary key of the provider table.  How can I populate this field with a SELECT statement inside my INSERT statement?


Here is my query:

INSERT INTO ReportingUnits ([ProviderNumber],[ReportingUnit],[UnitName],[SiteIdentifier],[SiteName]
      ,[AgencyID],[create_date], [rUnitProviderId])
  
  SELECT [ProviderNumber],[ReportingUnit],[UnitName],[SiteIdentifier],[SiteName]
      ,'2' ,getDate(),  

(SELECT ProviderID FROM Provider WHERE AgencyID = 2 AND ProviderNumber = THIS IS WHERE I NEED HELP!!!

  FROM [ReportingUnits]
  WHERE Agencyid = 1

Open in new window

0
Comment
Question by:huerita37
[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
1 Comment
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 500 total points
ID: 40216028
Hi,

I think this will help you to get what you wanted.

INSERT INTO ReportingUnits ([ProviderNumber],[ReportingUnit],[UnitName],[SiteIdentifier],[SiteName]
      ,[AgencyID],[create_date], [rUnitProviderId])
  
  SELECT [ProviderNumber],[ReportingUnit],[UnitName],[SiteIdentifier],[SiteName]
      ,'2' ,getDate(), (SELECT ProviderID FROM Provider b WHERE AgencyID = 2 AND b.ProviderNumber = a.[ProviderNumber]) rpid

  FROM [ReportingUnits] a
  WHERE Agencyid = 1

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

688 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