in sql anywhere, can i get the SQLSTATE full message text - sqlanywhere

refer to list of SQLSTATE
In my Procedure codes runtime, I can get the SQLSTATE 5 characters string. But how can I get the full message too?

Use the ERRORMSG function
SELECT ERRORMSG( '01W02' );
Using temporary table

Related

How can I pass a SQL to Cursor which is coming as string in DB2?

I want to pass a SQL to my stored proc as input as a string and my stored proc will pass this SQL as a string to a cursor and provide the output. But a Cursor doesn't accept a SQL as string and we need to prepare the SQL statement and then pass it onto the Cursor. I am not able to make that part working. Please find my sample code here :
Create Procedure abc(IN stmt VARCHAR(500))
SPECIFIC abc
LANGUAGE SQL
DYNAMIC RESULT SETS 1
Re: BEGIN
DECLARE Instmt VARCHAR(500)
PREPARE Instmt FROM stmt;
EXECUTE Instmt;
DECLARE v_cur CURSOR WITH RETURN FOR Instmt;
OPEN v_cur;
END Re
--call abc('select ename,sal from emp')
It gives me error as such that Cursor is expecting a SQL statement. So more or less I am not able to PREPARE the SQL properly to feed it to the CURSOR.
Error Message:
SQL0104N "An expected token "" was found following "".Expected token may include: ""
Please let me know how I could get it working.
Try this one
--#SET TERMINATOR #
CREATE OR REPLACE PROCEDURE TEST_PROC(IN stmt VARCHAR(500))
SPECIFIC TEST_PROC
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE test_cur CURSOR WITH RETURN TO CALLER FOR test_stmt;
PREPARE test_stmt FROM stmt;
OPEN test_cur;
END#

Not able to drop schema in DB2 Using ADMIN_DROP_SCHEMA Stored Procedure

I found at several places to be able to drop a schema in DB2 along with all of its contents (indexes, SPs, triggers, sequences etc) using
CALL SYSPROC.ADMIN_DROP_SCHEMA('schema_name', NULL, 'ERRORSCHEMA', 'ERRORTAB');
However, I am getting the following error while using this command:
1) [Code: -469, SQL State: 42886] The parameter mode OUT or INOUT is not valid for a parameter in the routine named "ADMIN_DROP_SCHEMA" with specific name "ADMIN_DROP_SCHEMA" (parameter number "3", name "ERRORTABSCHEMA").. SQLCODE=-469, SQLSTATE=42886, DRIVER=4.22.29
2) [Code: -727, SQL State: 56098] An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-469", SQLSTATE "42886" and message tokens "ADMIN_DROP_SCHEMA|ADMIN_DROP_SCHEMA|3|ERRORTABSCHEMA".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.22.29
Can anyone help me suggest what's wrong here? I tried to look at several places but didn't get any idea. It doesn't seem it's an authorization issue. Using DB2 version 11.5.
You are using the ADMIN_DROP_SCHEMA procedure parameters incorrectly, assuming you are CALLing the procedure from SQL and not the CLP.
The third and fourth parameters cannot be a literal (despite the documentation giving such an example), instead they must be host-variables (because the the procedure requires them to be input/output parameters).
If the stored-procedure completes without errors it sets these parameters to NULL. so your code should check for this.
If the stored-procedure detects errors, it creates and adds rows to the specified table and leaves the values of these parameters unchanged, and you must then query that table to list the error(s). You should drop this table before calling the stored procedure otherwise the procedure will fail with -601.
Example:
--#SET TERMINATOR #
drop table errschema.errtable#
set serveroutput on#
begin
declare v_errschema varchar(20) default 'ERRSCHEMA';
declare v_errtab varchar(20) default 'ERRTABLE';
CALL SYSPROC.ADMIN_DROP_SCHEMA('SOMESCHEMA', NULL, v_errschema, v_errtab);
if v_errschema is null and v_errtab is null
then
call dbms_output.put_line('The admin_drop_schema reported success');
else
call dbms_output.put_line('admin_drop_schema failed and created/populated table '||rtrim(v_errschema)||'.'||rtrim(v_errtab) );
end if;
end#
You can use global variables if you would like to use ADMIN_DROP_SCHEMA outside of compound SQL
E.g.
CREATE OR REPLACE VARIABLE ERROR_SCHEMA VARCHAR(128) DEFAULT 'SYSTOOLS';
CREATE OR REPLACE VARIABLE ERROR_TAB VARCHAR(128) DEFAULT 'ADS_ERRORS';
DROP TABLE IF EXISTS SYSTOOLS.ADS_ERRORS;
CALL ADMIN_DROP_SCHEMA('MY_SCHEMA', NULL, ERROR_SCHEMA, ERROR_TAB);

Error Regarding Running Stored procedure using liquibase

I am try to execute the stored procedure using liquibase having the / delimiter in
sql file the Database Is db2. The problem is it is giving me error as DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=END-OF-STATEMENT;demoTable = ((demovar;) not able to understand the cause as all other stored procedure in same file get executed well..
using the following changeset
and demo.sql has the stored procedure and set demovar declare in it
any suggestion what is cause
Your error message says:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601
-104 represents SQL0104N and here is explanation.
SQL0104N An unexpected token token was found following text. Expected tokens may include: token-list.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.messages.sql.doc/com.ibm.db2.luw.messages.sql.doc-gentopic1.html#sql0104n
Explanation
A syntax error in the SQL statement or the input command string for the SYSPROC.ADMIN_CMD procedure was detected at the specified token following the text text. The text field indicates the 20 characters of the SQL statement or the input command string for the SYSPROC.ADMIN_CMD procedure that preceded the token that is not valid.
As an aid, a partial list of valid tokens is provided in the SQLERRM field of the SQLCA as token-list. This list assumes the statement is correct to that point.
This message can be returned when text is passed to the command line processor (CLP) in command mode and the text contains special characters that are interpreted by the operating system shell, such as single or double quotes, which are not identified with an escape character.
The statement cannot be processed.
So you may need to follow "User response" section of the page and correct SQL statement in demo.sql.
Hope this helps.

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.

Custom error message for postgresql check constrint

I have the "valid_id" check constraint on my requests table. But when it violates the constraint it shows following error
ERROR: new row for relation "requests" violates check constraint
"valid_name" DETAIL: Failing row contains ....
But instead of that I want to show message like "Failed to insert record. name is required".
Is there any way to show the custom error message in PostgreSQL?
This is kind of advanced territory here because you want to be pretty familiar with SQL states as well as existing error messages before you get started. Note that you want to re-used existing sql states as appropriate so that the application doesn't know you have overridden your check constraint.
But what you can do is create a function which runs the check and issues a raise exception if the check fails. Something like:
CREATE FUNCTION check_is_not_null(value text, column_name text) RETURNS BOOL
LANGUAGE plpgsql AS $$
begin
IF $1 IS NULL THEN
RAISE EXCEPTION 'Error: % is required', $2;
END IF;
RETURN TRUE;
END;
$$;
If you are using 8.4 or higher, you can specify an SQL state.