Solved

Nesting a SELECT statement inside an INSERT statement

Posted on 2014-07-23
1
222 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
1 Comment
 
LVL 14

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

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

895 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

12 Experts available now in Live!

Get 1:1 Help Now