DB2 SQL PL CONTINE HANDLER DOES NOT CATCH ERROR - db2

Why does the exception handler not work in the following example ?
db2 => BEGIN
db2 (cont.) => DECLARE YADAMU_INSTANCE_ID VARCHAR(36);
db2 (cont.) => DECLARE FUNCTION_DEFINITION VARCHAR(256);
db2 (cont.) =>
db2 (cont.) => DECLARE CONTINUE HANDLER FOR SQLSTATE '42884' BEGIN
db2 (cont.) => SELECT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),8)
db2 (cont.) => CONCAT '-'
db2 (cont.) => CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
db2 (cont.) => CONCAT '-'
db2 (cont.) => CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
db2 (cont.) => CONCAT '-'
db2 (cont.) => CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
db2 (cont.) => CONCAT '-'
db2 (cont.) => CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000000 )), 'abcdef123456789', '1234567890' ),12)
db2 (cont.) => INTO YADAMU_INSTANCE_ID
db2 (cont.) => FROM SYSIBM.SYSDUMMY1;
db2 (cont.) => END;
db2 (cont.) =>
db2 (cont.) => SELECT YADAMU.YADAMU_INSTANCE_ID()
db2 (cont.) => INTO YADAMU_INSTANCE_ID
db2 (cont.) => FROM SYSIBM.SYSDUMMY1;
db2 (cont.) =>
db2 (cont.) => SET FUNCTION_DEFINITION = 'CREATE FUNCTION YADAMU.YADAMU_INSTANCE_ID() RETURNS VARCHAR(36) DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN ''' CONCAT YADAMU_INSTANCE_ID CONCAT '''';
db2 (cont.) => EXECUTE IMMEDIATE FUNCTION_DEFINITION;
db2 (cont.) => END;
db2 (cont.) => /
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0440N No authorized routine named "YADAMU_INSTANCE_ID" of type "FUNCTION"
having compatible arguments was found. LINE NUMBER=19. SQLSTATE=42884
db2 =>

Try this.
Since you recreate the function every time and don't handle the corresponding error, when the function exists, I've added the OR REPLACE clause to the function definition. Note the absence of ; at the end of the compound statement.
BEGIN
DECLARE YADAMU_INSTANCE_ID VARCHAR(36);
DECLARE FUNCTION_DEFINITION VARCHAR(256);
DECLARE CONTINUE HANDLER FOR SQLSTATE '42884', SQLSTATE '56098'
BEGIN
SELECT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),8)
CONCAT '-'
CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
CONCAT '-'
CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
CONCAT '-'
CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4)
CONCAT '-'
CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000000 )), 'abcdef123456789', '1234567890' ),12)
INTO YADAMU_INSTANCE_ID
FROM SYSIBM.SYSDUMMY1;
END;
PREPARE S1 FROM 'SET ? = YADAMU.YADAMU_INSTANCE_ID()';
EXECUTE S1 INTO YADAMU_INSTANCE_ID;
SET FUNCTION_DEFINITION = 'CREATE OR REPLACE YADAMU.FUNCTION YADAMU_INSTANCE_ID() RETURNS VARCHAR(36) DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN ''' CONCAT YADAMU_INSTANCE_ID CONCAT '''';
EXECUTE IMMEDIATE FUNCTION_DEFINITION;
END
/

Related

SQL Server function PATINDEX in PostgreSQL?

In SQL Server I execute:
select PATINDEX('%\%[0123456789][\ ]%', N'\deftab1134\paperw12240\paperh20000\margl900\margt1440\margr540\margb1440\plain\f1\fs24 That is my report');
---
1
It is correct. I need the same function in PostgreSQL.
I have found this function:
CREATE OR REPLACE FUNCTION patindex( "pattern" TEXT, "expression" TEXT)
RETURNS INT
AS $BODY$
SELECT
COALESCE(
STRPOS(
lower($2)
,(
SELECT
lower(( REGEXP_MATCHES(
lower($2)
,'(' || REPLACE( REPLACE( lower(TRIM( $1, '%' )), '%', '.*?' ), '_', '.' ) || ')'
) )[ 1 ])
LIMIT 1
)
)
,0
)
;
$BODY$ LANGUAGE 'sql' IMMUTABLE;
But it works incorrectly with the same parameters:
select helper2_patindex('%\%[0123456789][\ ]%',
'\deftab1134\paperw12240\paperh20000\margl900\margt1440\margr540\margb1440\plain\f1\fs24 That is my report');
----
87
What is incorrect? what can I fix?

Pass a date parameter on dblink Postgres

How I pass a date parameter to dblink on postgres ?
SELECT * FROM dblink
(
'my connection'
,
'SELECT s.dtcreated FROM realvida.tbstudente s ' ||
' WHERE s.dtcreated' = $1 -- my parameter pass '02-01-2017'
) as result2( dtcreated date )
This way it returned me an error:
operator does not exist: date = integer
change 'SELECT s.dtcreated FROM realvida.tbstudente s ' ||
' WHERE s.dtcreated' = $1
to
format('SELECT s.dtcreated FROM realvida.tbstudente s WHERE s.dtcreated = %L',$1)

Plpgsql: How can i perform the following task in plpgsql?

--Syntax in SQL Server
SET #InnerQuery = #InnerQuery + N'SELECT * from [' + #TableName + '] UNION ALL';
--Here i am trying in plpgsql for above query.
innerquery:= innerquery UNION ALL select * from '%',TableName; --Not in working state
Try this:
execute innerQuery || ' union all select * from "'
|| tableName || '"';

Applying T-SQL...Back to Basics

IM very new to TSQL and am getting my head around stored proceedures etc.
I am using the Code to find a value within one table in my data base, but im not too sure how i would use this code...
Do i need to replace all #* with my relevant table or column name or simply compy paste and execute
Thanks for the help
How do I find a value anywhere in a SQL Server Database?
CREATE PROC SearchAllTables
(#SearchStr nvarchar(100) ) AS BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
DECLARE #Results
TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
SET #TableName = (SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > #TableName
AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0)
WHILE (#TableName IS NOT NULL)
AND (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =(SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(#TableName, 2)
AND TABLE_NAME = PARSENAME(#TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
('SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' + ' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2)
END
END
END
SELECT ColumnName, ColumnValue
FROM #Results
END
Well, to answer your question:
First you have to copy the code to a query window an run it.
That will create the stored procedure.
Now you can call the stored procedute by calling:
EXEC SearchAllTables 'a string of your choice'
Note that you will only get hits from text columns (like 'char', 'varchar', 'nchar', 'nvarchar').

How to express PostgreSQL variable as a string

Is there any way how to express a variable in PostgreSQL as a string?
Example:
\set table_name countries
SELECT 'SELECT * FROM ' || CAST( :table_name, 'text' ) AS specificQuery;
leads to this error:
ERROR: syntax error at or near ","
LINE 1: SELECT 'SELECT * FROM ' || CAST( countries, 'text' ) AS specificQuery;
From the line sample above is obvious, that it doesn't convert "countries" to a string, but it is expressed as name of column/table.
How do I convert it?
Are you looking for something like this?:
SELECT * FROM :"table_name";
http://www.postgresql.org/docs/current/interactive/app-psql.html#APP-PSQL-VARIABLES
Something like this :
SELECT 'SELECT * FROM ' || countries::text AS specificQuery;