Delete in COBOL DB2 with fetch first 100 rows - db2

Below is my query that am using in cobol db2 module
EXEC SQL
DELETE FROM AA_AA
WHERE A=0
FETCH FIRST 30 ROWS ONLY
END-EXEC
I could see the below error while compiling
INVALID KEYWORD 'FETCH' VALID SYMBOLS ARE: AND OR QUERYNO SKIP WITH DELETE FROM AA_AA WHERE A=0 FETCH FIRST 30 ROWS ONLY

Try this:
DELETE FROM (SELECT * FROM AA_AA WHERE A = 0 FETCH FIRST 30 ROWS ONLY)

Related

SQL3116W The field value in row and column is missing, but the target column is not nullable. How to specify to use Column Default

I'm using LOAD command to get data into a table where one of the columns has the default value of the current timestamp. I had NULL value in the data being read as I thought it would cause the table to use the default value but based on above error that's not the case. How do I avoid the above error in this case?
Here is the full command, input file is text file: LOAD FROM ${LOADDIR}/${InputFile}.exp OF DEL MODIFIED BY COLDEL| INSERT INTO TEMP_TABLE NONRECOVERABLE
Try:
LOAD FROM ${LOADDIR}/${InputFile}.exp OF DEL MODIFIED BY USEDEFAULTS COLDEL| INSERT INTO TEMP_TABLE NONRECOVERABLE
This modifier usedefaults has been available in Db2-LUW since V7.x, as long as they are fully serviced (i.e. have had the final fixpack correctly applied).
Note that some Db2-LUW versions place restrictions on usage of usedefaults modifier, as detailed in the documentation. For example, restrictions relating to use with other modifiers, or modes or target table type.
Always specify your Db2-server version and platform when asking for help because the answer can depende on these facts.
You can specify which columns from the input file go into which columns of the table using METHOD P - if you omit the column you want the default for it will throw a warning but the default will be populated:
$ db2 "create table testtab1 (cola int, colb int, colc timestamp not null default)"
DB20000I The SQL command completed successfully.
$ cat tt1.del
1,1,1
2,2,2
3,3,99
$ db2 "load from tt1.del of del method P(1,2) insert into testtab1 (cola, colb)"
SQL27967W The COPY NO recoverability parameter of the Load has been converted
to NONRECOVERABLE within the HADR environment.
SQL3109N The utility is beginning to load data from file
"/home/db2inst1/tt1.del".
SQL3500W The utility is beginning the "LOAD" phase at time "07/12/2021
10:14:04.362385".
SQL3112W There are fewer input file columns specified than database columns.
SQL3519W Begin Load Consistency Point. Input record count = "0".
SQL3520W Load Consistency Point was successful.
SQL3110N The utility has completed processing. "3" rows were read from the
input file.
SQL3519W Begin Load Consistency Point. Input record count = "3".
SQL3520W Load Consistency Point was successful.
SQL3515W The utility has finished the "LOAD" phase at time "07/12/2021
10:14:04.496670".
Number of rows read = 3
Number of rows skipped = 0
Number of rows loaded = 3
Number of rows rejected = 0
Number of rows deleted = 0
Number of rows committed = 3
$ db2 "select * from testtab1"
COLA COLB COLC
----------- ----------- --------------------------
1 1 2021-12-07-10.14.04.244232
2 2 2021-12-07-10.14.04.244232
3 3 2021-12-07-10.14.04.244232
3 record(s) selected.

Snowflake How to get Records failed in Copy command

is it possible to get records which failed during Copy command in Snowflake from internal stage to snowflake table?
I am trying to load error recrods in a error table during Copy command execution . Copy Command used:
Copy into table ( col1, col2,col3,col4) from ( select $1,$2,$3,56 from #%table) ON_ERROR=CONTINUE
To get all the bad records, you can run the copy with VALIDATION_MODE = 'RETURN ERRORS'. Then use the RESULT_SCAN from the validation in an insert statement.
If one of your columns is unique (i.e. col1), maybe you can compare rows in the table with the rows in the stage:
select $1 from #%table
MINUS
select col1 from table;
Please check below select statement after copy command
select rejected_record from table(validate(test_copy , job_id => '_last')) ;

DB2 - If table is empty for date X, insert, else go on

--DB2 version 10 on AIX
I have a stored procedure, which I need to update. And want to check if there is data for a certain date. If data exists, go on, else run insert and then go on.
IF (SELECT COUNT(*)
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014' < 1)
THEN (INSERT INTO SCHEMA1.TABLE1_STEP1 (SELECT * FROM SCHEMA2.TABLE2 FETCH FIRST 2 ROWS ONLY))
END IF;
This errors-out.
DB2 Database Error: ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "(" was found following "/2014') < 1) THEN". Expected tokens may include: "". SQLSTATE=42601
Any thoughts on what's wrong?
I'm guessing you probably want the less than sign outside of the parenthesis...
However, as an aside, you can also do this kind of statement without an IF (although, I don't have an AIX DB2 available to check for sure. It worked on DB2 for z/OS and LUW, however):
INSERT INTO SCHEMA1.TABLE1_STEP1
SELECT *
FROM SCHEMA2.TABLE2
WHERE NOT EXISTS (
SELECT *
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014'
)
FETCH FIRST 2 ROWS ONLY
Also, you're not providing an ORDER BY on the SCHEMA2.TABLE2 select, so your results could come back in any order (whatever is "easiest" for the database engine)... order is not guaranteed unless you provide the ORDER BY statement.

DB2 Suppress 'The command completed with no results returned'

I'm running SQL scripts on DB2 9.7.0.6 (AIX).
Client is Embacadero's Rapid SQL 7.7
I insert the following code at the beginning of my stored procedure to calculate the date for me.
DROP VARIABLE FOLM;
CREATE VARIABLE FOLM CHAR (10);
SET FOLM = (SELECT CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS - 1 MONTH
FROM SYSIBM.SYSDUMMY1);
When I run the stored proc, the above code produces these three lines before the results of my procedure:
The command completed with no results returned
The command completed with no results returned
The command completed with no results returned
How can I suppress this?

How can I see the numbers of rows a query affects when deleting

I want to see how many rows my delete query effects so I know its correct.
Is this possible using pgadmin?
Start a transaction, delete and then rollback;
In psql :
test1=> begin;
BEGIN
test1=> delete from test1 where test1_id = 1;
DELETE 2
test1=> rollback;
ROLLBACK
In pgAdmin (in the "History" tab on the "Output pane"):
-- Executing query:
begin;
Query returned successfully with no result in 16 ms.
-- Executing query:
delete from test1 where test1_id = 1;
Query returned successfully: 2 rows affected, 16 ms execution time.
-- Executing query:
rollback;
Query returned successfully with no result in 16 ms.
I'm not sure how to automatically do this but you can always do a select then a delete.
SELECT COUNT(*) FROM foo WHERE delete_me=true;
DELETE FROM foo WHERE delete_me=true;
As Andrew said, when doing interactive administration, you can just replace DELETE by SELECT COUNT(*).
If you want to this information in a program of yours (after executing the DELETE), many programming languages provide a construct for this. For example, in PHP it's pg_affected_rows and in .NET it's the return value of ExecuteNonQuery.
Use RETURNING and fetch the result like you would fetch a SELECT-result:
DELETE FROM test1 WHERE test1_id = 1 RETURNING id;
This works as of version 8.2