com.ibm.db2.jcc.am.SqlSyntaxErrorException - db2

I have installed DB2 on my local machine and created a table Users with a column Name.
From my portlets I am executing a simple select statement:
Class.forName("com.ibm.db2.jcc.DB2Driver");
String dburl = "jdbc:db2://localhost:50001/Portlets";
conn = DriverManager.getConnection(dburl,"db2admin","password");
String selectTableSQL = "SELECT Name from Users";
statement = conn.createStatement();
ResultSet rs = statement.executeQuery(selectTableSQL);
when i execute this i get the following exception:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.USERS, DRIVER=3.62.56.
I dont understand why I am getting this exception. There is nothing much in the stack trace as well.

The SQLCODE in your message is what tells you the actual error message. -204 is the error you are looking for. If you cross-reference the Information Center article about -204, you will see the following message:
name is an undefined name.
And if you look back at your exception, you see name = DB2ADMIN.USERS (the SQLERRMC field).
My guess is that you aren't finding anything because you forgot to append a Schema to your table (the DB2ADMIN part was assumed in the error message because that's your login name).

Related

Query returns Error despite being executed succesfully (Robot Framework / JayDeBeApi)

Using the Keyword Query from the Robot Framework DatabaseLibrary JayDeBeApi in conjunction with DB2 like this: ${results}= Query CREATE TABLE SCHEMANAME.TEST_TEMP (id BIGINT, name VARCHAR(25)) is being executed (table exists afterwards).
But nevertheless RobotFramework throws a FAIL and ${results} contains the Message DatabaseError: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-601, SQLSTATE=42710, SQLERRMC=SCHEMANAME.TEST_TEMP;TABLE, DRIVER=4.14.122 and often even a very simple Message Error after running the same statement.
Running the query above (copy/paste) directly within a database SQL window doesn't return any errors.
How is it possible, in RobotFramework the query is executed successfully but nevertheless an error is thrown?
The error SQLCODE=-601 means that you are trying to create an object that already exists. So when you say that the table exists afterwards, it means that it existed before you ran the statement. I don't know the framework you are using, but the explanation by #pavelsaman in comment seems to be a very likely cause.

Copy data from postgres to db2

I'm trying to copy data from postgres to db2. Just a full table.
I found these instructions and am following them, but they're failing for me on the db2 side.
currently I'm doing:
engine_pstgrs._metadata = MetaData(bind=engine_pstgrs)
engine_pstgrs._metadata.reflect(engine_pstgrs)
table_one = Table('table_one', engine_pstgrs._metadata, schema='prod_schema')
print("Got prod_schema.table_one")
#Create new table
engine_db2._metadata = MetaData(bind=engine_db2)
new_pages = Table('new_pages', engine_db2._metadata, schema='mru')
print("Created new table")
for column in table_one.columns:
print("Column appended to new table")
new_pages.append_column(column.copy())
new_pages.create()
And I'm getting the error:
(ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: Statement Execute Failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token ")" was found following "TABLE MRU.new_pages (". Expected tokens may include: "<table_element_list>". SQLSTATE=42601 SQLCODE=-104 [SQL: '\nCREATE TABLE mru.new_pages (\n)\n\n']
I'm open to trying another way (i.e. Pandas) but I'm not seeing how to do so in a sustainable amount of time (the table is very very large)

Dreamfactory: Database connection to Postgres

First I set up the dream factory by "Bitnami Installer for Windows". Following to https://github.com/dreamfactorysoftware/dsp-core/wiki/Install-Microsoft-Windows
Then I follow this add-a-rest-api-to-any-sql-db-in-minutes to add Services to my Remote Postgres Database.
On the "API Docs" tab, it is success to call the GET /db operation. ( getTables() - List all table names).
I got the following error when trying to call GET /db/{table_name} operation ( getRecordsByFilter() - Retrieve one or more records by using a filter).
Please help
[app][ERROR ] CDbCommand::fetchAll() failed: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "field_name"
LINE 1: SELECT k.column_name field_name
^. The SQL statement executed was: SELECT k.column_name field_name
FROM "information_schema"."key_column_usage" k
LEFT JOIN "information_schema"."table_constraints" c
ON k.table_name = c.table_name
AND k.constraint_name = c.constraint_name
WHERE c.constraint_type ='PRIMARY KEY'
AND k.table_name = :table
AND k.table_schema = :schema.
Please check the version of PostgreSQL you are connecting to. It turns out that omission of as keyword is supported starting from 8.4.
Here's a quote from 8.4 release notes section E.23.3.3. Queries:
Allow AS to be optional when specifying a SELECT (or RETURNING) column
output label (Hiroshi Saito)
This works so long as the column label is not any PostgreSQL keyword;
otherwise AS is still needed.
Therefore SELECT k.column_name field_name is not valid for 8.3 and below, but SELECT k.column_name AS field_name would work.
If PostgreSQL version is the cause of your problem you have several options:
update the database to 8.4 and above;
patch the Dreamfactory codebase yourself to work around this problem;
raise a ticket in Dreamfactory's bug tracker and wait them to fix it for you.

DB2 query in RazorSQL for getting the next val from a sequence

I am trying to run a DB2 query in RazorSQL for getting the next val from a sequence but getting the following error. Can anybody plz help me out.
VALUES NEXT VALUE FOR SEQ_UPLOAD_MASTER;
ERROR: An undefined object or constraint name was detected.
DB2 SQL error: SQLCODE: -204, SQLSTATE: 42704, SQLERRMC:
DB2INST1.SEQ_UPLOAD_MASTER Error Code: -204
Query = VALUES NEXT
VALUE FOR SEQ_UPLOAD_MASTER
Thanks. I got the solution. I was trying with the wrong syntax. Correct one is
`select nextval for ARCH_TRANCHE.SEQ_UPLOAD_MASTER from sysibm.sysdummy1;`

Table Valued User Defined Functions in DB2 Z/OS

Does anyone know if DB2 v9.1 z/OS supports Table Valued User Defined Functions?
This is what I am trying to create but I keep getting the error message below.
CREATE FUNCTION func_test(v_vchCol CHAR(10))
RETURNS TABLE(col_a char(10), row_cnt integer)
LANGUAGE SQL
SPECIFIC FUNCINFO
NOT DETERMINISTIC
READS SQL DATA
return
select col_1, count(*)
from SCHEMA_NAME.TEST1
where col_1 = v_vchCol
group by col_1;
Error Message:
ERROR [56038] [IBM][DB2] SQL0969N There is no message text
corresponding to SQL error "-4700" in the message file on this
workstation. The error was returned from module "DSNHSMS1" with
original tokens "". SQLSTATE=56038
Any help would be much appreciated
Yes, but it appears to require new function mode which apparently isn't enabled yet in the DB2 instance you're connected to.