将select语句的结果插入到SQL Server 2016表或临时表中,然后插入或选择到

时间:2018-12-03 06:21:20

标签: json sql-server bulkinsert insert-into

从字面上看,这是我第一次发布,因此,如果我做错了,请原谅。

我想知道如何将其结果保存到SQL Server 2016表(临时表或烫发表)中。

谢谢。

    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

1 个答案:

答案 0 :(得分:0)

要从选择结果中插入表,请尝试以下方法:

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'
)

结果应该在以下位置可用: results_from_query

来源:https://www.w3schools.com/sql/sql_insert_into_select.asp