[英]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.