Search code examples
jsonsql-serverbulkinsertinsert-into

Insert result of select statement into a SQL Server 2016 table or temp table to then insert or select into


Literally the first time I have posted, so please excuse me if I have done it incorrectly.

I'd like to know how I can save the result of this into a SQL Server 2016 table, either a temp or perm table.

Thank you.

    DECLARE @json nvarchar(max)

    SET @json = N'{
      "response": [
        {
          "application": {
            "info": {
              "dat_id": "010.2018.00036494.001",
              "development_type": "Residential - Single new dwelling",
              "application_type": "DA",
              "last_modified_date": "2018-12-03T11:35:24+11:00",
              "description": "Residence, Garage & Colorbond Shed, Demolition of Existing Residence & Tree Removal",
              "authority": {
                "ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
                "name": "AlburyCity"
              },
              "lodgement_date": "2018-10-26T00:00:00+11:00",
                      )
    […..]


    select * from OPENJSON(@json,'$.response')
    with
    (
    [dat_id] varchar(200) '$.application.info.dat_id',
    [development_type] varchar(200) '$.application.info.development_type',
    [last_modified_date] varchar(200) '$.application.info.last_modified_date',
    [description] varchar(300) '$.application.info.description',
    [ref] varchar(200) '$.application.info.authority.ref',
    [name] varchar(200) '$.application.info.authority.name'
    )

enter image description here


Solution

  • To insert into a table from a select result try this way:

    DECLARE @json nvarchar(max)
    
    SET @json = N'{
      "response": [
        {
          "application": {
            "info": {
              "dat_id": "010.2018.00036494.001",
              "development_type": "Residential - Single new dwelling",
              "application_type": "DA",
              "last_modified_date": "2018-12-03T11:35:24+11:00",
              "description": "Residence, Garage & Colorbond Shed, Demolition of Existing Residence & Tree Removal",
              "authority": {
                "ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
                "name": "AlburyCity"
              },
              "lodgement_date": "2018-10-26T00:00:00+11:00",
                      )
    […..]
    
    
    select * into results_from_query from OPENJSON(@json,'$.response')
    with
    (
    [dat_id] varchar(200) '$.application.info.dat_id',
    [development_type] varchar(200) '$.application.info.development_type',
    [last_modified_date] varchar(200) '$.application.info.last_modified_date',
    [description] varchar(300) '$.application.info.description',
    [ref] varchar(200) '$.application.info.authority.ref',
    [name] varchar(200) '$.application.info.authority.name'
    )
    

    The results should be available at : results_from_query table

    Source: https://www.w3schools.com/sql/sql_insert_into_select.asp