简体   繁体   中英

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

I begin with 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
Incorrect syntax near the keyword 'select'.
Msg 156, Level 15, State 1, Line 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:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM