0
votes

SQL compilation error: Expression type does not match column data type, expecting VARIANT but got VARCHAR(3038) for column ATTRIBUTES. Can someone help to correct the query?

MERGE INTO "W_TESTTABLE" T USING (SELECT * FROM (VALUES ('{"type":"issue__c", "url":"/services/data/v42.0"}','a0E2v00001jiirTEAQ','00590000005myFsAAI','false','test account','2019-12-18T14:15:06.000000+05:30','00590000005myFsAAI','2019-12-18T14:15:06.000000+05:30','00590000005myFsAAI','2019-12-18T14:15:06.000000+05:30',NULL,'2019-12-18T14:15:07.000000+05:30','2019-12-18T14:15:07.000000+05:30',NULL,NULL,NULL,NULL,NULL) AS UNUSED_ALIAS ("ATTRIBUTES","ID","OWNERID","ISDELETED","NAME","CREATEDDATE","CREATEDBYID","LASTMODIFIEDDATE","LASTMODIFIEDBYID","SYSTEMMODSTAMP","LASTACTIVITYDATE","LASTVIEWEDDATE","LASTREFERENCEDDATE","ASSIGNED_BY__C","FIELD11111111111111111111111111111111111__C","RFIELD1111111111111111111111111111111111__C","ISSUE_ID__C","ORDERSTEST__C"))) S ON (T."ID" = S."ID") WHEN MATCHED THEN UPDATE SET T."ATTRIBUTES"=S."ATTRIBUTES",T."OWNERID"=S."OWNERID",T."ISDELETED"=S."ISDELETED",T."NAME"=S."NAME",T."CREATEDDATE"=S."CREATEDDATE",T."CREATEDBYID"=S."CREATEDBYID",T."LASTMODIFIEDDATE"=S."LASTMODIFIEDDATE",T."LASTMODIFIEDBYID"=S."LASTMODIFIEDBYID",T."SYSTEMMODSTAMP"=S."SYSTEMMODSTAMP",T."LASTACTIVITYDATE"=S."LASTACTIVITYDATE",T."LASTVIEWEDDATE"=S."LASTVIEWEDDATE",T."LASTREFERENCEDDATE"=S."LASTREFERENCEDDATE",T."ASSIGNED_BY__C"=S."ASSIGNED_BY__C",T."FIELD11111111111111111111111111111111111__C"=S."FIELD11111111111111111111111111111111111__C",T."RFIELD1111111111111111111111111111111111__C"=S."RFIELD1111111111111111111111111111111111__C",T."ISSUE_ID__C"=S."ISSUE_ID__C",T."ORDERSTEST__C"=S."ORDERSTEST__C" WHEN NOT MATCHED THEN INSERT ("ATTRIBUTES","ID","OWNERID","ISDELETED","NAME","CREATEDDATE","CREATEDBYID","LASTMODIFIEDDATE","LASTMODIFIEDBYID","SYSTEMMODSTAMP","LASTACTIVITYDATE","LASTVIEWEDDATE","LASTREFERENCEDDATE","ASSIGNED_BY__C","FIELD11111111111111111111111111111111111__C","RFIELD1111111111111111111111111111111111__C","ISSUE_ID__C","ORDERSTEST__C") VALUES (S."ATTRIBUTES",S."ID",S."OWNERID",S."ISDELETED",S."NAME",S."CREATEDDATE",S."CREATEDBYID",S."LASTMODIFIEDDATE",S."LASTMODIFIEDBYID",S."SYSTEMMODSTAMP",S."LASTACTIVITYDATE",S."LASTVIEWEDDATE",S."LASTREFERENCEDDATE",S."ASSIGNED_BY__C",S."FIELD11111111111111111111111111111111111__C",S."RFIELD1111111111111111111111111111111111__C",S."ISSUE_ID__C",S."ORDERSTEST__C")

I tried doing PARSE_JSON for VALUES something like this

MERGE INTO "W_TESTTABLE" T USING (SELECT * FROM (VALUES (PARSE_JSON('{"type":"issue__c", "url":"/services/data/v42.0"}')....

but got an error Invalid expression [PARSE_JSON('{"type":"issue__c", "url":"/services/data/v42.0/sobjects/issue__c/a0E2v00001jiirTEAQ"}')] in VALUES clause:

I am able to do insert query succesfully

INSERT INTO  DEMO_DB.public.W_TEST(ID, ATTRIBUTES) SELECT 2, PARSE_JSON('{"first_name":"Mickey","last_name":"Mouse"}')

But my requirement is a MERGE query

Table schema Schemapart1

Schemapart2

Schemapart3

1

1 Answers

1
votes

Try below code

 MERGE INTO  "W_TESTTABLE"  t
USING (
SELECT *,parse_json(ATTRIBUTES) as ATTRIBUTES_NEW FROM VALUES 
       ( 
              '{"type":"issue__c", "url":"/services/data/v42.0"}', 
               'a0E2v00001jiirTEAQ', 
               '00590000005myFsAAI', 
               'false', 
               'test account', 
               '2019-12-18T14:15:06.000000+05:30', 
               '00590000005myFsAAI', 
               '2019-12-18T14:15:06.000000+05:30', 
               '00590000005myFsAAI', 
               '2019-12-18T14:15:06.000000+05:30', 
               NULL, 
               '2019-12-18T14:15:07.000000+05:30', 
               '2019-12-18T14:15:07.000000+05:30', 
               NULL, 
               NULL, 
               NULL, 
               NULL, 
               NULL 
       ) 
        as unused_alias ("ATTRIBUTES","ID","OWNERID","ISDELETED","NAME","CREATEDDATE","CREATEDBYID","LASTMODIFIEDDATE","LASTMODIFIEDBYID","SYSTEMMODSTAMP","LASTACTIVITYDATE","LASTVIEWEDDATE","LASTREFERENCEDDATE","ASSIGNED_BY__C","FIELD11111111111111111111111111111111111__C","RFIELD1111111111111111111111111111111111__C","ISSUE_ID__C","ORDERSTEST__C" )) s
  ON (t."ID" = s."ID" ) 
WHEN matched THEN UPDATE 
set              T."ATTRIBUTES"=s."ATTRIBUTES_NEW", 
                 t."OWNERID"=s."OWNERID", 
                 t."ISDELETED"=s."ISDELETED", 
                 t."NAME"=s."NAME", 
                 t."CREATEDDATE"=s."CREATEDDATE", 
                 t."CREATEDBYID"=s."CREATEDBYID", 
                 t."LASTMODIFIEDDATE"=s."LASTMODIFIEDDATE", 
                 t."LASTMODIFIEDBYID"=s."LASTMODIFIEDBYID", 
                 t."SYSTEMMODSTAMP"=s."SYSTEMMODSTAMP", 
                 t."LASTACTIVITYDATE"=s."LASTACTIVITYDATE", 
                 t."LASTVIEWEDDATE"=s."LASTVIEWEDDATE", 
                 t."LASTREFERENCEDDATE"=s."LASTREFERENCEDDATE", 
                 t."ASSIGNED_BY__C"=s."ASSIGNED_BY__C", 
                 t."FIELD11111111111111111111111111111111111__C"=s."FIELD11111111111111111111111111111111111__C",
                 t."RFIELD1111111111111111111111111111111111__C"=s."RFIELD1111111111111111111111111111111111__C",
                 t."ISSUE_ID__C"=s."ISSUE_ID__C", 
                 t."ORDERSTEST__C"=s."ORDERSTEST__C" 
WHEN NOT matched THEN INSERT 
       ( 
              ATTRIBUTES,
              "ID",
              "OWNERID", 
              "ISDELETED", 
              "NAME", 
              "CREATEDDATE", 
              "CREATEDBYID", 
              "LASTMODIFIEDDATE", 
              "LASTMODIFIEDBYID", 
              "SYSTEMMODSTAMP", 
              "LASTACTIVITYDATE", 
              "LASTVIEWEDDATE", 
              "LASTREFERENCEDDATE", 
              "ASSIGNED_BY__C", 
              "FIELD11111111111111111111111111111111111__C", 
              "RFIELD1111111111111111111111111111111111__C", 
              "ISSUE_ID__C", 
              "ORDERSTEST__C" 
       ) 
       VALUES 
       ( 
              s."ATTRIBUTES_NEW", 
              s.ID ,
              s."OWNERID", 
              s."ISDELETED", 
              s."NAME", 
              s."CREATEDDATE", 
              s."CREATEDBYID", 
              s."LASTMODIFIEDDATE", 
              s."LASTMODIFIEDBYID", 
              s."SYSTEMMODSTAMP", 
              s."LASTACTIVITYDATE", 
              s."LASTVIEWEDDATE", 
              s."LASTREFERENCEDDATE", 
              s."ASSIGNED_BY__C", 
              s."FIELD11111111111111111111111111111111111__C", 
              s."RFIELD1111111111111111111111111111111111__C", 
              s."ISSUE_ID__C", 
              s."ORDERSTEST__C"
       )