merge when not matched with data value from target - sql

MERGE Table1 AS T USING Table2 AS S ON (T.1 = S.10)
WHEN MATCHED AND T.2 = 'testname1' THEN UPDATE SET T.4 = S.11
WHEN NOT MATCHED THEN INSERT (t.3, t.1, t.2, t.4) VALUES (22, S.10, 'testname1', s.11);
MERGE Table1 AS T USING Table2 AS S ON (T.1 = S.10)
WHEN MATCHED AND T.2 = 'testname2' THEN UPDATE SET T.4 = S.11
WHEN NOT MATCHED THEN INSERT (t.3, t.1, t.2, t.4) VALUES (22, S.10, 'testname2', s.11);
I need to run this 5 times changing the data for T.2 each time. The update statements work fine all 5 times but the insert only works for the first run because t.1 now exits in the table and you can not have a when not matched and a reference to a target column. What I would like is "WHEN NOT MATCHED AND T.2 <> 'testname2' THEN INSERT ....
t.1 is the only column you can join on and when done each t.1 should have 5 rows of data with t.2 and t.4 updated to the data from s10 and s.11. Testname is text and NOT stored in any table.
completed data
22, 1234, testname 1, text 11
22, 1234, testname 2, text 12
22, 1234, testname 3, text 13
22, 1234, testname 4, text 14
22, 1234, testname 5, text 15
How can I get this to work??

Related

SQL, How to take into account previously populated records when populating a sequential number ID for a field in a nightly script

I am trying to include a clause in a script that runs nightly that will populate a three digit number into a field if that record meets a set of conditions. I will include the script that I have written for this below but I do not know how to account for the numbers that will have been populated on previous nights and keep the new numbers to be populated in sequential order. The numbers must start at 100 and go up by 1 each time a new record is found that meets the conditions.
All help is appreciated.
My current script:
DECLARE #myVar NVarchar(50)
SET #myVar = 99
UPDATE Database1..Thing
SET #myVar = Thing_Number_NEW = #myVar + 1
WHERE (Thing_Number = '' OR Thing_Number IS NULL)
AND Thing_Number_Needed = 'Yes'
AND Symbology IN (2, 3, 55, 66)
AND Thing_Number_New IS NULL
AND Last_edited_user is not null
Is this what you want?
UPDATE Database1..Thing
SET Thing_Number_NEW = COALESCE(n.max_Thing_Number_NEW + 1, 100)
FROM (SELECT MAX(Thing_Number_NEW) as max_Thing_Number_NEW FROM Database1..Thing) n
WHERE (Thing_Number = '' OR Thing_Number IS NULL)
Thing_Number_Needed = 'Yes' AND
Symbology IN (2, 3, 55, 66) AND
Thing_Number_New IS NULL AND
Last_edited_user is not null;

DB2 MERGE does not INSERTwhen NOT MATCHED

My Statement:
MERGE INTO tblshoppingcart AS target USING
(SELECT * FROM tblshoppingcart
WHERE session_id = 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae ' AND primlink = '19830625000054' AND store = 17 AND catalog = 'SS3' AND quantity = 35 AND item_type = 0) AS source
ON target.primlink = source.primlink AND
target.session_id = source.session_id AND target.item_type = source.item_type
WHEN NOT MATCHED THEN
INSERT VALUES ( 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae', '19830625000054', 17, 'SS3', 'PAS', 35, 5, '', 0 )
WHEN MATCHED THEN
UPDATE SET quantity = 15
When matched the UPDATE works fine
When NOT MATCHED the INSERT doesn't throw an error but doesn't insert anything either.
Try changing WHEN NOT MATCHED THEN to:
WHEN NOT MATCHED BY TARGET THEN
Thanks, for the repsonses I found the issue to be I was passing hardcoded values but when I used the source/target names it worked.
So instead of INSERT VALUES ( 'f7f2eb03-5ca5-4a85-b83e-70f197c087ae'
I do INSERT VALUES ( target.session

ON DUPLICATE KEY UPDATE throws SQL error ORA-00933: SQL command not properly ended

I have a SQL Query that uses ON DUPLICATE KEY UPDATE as:
QUERY UPDATED :
MERGE INTO reports rt
USING (SELECT REPORT_ID,TITLE,CATEGORY,DISPLAY_ORDER,QUERY,DESCRIPTION,CONTENT_SEQ,DELD,ADMIN_ID
FROM reports) rs
ON (rt.report_id = rs.report_id)
WHEN MATCHED THEN
UPDATE SET rt.TITLE = 'a',
rt.CATEGORY = 'z',
rt.DISPLAY_ORDER= 9,
rt.QUERY ='q',
rt.DESCRIPTION='d',
rt.CONTENT_SEQ=1,
rt.DELD=0,
rt.ADMIN_ID=1
WHEN NOT MATCHED THEN
INSERT(REPORT_ID,TITLE,CATEGORY,DISPLAY_ORDER,QUERY,DESCRIPTION,CONTENT_SEQ,DELD,ADMIN_ID)
VALUES(27,
'a',
'z',
9,
'q',
'd',
1,
0,
1;
When I run this query it throws an error : SQL Error: ORA-00933: SQL command not properly ended.
P.S : I am using Oracle Database.
Is it that it wont support 'ON DUPLICATE KEY UPDATE' clause ?
If No,what is the other option to achieve this ?
I am using Oracle Database.
Is it that it wont support 'ON DUPLICATE KEY UPDATE' clause ? If
No,what is the other option to achieve this ?
Probably it is a MySQL supported feature. Oracle doesn't support ON DUPLICATE KEY UPDATE. You need to use a MERGE statement.
For example,
MERGE INTO dest_table t
USING (SELECT <column_list>
FROM source_table) s
ON (t.col1 = s.col1) -- use required join keys
WHEN MATCHED THEN
UPDATE SET t.<column_list> = s.<column_list>
...
WHEN NOT MATCHED THEN
INSERT( t.<column_list>)
VALUES( s.<column_list>)
Read the documentation for more details on MERGE. Few examples here.
Update Based on OP's new query in the edited question.
WHEN NOT MATCHED THEN INSERT(REPORT_ID,TITLE,CATEGORY,DISPLAY_ORDER,QUERY,DESCRIPTION,CONTENT_SEQ,DELD,ADMIN_ID)
VALUES(27,
'a',
'z',
9,
'q',
'd',
1,
0,
1;
There is a closing brace missing in the end before the semi-colon. Try this:
MERGE INTO reports rt
USING (SELECT report_id,
title,
category,
display_order,
query,
description,
content_seq,
deld,
admin_id
FROM reports) rs
ON (rt.report_id = rs.report_id)
WHEN matched THEN
UPDATE SET rt.title = 'a',
rt.category = 'z',
rt.display_order = 9,
rt.query = 'q',
rt.description = 'd',
rt.content_seq = 1,
rt.deld = 0,
rt.admin_id = 1
WHEN NOT matched THEN
INSERT(report_id,
title,
category,
display_order,
query,
description,
content_seq,
deld,
admin_id)
VALUES(27,
'a',
'z',
9,
'q',
'd',
1,
0,
1);

How to debug this SQL error message, "can't insert NULL "?

OUT_RC = 37 ORA-01400: cannot insert NULL into ("PARTY"."SOURCE_SYSTEM_PARTY_CHARC"."PTY_CHARC_TY_VLU"): ORA-06512: at "PARTY.UP_MANAGE_SRC_SYS_PARTY_CHAR", line 131
I'm getting this error message, but at line 131 is a MERGE statement
MERGE INTO PARTY.SOURCE_SYSTEM_PARTY_CHARC SSPC
USING (SELECT l_sspc_chgs(index).PTY_CHARC_TY_ID AS PTY_CHARC_TY_ID,
l_sspc_chgs(index).SRC_SYS_PTY_ID AS SRC_SYS_PTY_ID, ...
Her eis more code(surrounded the above MERGE line :
OPEN SRC_SYS_PTY_CHARC_STG_CUR;
LOOP
FETCH SRC_SYS_PTY_CHARC_STG_CUR
BULK COLLECT INTO l_sspc_chgs LIMIT blklimit;
FOR indx IN 1 .. l_sspc_chgs.COUNT
LOOP
IF (l_sspc_chgs(indx).DLTD_IND = 'N')
THEN
MERGE INTO PARTY.SOURCE_SYSTEM_PARTY_CHARC SSPC
USING (SELECT l_sspc_chgs(indx).PTY_CHARC_TY_ID AS PTY_CHARC_TY_ID,
l_sspc_chgs(indx).SRC_SYS_PTY_ID AS SRC_SYS_PTY_ID,
l_sspc_chgs(indx).PTY_CHARC_TY_VLU AS PTY_CHARC_TY_VLU,
SYSTIMESTAMP AS CREATE_TS,
USER AS CREATE_USER_ID,
SYSTIMESTAMP AS UPDATE_TS,
USER AS UPDATE_USER_ID
FROM DUAL) SRC
ON (SSPC.PTY_CHARC_TY_ID = SRC.PTY_CHARC_TY_ID
AND SSPC.SRC_SYS_PTY_ID = SRC.SRC_SYS_PTY_ID)
WHEN MATCHED THEN
UPDATE SET PTY_CHARC_TY_VLU = SRC.PTY_CHARC_TY_VLU
,UPDATE_TS = SRC.UPDATE_TS
,UPDATE_USER_ID = SRC.UPDATE_USER_ID
WHEN NOT MATCHED THEN
INSERT ( SRC_SYS_PTY_CHARC_ID, PTY_CHARC_TY_ID, SRC_SYS_PTY_ID,
PTY_CHARC_TY_VLU, CREATE_TS, CREATE_USER_ID,
UPDATE_TS, UPDATE_USER_ID)
VALUES (
SOURCE_SYSTEM_PARTY_CHARC_SEQ.NEXTVAL,
...
l_sspc_chgs is used for bulk collect (from top of code) :
TYPE sspc_chgs IS TABLE OF SRC_SYS_PTY_CHARC_STG_CUR%ROWTYPE
INDEX BY PLS_INTEGER;
l_sspc_chgs sspc_chgs;
You column "PARTY"."SOURCE_SYSTEM_PARTY_CHARC"."PTY_CHARC_TY_VLU" must have "NOT NULL" constraint.
Try to replace
l_sspc_chgs(indx).PTY_CHARC_TY_VLU AS PTY_CHARC_TY_VLU
with
NVL(l_sspc_chgs(indx).PTY_CHARC_TY_VLU, 'Default value') AS PTY_CHARC_TY_VLU

Update Field with Special Character ' (single quote)

I am workign with SQL Server 2008 and have to update one field (FIELD_NAM) which contains values of this format:
First value in the field: 'abc', 'efg', 'xyz'
Second value in the field: 'aaaaa', 'bbbb', 'vvvvvv'
I tried with the following statement:
UPDATE Table
SET FIELD = 'ttttt', 'kkkk', 'mmmmmm'
WHERE ID = 1
(only one row and one field/column has to be updated)
The error I got is
Incorrect syntax near 'ttttt'
update "table"
set field = '''ttttt'', ''kkkk'', ''mmmmmm'''
where id = 1
;
UPDATE Table
SET FIELD = 'ttttt\', \'kkkk\', \'mmmmmm'
WHERE ID = 1
OR
UPDATE Table
SET FIELD = 'ttttt'', ''kkkk'', ''mmmmmm'
WHERE ID = 1