【问题标题】:Insert result of select statement into a SQL Server 2016 table or temp table to then insert or select into将 select 语句的结果插入到 SQL Server 2016 表或临时表中,然后插入或选择到
【发布时间】:2018-12-03 06:21:20
【问题描述】:

第一次发帖,如有错误请见谅。

我想知道如何将结果保存到 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'
    )

【问题讨论】:

  • 您想将其保存到 JSON 列还是文本列?您是否已经创建了一个表来存储您的数据?
  • 你好蒂姆,我想把它作为行和列,只是一个可以查询的普通表。在我运行查询之后,它会显示我希望它如何存储在表中。我还没有创建 LND 表,我认为它可以即时完成,或者我可以创建它。问候,D。
  • 这显然没有任何意义,因为您的 JSON 内容是嵌套的。您应该向我们展示输出的样子。
  • 嗨蒂姆,我刚刚尝试添加一张图片,它说它会添加一个链接。我无法输入图片的描述。
  • 看到你的select *了吗? 之前放一个INSERT INTO MyTable (Column1,column2),它将把它插入到那个表中

标签: json sql-server bulkinsert insert-into


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2010-10-24
    • 2019-12-10
    • 2015-02-10
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多