简体   繁体   中英

Insert data into two tables from single select statement in sql

In my Sql server there is one database Employee which has table parish. and another one database with customer which has table clients and fips.There is one stored procedure which take data from this two tables clients and fips . now i want to revert the operation. Take data from parish table and insert into clients and fips. Below is the stored procedure which take data from clients and fips.

ALTER PROCEDURE [dbo].[Rpt_getexportparish] @clientID              AS INT,
                                            @assessment_type       AS NVARCHAR(10),
                                            @political_subDivision AS NVARCHAR(10),
                                            @district              AS NVARCHAR(10),
                                            @acct_status           AS NVARCHAR(10),
                                            @millage_type          AS NVARCHAR(10),
                                            @tax_year              AS NVARCHAR(10)
AS
  BEGIN
      SELECT FIPScode             AS fips_code,
             f.cnty_name          AS gov_name,
             c.NAME               AS gov_agency,
             PhysicalAddress1     AS address2,
             PhysicalAddress2     AS address1,
             TaxYear              AS tax_year,
             PhysicalAddressCity  AS city,
             PhysicalAddressState AS state,
             PhysicalAddressZip   AS zip,
             AssessorName         AS assr_name
      FROM   Clients c
             JOIN fips f
               ON c.FIPScode = f.cnty_fips
      WHERE  id = @clientID
             AND place_fips = @political_subDivision
  END 

i want reverse of above. select data from parish and insert into clients and fips table. then what is the sql query for that.

There is no relation in both table clients and fips.

The OUTPUT command will allow you to insert into a secondary Table with a single query.

Here's an example of how it works

DROP TABLE #SOURCE
DROP TABLE #TARGET1
DROP TABLE #TARGET2

CREATE TABLE #SOURCE(ID INT IDENTITY(1,1),Val FLOAT)
GO
CREATE TABLE #TARGET1(ID INT,Val FLOAT)
GO
CREATE TABLE #TARGET2(ID INT,Val FLOAT)
GO
--ADD 10 ROWS TO SOURCE TABLE
INSERT INTO #SOURCE VALUES (RAND())
GO 10

INSERT INTO #TARGET1
OUTPUT INSERTED.* INTO #TARGET2
SELECT
    *
FROM
    #SOURCE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM