简体   繁体   English

如何在 sql 服务器中的查询创建的表中添加行?

[英]how to add row in a table created by a query in sql server?

I begin with SQL.我从 SQL 开始。 I first writed a query which creates a table.我首先编写了一个创建表的查询。 With this table I would like to add some rows.有了这张表,我想添加一些行。 I tought that using "insert into" statement was the right idea but it does not work.我认为使用“插入”语句是正确的想法,但它不起作用。

insert into (
    select Element = [Key]
          ,New = max(case when time_index=1 then value end)
          ,'Current' = max(case when time_index>=2 then value end)
     From  (
            Select [time_index]
                  ,B.*
             From  (select * from ifrs17.output_bba where id in (602677,602777)) A
             Cross Apply (
                          Select [Key]
                                ,Value
                           From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper ) ) 
                           Where [Key] not in ('time_index')
                         ) B
            ) A
     Group By [Key])
 values ('acc_test','test', 'test')

I receive this error message:我收到此错误消息:

Msg 156, Level 15, State 1, Line 2消息 156,第 15 级,State 1,第 2 行
Incorrect syntax near the keyword 'select'.关键字“select”附近的语法不正确。
Msg 156, Level 15, State 1, Line 17消息 156,第 15 级,State 1,第 17 行
Incorrect syntax near the keyword 'values'.关键字“值”附近的语法不正确。

Can you please show me how to add rows in my table?你能告诉我如何在我的表格中添加行吗?

Use UNION ALL to join the query and a row value constructs by elements:使用 UNION ALL 连接查询和按元素构造的行值:

insert into (
    select Element = [Key]
          ,New = max(case when time_index=1 then value end)
          ,'Current' = max(case when time_index>=2 then value end)
     From  (
            Select [time_index]
                  ,B.*
             From  (select * from ifrs17.output_bba where id in (602677,602777)) A
             Cross Apply (
                          Select [Key]
                                ,Value
                           From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper ) ) 
                           Where [Key] not in ('time_index')
                         ) B
            ) A
     Group By [Key])
UNION
SELECT 'acc_test', 'test', 'test'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在sql查询中添加表底部的行? - How to add row at the bottom of table in sql query? SQL 服务器:枚举使用 ROW_NUMBER() OVER (PARTITION - SQL Server: Enumerating table query groups created with ROW_NUMBER() OVER (PARTITION 如何为SQL Server中另一个表中的所有行添加行 - How to add a row for all the rows in another table in SQL Server 如何基于另一个SQL Server表的单列中的更改添加一个SQL Server表的行和时间戳 - How to add a row and timestamp one SQL Server table based on a change in a single column of another SQL Server table 将查询的输出插入到在不同服务器SQL Server 2008上创建的表中 - Inserting the output of a query into a table created on a different server SQL Server 2008 SQL:如何进行查询以返回每个用户从表数据中最后创建的行 - SQL: How to make a query that return last created row per each user from table's data 如何使用select在SQL Server WITHDD D操作中显示新创建的表中的静态行 - How to show static row in newly created table in SQL Server WITHOUT DDL operation using select 根据表中最近创建的行查找值(SQL Server) - Find a value based on the most recently created row in a table (SQL Server) 如何更新标准 SQL 中查询创建的表? - How to update a table created by query in Standard SQL? 如何循环在 SQL 服务器中动态创建的查询 - How to LOOP a query that is created dynamically in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM