Update statement not working with cte in DB2 z/os - database

I am getting an error while running this query in db2 database.
Query:
NOT WORKING
WITH CTE AS
(SELECT PRODUCT,ID FROM TABLE )
UPDATE CTE
SET PRODUCT = 'CK'
Working
UPDATE TABLE
SET PRODUCT = 'CK'
Please need help here.
Thanks in advance!

Related

How INSERT and UPDATE works based on IF condition?

Curious to know one simple SQL concept in SQL Server.
I have two tables, based on certain IF condition , I would like to update and insert data from different tables.I'm using below query for this in side a stored procedure.
IF( SELECT COUNT(colname) FROM [dbo].[Table_BKP] WHERE colname LIKE ('%Dear(DEC''D)444-0292/555-4528C%'))> 0
BEGIN
;WITH Dear AS
(
SELECT ID,colname,SUBSTRING(colname,PATINDEX('%(DEC''D)%',colname),LEN(colname)-1) AS colname_4SUPPL,LEFT(colname,PATINDEX('%(DEC''D)%',colname)-1) AS colname_4MAIN
FROM [dbo].[Table_BKP] WHERE RTRIM(LTRIM(colname)) LIKE ('Dear(DEC''D)444-0292/555-4528C')
) SELECT * INTO Dear_ FROM Dear
UPDATE A SET A.colname = B.colname_4MAIN FROM Table AS A INNER JOIN
Dear_ AS B ON A.ID = B.ID
INSERT INTO Table_SUPPL(ID,colnameMSC,[SOURCE],[TARGET])
SELECT ID,replace(colname_4SUPPL,'(DEC''D)',''),'colname','colnameMSC'
FROM Dear_
END
update statement is working fine and data is getting updated into the required table.However, the data is not getting inserted into the required table.
But, when I changed the sequence as below, it worked perfectly.
IF( SELECT COUNT(colname) FROM [dbo].[Table_BKP] WHERE colname LIKE ('%Dear(DEC''D)444-0292/555-4528C%'))> 0
BEGIN
;WITH Dear AS
(
SELECT ID,colname,SUBSTRING(colname,PATINDEX('%(DEC''D)%',colname),LEN(colname)-1) AS colname_4SUPPL,LEFT(colname,PATINDEX('%(DEC''D)%',colname)-1) AS colname_4MAIN
FROM [dbo].[Table_BKP] WHERE RTRIM(LTRIM(colname)) LIKE ('Dear(DEC''D)444-0292/555-4528C')
) SELECT * INTO Dear_ FROM Dear
INSERT INTO Table_SUPPL(ID,colnameMSC,[SOURCE],[TARGET]) SELECT
ID,replace(colname_4SUPPL,'(DEC''D)',''),'colname','colnameMSC' FROM
Dear_
UPDATE A SET A.colname = B.colname_4MAIN FROM Table AS A INNER JOIN
Dear_ AS B ON A.ID = B.ID
END
Why, the formar quesry is not working as expected? And, the later one [just after changing sequesnce (1st INSERT then Update)]; Its working perfectly...How SQL Server manages these sequence? Could anyone please clarify this simple doubt? Thanks.

SQL Server : using SELECT in NOT IN WHERE Clause

I've been using this query statement ever since. I wonder why this does not work on SQL Server 2008 R2.
SELECT
UserName
FROM
Users
WHERE
UserName NOT IN (SELECT UserName FROM UserTableT2)
The codes does not return any data. Goal is select all UserName in Users table which do not belong to UserTableT2.
EDIT:
Here's the actual query
Update using #Tim Schelmter's query:
Update :
Update:
Thank you!
I would use NOT EXISTS:
SELECT u.UserName
FROM Users u
WHERE NOT EXISTS
(
SELECT 1 FROM UserTableT2 ut2
WHERE u.UserName = ut2.UserName
)
Why? Because it works also if there are NULL values in UserTableT2.UserName.
Worth reading:
Instead of NOT IN, use a correlated NOT EXISTS for this query pattern.
Always. Other methods may rival it in terms of performance, when all
other variables are the same, but all of the other methods introduce
either performance problems or other challenges.
With your updated columns and tables:
SELECT u.usr_id
FROM ousr u
WHERE NOT EXISTS
(
SELECT 1 FROM ApprovalStageApprovers asa
WHERE u.usr_id = asa.ApprovalUser
)

Update Table With Join Condition

I have following scenario.
Table A // Dev
ID
NAME
Address
Table A // Prod
ID
NAME
Address
While Deleting from Dev i need to check if it exists in prod and if it does then i need to restore it's values from prod and delete all those don't exist in Prod. ANY SQL help around this? Can anyone suggest a query?
You should use Lookup
1.Source will be your development .
2.Drag a lookup and write a query to get the ID from the production table .Match the ID's from source and Production in Lookup and select the ID from Production as well as other columns in production
3.Drag a OLedb command and write a query to update the dev
update d set d.Col1 = ?, d.Col2 =?
from dev.tableA d
where d.id = ?
4.Similary write the query for delete and map the columns selected fromn the lookup
delete from dev.tableA
where id <> ?
Note: Oleb command executes for each row .Therefore it will be slow if you have too many rows . If performance is a main concern then you can dump all the data after lookup into a table in your development server and then use Merge syntax in a Execute SQL task to perform update and delete operation
If both databases are in the same instance then you could try following queries:
--update query
update d set d.name = p.name, d.address = p.address
from dev.dbo.tableA d
join prod.dbo.tableA p on d.id = p.id
--delete query
delete dev.dbo.tableA
where id not in (select id from prod.dbo.tableA)

Update Multiple records in single query MS SQL

I am trying to update multiple records in the table using the temporary table using the below query, but doesn't working. Please tell me the proper way to update multiple records.
UPDATE sarufoo
SET sarufoo.mobile = (SELECT mobile_no FROM logan)
WHERE sarufoo.homep IN (SELECT homep FROM logan);
One neater way to achieve that be would to join the two tables:
UPDATE sf
SET sf.mobile = l.mobile_no
From
sarufoo sf
JOIN logan l ON sf.homep = l.homep
You have to correlate the row you are updating with the row you are selecting from. Otherwise your subselect (the SET one) will return every row it has.
UPDATE sarufoo
SET sarufoo.mobile = (SELECT mobile_no FROM logan WHERE sarufoo.homep = logan.homep)
WHERE sarufoo.homep IN (SELECT homep FROM logan);

Duplicate Query in Microsoft Access or SQL Server

I have over 1,000 entries with duplicate values in an Access database. I want to create a column called Duplicate and set it to true if a record is NOT THE FIRST ONE with a particular value. This means, if the record is the first one with the value "Red Chair", the Duplicate field is set to false, but all subsequent records with value "Red Chair" will have the Duplicate field set to true.
How do I perform this query in Access?
This database will be upsized to an SQL Server database, so an option for me is to 'ignore' the duplicate records while retrieving the records in my SQL query. If this option is viable, I'd like to know how to do this in SQL as alternative. Thanks.
You will have to use subqueries. Try this
UPDATE Tabelle1 SET Tabelle1.b = 'Duplicate'
WHERE
((Tabelle1.[ID] In
(SELECT Tabelle1.[ID] FROM Tabelle1 WHERE
((Tabelle1.[a] In
(SELECT [a] FROM [Tabelle1] As Tmp GROUP BY [a] HAVING Count(*)>1 )
)
AND
(Tabelle1.[ID] Not In
(SELECT min([id]) FROM [Tabelle1] as grpid GROUP BY [a] HAVING Count(*)>1)
));
)));
I'm no expert on the Access dialect, but this adaptation of RJIGO's answer or something similar may also work and be more efficient:
UPDATE Tabelle1 SET
b = 'Duplicate'
WHERE
Tabelle1.[ID] > (
SELECT min([id])
FROM [Tabelle1] as T2
WHERE T2.[a] = Tabelle1.[a]
);
I hope this sql help u:
SELECT table.field, Count(table.field) AS test, IIf([test]>1,"TRUE","FALSE") AS check
FROM table
GROUP BY table.field, IIf([test]>1,"TRUE","FALSE");

Resources