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
Related
I have the below MERGE statement that I built to implement SCD type 2 on my SQL Server DB however if you look at the attached image, this sometimes inserts multiple duplicate rows which is incorrect - this should be one latest row per meterkey/meterserialnumber.
Is there anything obvious in the code I need to amend? It's weird because if I add a test row of data and run the merge, this works fine.. its just the occasional rows where it seems to be doing this.
DECLARE #DateNow int = CONVERT(INT, CONVERT(VARCHAR(8), GETDATE(), 112))
IF Object_id('tempdb..#meterkeysinsert') IS NOT NULL
DROP TABLE #meterkeysinsert;
CREATE TABLE #meterkeysinsert
(
MeterKey int,
change VARCHAR(10)
);
MERGE INTO [DIM].[MeterDetails] AS Target
using dbo.test AS Source
ON Target.meterkey = source.meterkey
and target.MeterSerialNumber = Source.MeterSerialNumber
and target.islatest = 1
WHEN matched THEN
UPDATE SET Target.islatest = 0,
Target.todatekey = #Datenow
WHEN NOT matched BY target THEN
INSERT ( meterkey
,[MeterSerialNumber]
,[lguf]
,[electricityMetertype]
,[profileType]
,[timeSwitchCode]
,[lineLossFactorId]
,[standardSettlementConfiguration]
,[energisationStatus]
,[DateSpecifiedKey]
,[distributorId]
,[gspid]
,[FromDatekey]
,[ToDatekey]
,[IsLatest])
VALUES (Source.meterkey
,Source.[MeterSerialNumber]
,Source.[lguf]
,Source.[electricityMetertype]
,Source.[profileType]
,Source.[timeSwitchCode]
,Source.[lineLossFactorId]
,Source.[standardSettlementConfiguration]
,Source.[energisationStatus]
,Source.[DateSpecifiedKey]
,Source.[distributorId]
,Source.[gspid]
,#Datenow
,NULL
,1 --IsRowCurrent
)
output Source.meterkey,
$action
INTO #meterkeysinsert;
INSERT INTO [DIM].[MeterDetails]
(MeterKey
,[MeterSerialNumber]
,[lguf]
,[electricityMetertype]
,[profileType]
,[timeSwitchCode]
,[lineLossFactorId]
,[standardSettlementConfiguration]
,[energisationStatus]
,[DateSpecifiedKey]
,[distributorId]
,[gspid]
,[FromDateKey]
,[ToDateKey]
,[IsLatest])
SELECT A.MeterKey
,[MeterSerialNumber]
,[lguf]
,[electricityMetertype]
,[profileType]
,[timeSwitchCode]
,[lineLossFactorId]
,[standardSettlementConfiguration]
,[energisationStatus]
,[DateSpecifiedKey]
,[distributorId]
,[gspid]
,#Datenow
,null
,1
FROM dbo.test a
INNER JOIN #meterkeysinsert CID
ON a.MeterKey = CID.Meterkey
AND CID.change = 'UPDATE'
`
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;
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??
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
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