DELETE DB2 using OPENQUERY returns error about key column insufficient - linked-server

I am trying to delete rows on DB2 i Series using a link server but am getting an error message.
Key column information is insufficient or incorrect. Too many rows were affected by update
This is the query
DELETE FROM DB2
FROM OPENQUERY(TEST1, 'SELECT FIELD1 FROM LIBRARY1.FILE1') DB2
INNER JOIN #DLT_FILE1 DLT ON
DB2.FIELD1 = DLT.FIELD1
There is one column in both temp file #DLT_FILE1 and DB2 table LIBRARY1.FILE1

Db2 for IBM i (aka DB2-400) doesn't allow positioned deletes, ie from a cursor, that uses joins.
AMarc's suggestion might work, once you fix the syntax...I believe this is correct.
DELETE
FROM OPENQUERY(TEST1
, 'SELECT FIELD1 FROM LIBRARY1.FILE1 DB2
WHERE EXISTS (SELECT 1
FROM #DLT_FILE1 DLT
WHERE DB2.FIELD1 = DLT.FIELD1)
')

Related

DB2 Encrypt Function - Insert Encrypt data

I am trying to insert data into table from application
statement required "insert into table1 (memberid) values (encrypt('1111','abcdef'))".
however application is preparing as
insert into table1 (memberid) values ('encrypt('1111','abcdef')') and Rows are getting inserted.
while select decrypt_char(memberid,'abcdef') from table1
getting SQL20146N - The Decryption function failed. The data is not encrypted.
You may get this error if you have made mistakes in your code or column declaration.
You should avoid using ENCRYPT() and DECRYPT_CHAR() because they are considered insecure , and are deprecated so might be removed from a future release of Db2. Instead use Db2 native encryption or underlying file system encryption.
Below is a worked example for your case, on Db2-LUW:
create table table1(memberid varchar(64) for bit data)
insert into table1 (memberid) values (encrypt('1111','abcdef'))
select hex(memberid) from table1
1
--------------------------------------------------------------------------------------------------------------------------------
0828D8FFB804AFD51CFBD754BD9D234F
select decrypt_char(memberid, 'abcdef') from table1
1
--------------------------------------------------------
1111

near " ": syntax error: UPDATE tablename

I use SQLLite. My sql code has only one line: UPDATE loty and it gives:
near " ": syntax error: UPDATE loty
Once I overcome this I will proceed with further lines as I want to insert values to a column in loty from another table.
Why this one line of code (UPDATE loty) gives this syntax error?
You are using incomplete syntax.
The original syntax is:-
UPDATE table_name SET field1 = new-value1, field2 = new-value2
[WHERE Clause]
You never relate the two tables properly. SQLite does not support update join, but it does support using a correlated subquery in the SET clause:
UPDATE loty t1
SET destinationairportcode = (SELECT destinationairportc‌​ode FROM kody_lotnisk t2
WHERE t1.destination = t2.destination);
You never gave us any clear logic regarding whether you want to update every record in the loty table, or just certain records. If the latter, then you can add a WHERE clause to the outer query.

Strange result in DB2. Divergences queries

A strange thing, that I don't know the cause, is happenning when trying to collect results from a db2 database.
The query is the following:
SELECT
COUNT(*)
FROM
MYSCHEMA.TABLE1 T1
WHERE
NOT EXISTS (
SELECT
*
FROM
MYSCHEMA.TABLE2 T2
WHERE
T2.PRIMARY_KEY_PART_1 = T1.PRIMARY_KEY_PART_2
AND T2.PRIMARY_KEY_PART_2 = T1.PRIMARY_KEY_PART_2
)
It is a very simple one.
The strange thing is, this same query, if I change COUNT(*) to * I will get 8 results and using COUNT(*) I will get only 2. The process was repeated some more times and the strange result is still continuing.
At this example, TABLE2 is a parent table of the TABLE1 where the primary key of the TABLE1 is PRIMARY_KEY_PART_1 and PRIMARY_KEY_PART_2, and the primary key of the TABLE2 is PRIMARY_KEY_PART_1, PRIMARY_KEY_PART_2 and PRIMARY_KEY_PART_3.
There's no foreign key between them (because they were legacy ones) and they have a huge amount of data.
The DB2 query SELECT VERSIONNUMBER FROM SYSIBM.SYSVERSIONS returns:
7020400
8020400
9010600
And the client used is SquirrelSQL 3.6 (without the rows limit marked).
So, what is the explanation to this strange result?
Without the details (including, at least, the exact Db2 version and DDL for both tables and their indexes) it can be just anything, and even with that details only IBM support will be really able to say, what is the actual reason.
Generally this looks like damaged data (e.g. differences in index vs table data).
Worth to open the support case with IBM.

Inserting records into a SQLite Database isn't actually inserting

I set up a SQLite Database DB Connection via the CF Admin after installing the JDBC Driver. After setting it up I got a successful connection message. I also know that I connected successfully because if I run a simple select query it doesn't fail out and if I run a CFDump it shows the proper columns. To further test this simple select statement, if I changed the table name it does fail. So, it's not a connection issue.
I am simply trying to insert records into a table and then check to see if those records were added. These are the queries I am using:
<cfquery datasource="fooDB" name="foo">
INSERT INTO FooTable
(FooColumn)
VALUES
('Test')
</cfquery>
<cfquery datasource="fooDB" name="checkIfwasSuccessful">
SELECT *
FROM FooTable
</cfquery>
This is my SQlite table creator:
CREATE TABLE FooTable (
id INTEGER PRIMARY KEY,
FooColumn TEXT,
OtherColumn1 TEXT,
OtherCOlumn2 TEXT
);
The CFDump of the query checkIfwasSuccessful is an empty result.
Any ideas??
Thank you in advance!!
Use Cftransaction to verify that your query is being commmited.
Have you tried either a) supplying an id with the insert, or b) using the AUTOINCREMENT keyword after PRIMARY KEY?

Deleting rows from SQLite table when no match exists in another table

I need to delete rows from an SQLite table where their row IDs do not exist in another table. The SELECT statement returns the correct rows:
SELECT * FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL;
However, the delete statement generates an error from SQLIte:
DELETE FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL;
The error is: SQLite Error 1 - near "left": syntax error. Is there another syntax I could use?
SQLite apparently doesn't support joins with the delete statement, as you can see on the Syntax diagrams. You should however be able to use a subquery to delete them.
ie.
DELETE FROM cache WHERE id IN
(SELECT cache.id FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL);
(Not tested)
Since you going down the route of subquery, might as well get rid of the join altogether and simplify the query:
DELETE FROM cache WHERE id NOT IN (SELECT id from main);
Solution from #wimvds didn't work for me, so I modified the query and removed WHERE condition:
DELETE FROM FirstTable WHERE firstTableId NOT IN (SELECT SecondTable.firstTableId FROM SecondTable LEFT JOIN FirstTable ON FirstTable.firstTableId=SecondTable.firstTableId)
This deletes all the rows from FirstTable that do not have their id assigned to any row in SecondTable.

Resources