Insert a column from one table to another using a join

  • Hi, I know this is probably very simple but i'm not getting it.

    Below are two table stuructres. All I am trying to do is left outer join from [AHMC_GAR_FC].[F/C] to [AHMC_CLAIMS_GAR].[ARCURRFC] and insert values from [AHMC_GAR_FC].[DESCRIPTION] to [AHMC_CLAIMS_GAR].[PAYOR] .

    I used following query, but it didn't work. it gave me an error "The specified schema name "AHMC_CLAIMS_GAR" either does not exist or you do not have permission to use it."

    USE [PREPROCESSING]

    SELECT [Description]

    INTO AHMC_CLAIMS_GAR.[PAYOR]

    FROM dbo.[AHMC_CLAIMS_GAR]

    LEFT OUTER JOIN dbo.[AHMC_GAR_FC] ON dbo.[AHMC_CLAIMS_GAR].[ARCURRFC] = dbo.[AHMC_GAR_FC].[F/C]

    Below are the tables.

    CREATE TABLE [dbo].[AHMC_GAR_FC](

    [F/C] [nvarchar](255) NULL,

    [DESCRIPTION] [nvarchar](255) NULL,

    [PAYER GROUP] [nvarchar](255) NULL

    )

    CREATE TABLE [dbo].[AHMC_CLAIMS_GAR](

    [Facility] [nvarchar](255) NULL,

    [ARPNUM] [nvarchar](255) NULL,

    [ARPNAME] [nvarchar](255) NULL,

    [ARADMDT] [datetime] NULL,

    [ARPTYPE] [nvarchar](255) NULL,

    [ARTOTADJ] [nvarchar](255) NULL,

    [ARDISDT] [datetime] NULL,

    [ARDISDT2] [datetime] NULL,

    [DISCH MONTH] [nvarchar](255) NULL,

    [ARORIGFC] [nvarchar](255) NULL,

    [ARCURRFC] [nvarchar](255) NULL,

    [ARTOTCHG] [money] NULL,

    [ARCURBAL] [money] NULL,

    [ARDRGCD1] [nvarchar](255) NULL,

    [ARREIAMT] [nvarchar](255) NULL,

    [ARTOTREC] [nvarchar](255) NULL,

    [ARTINSRC] [nvarchar](255) NULL,

    [ARTNINSR] [nvarchar](255) NULL,

    [ISBILLDT] [nvarchar](255) NULL,

    [BLD/UNBLD] [nvarchar](255) NULL,

    [DATE] [datetime] NULL,

    [AGE ] [nvarchar](255) NULL,

    [RANGE ] [nvarchar](255) NULL,

    [PAYER] [nvarchar](255) NULL,

    [PAYER 2] [nvarchar](255) NULL,

    [nvarchar](255) NULL,

    [E/I fc] [nvarchar](255) NULL,

    [E/I 2] [nvarchar](255) NULL,

    [PAYOR] [nvarchar](255) NULL

    )

    ------------
    🙂

  • If you're trying to insert into an existing table:

    You can't use "SELECT INTO" with an existing table. You need to do an "INSERT INTO".

    "SELECT INTO" creates the table using the field list (and corresponding data types) you have provided then inserts the data.

    If you are trying to create the table using the SELECT INTO statement:

    You need to check the security of the login you are using on the database you're trying to use. Make sure you're allowed to create tables.

    Dan

  • I'm thinking you just want to do an UPDATE, not an INSERT. INSERT adds new rows. You seem to be wanting to just add the values from a matching record in another table. you need to do it in 2 steps: add the column, then populate it.

    So -

    alter table AHMC_CLAIMS_GAR

    add PAYOR varchar(50) NULL ---<---- or whatever type is required to hold that data

    update AHMC_CLAIMS_GAR

    set Payor=AHMC_GAR_FC.[Description]

    from AHMC_CLAIMS_GAR

    inner join AHMC_GAR_FC on AHMC_CLAIMS_GAR.ARCURRFC=AHMC_GAR_FC.[F/C]

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • alter table AHMC_CLAIMS_GAR

    add PAYOR varchar(50) NULL ---<---- or whatever type is required to hold that data

    update AHMC_CLAIMS_GAR

    set Payor=AHMC_GAR_FC.[Description]

    from AHMC_CLAIMS_GAR

    inner join AHMC_GAR_FC on AHMC_CLAIMS_GAR.ARCURRFC=AHMC_GAR_FC.[F/C]

    Matt, Yes you were right, I did want to do an update. I just couldn't figure that out earlier. It makes sense now. I tried it and the code above that you gave worked. Awesome awesome...

    Dan, what you are saying makes sense now.

    Thanks Guys.

    ------------
    🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply