The cursor SQL_CURLH200C1 is not in a prepared state - sql

Can anyone provide an explanation for the following:
select * from 'table' as t where t.identifier = 1234567890
Returns:
THE SQL STATEMENT IS NOT SUPPORTED. SQLCODE=-142, SQLSTATE=42612
select * from 'table' as t where t.identifier = 12345
Returns:
1 row(s)
Identifier is defined as PIC S9(11) COMP-3, DB2

'table' is a string literal with the value table therefor it cannot be used as a table name.
If your table is really called table, then you need to use this:
select * from "TABLE"
or
select * from "table"
depending on how you created that table named table.

Related

How to Dynamically use a table from SSIS expression

I have Variable [User::Tablename1] String, I need an Expression that can evaluate at runtime, to check,
If Tablename1 is 'Customer'
select * from dbo.Customer where CustID = 1
Else If Tablename1 is any other tablename
"select * from dbo"**Tablename** where ProdID= 1
My concern is how to make the tablename dynamic and the filter column.
You should create two variables with these texts in the expression for your Select variables:
1: "select * from dbo."+[User::Tablename1]+ "where ProdID= 1"
2: "select * from dbo.Customer where CustID = 1"
Then you can use a sql command text for getting your table names and pass them as a input to a for each loop container to set it to your [User::Tablename1] variable.
In the for each you can use one Expression Task to compare 'Customer' with your [User::Tablename1] value.
At the end, by using the green arrow of the button of expression task, you can set the constraint with the result output. If the result is yes you can execute the Second select, else you execute the first select with any given table name.

Regex put name schema for 'table' in select query syntax

How can I put with regex, schema name table, for query select?
Query doesn't have fixed size.
Example:
select * from t1 union select * from t2
Result:
select * from schema1.t1 union select * from schema1.t2
Thanks!
(1) If you are using it in a code, maybe you can do a search and replace at front end as Luk suggested.
(2) Alternatively, in your current session you can set default schema as schema1 so your query would work without schema name. But it would depend on your database name.
Like in Oracle you can do
ALTER SESSION SET CURRENT_SCHEMA=schema1
Now select * from t1 would effectively mean select * from schema1.t1.
You can search how to set default schema in <your database> in google and you would get syntax for other databases as well.

HSQL execute condition LIKE ANY

I'm trying to execute an SQL statement, with a where clause that does something like:
WHERE col LIKE ANY (values...)
According to the HSQLDB documentation it seems to me that I should be able to do it:
condition
{ ...
| value [NOT] LIKE value [ESCAPE] value }
value
[+ | -] { term [{ + | - | * | / | || } term]
| ( condition )
| function ( [parameter] [,...] )
| selectStatement giving one value
| {ANY|ALL} (selectStatement giving single column)
However, this does not seem to work.
I can execute this:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with single value)
But any of these will give me an error:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with multiple values)
-> cardinality violation
SELECT * FROM table WHERE col LIKE ANY (selectStatement giving 1 column with single value)
SELECT * FROM table WHERE col LIKE ANY (selectStatement giving 1 column with multiple values)
-> unexpected token: SELECT
Can you help me understand what I'm doing wrong?
Is this not supported, or am I misunderstanding the documentation?
Thanks!
Depending on what you want to achieve, you can use the following:
This query uses the value returned by the subquery for LIKE comparison. The value normally has a escape character, for example super%, which matches all words beginning with super:
SELECT * FROM table WHERE col LIKE (selectStatement giving 1 column with single value)
This query uses the values returned by the subquery as a list. Each of the list values is compared to the col value and when the first exact match is found, the result is true. This type of query is used to return a subset of the rows from the table that have a value that is in the list returned by the subquery:
SELECT * FROM table WHERE col = ANY (selectStatement giving 1 column with multiple values)
I am late to the game here, but would like to add the idea of a colleague of mine to use EXISTS() to allow multiple LIKE/ILIKE conditions.
So that query simulates the "x LIKE ANY('{}')"
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
EXISTS(SELECT *
FROM
UNNEST (ARRAY['a%', 'b%', 'c%']) AS t(like_value)
WHERE
LOWER(tables.table_name) LIKE LOWER(like_value))
ORDER BY
tables.table_name
is equivalent to that in Postgresql
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
tables.table_name ILIKE ANY ('{a%, b%, c%}')
ORDER BY
tables.table_name
Additionally, if you would like to use that from Java using prepared statements and to prevent Hsqldb from throwing a "General error" you have to cast the placeholder like so:
SELECT
tables.table_name
FROM
information_schema.tables
WHERE
EXISTS(SELECT *
FROM
UNNEST (CAST(? as VARCHAR(2048) ARRAY)) AS t(like_value)
WHERE
LOWER(tables.table_name) LIKE LOWER(like_value))
ORDER BY
tables.table_name

SQL pattern matching in search conditions

I have a table with a field name.
For a given name like ABC I want to get the records with that name and the records which have an L appended at the end.
So for ABC I want all records with name either ABC or ABCL.
I tried getting the records using the following code but it doesn't work.
SELECT * FROM tbl
WHERE name like "ABC[|L]"
I am using TSQL.
How can pattern match these names?
Use this SQL:
SELECT * FROM tbl WHERE name IN ('ABC', 'ABCL')
If you are using this SQL within a Stored Procedure / Function, something like following would work. Assuming, you are passing in the value for name in a #name variable.
SELECT * FROM tbl WHERE name IN (#name, #name + 'L')
Use the IN operator.
SELECT *
FROM tbl
WHERE name IN ('ABC', 'ABCL')
In case you will be using variables instead of literal strings, try this:
select *
from tbl
where name like (#YourName + #ExtraLetter)
or name = #YourName

Using SQL query to determine if a table exists

Guys is there any other way to determine a table exists other than below
select count(*) from <table> where rownum =1
select * from user_table where table_name=<table>
kindly let me know the best way to check whether a table exists using oracle sql.
Thanks for the answer , my requirement is to check from the first date of current month ie 01/12/2010 with table name in the format suresh_20101201 exists in the database, if not then it should check for table suresh_20101202 and thereon till suresh_20101231 . is it possible to do in oracle sql query.
You can do this (in oracle, in mssql there is a bit different):
select count(*)
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'your_table_name';
In most sql servers there is a system domain where you can query for a table's existence. It's highly implementation specific though. For example, in recent versions of MySql:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
AND table_name LIKE 'whatever'
You need to ask your server's system catalog. Not sure what database you meant but for SQL Server it would be:
select * from sys.tables where name='your-table-name-'
Used this in Oracle SQL Developer:
SELECT COUNT(*) FROM DUAL WHERE EXISTS (
SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'myschema' AND OBJECT_NAME = 'your_table_name')
This will return either a 0 or 1 if your table exists or not in the ALL_OBJECTS records.
Below query can be triggered to Oracle for checking whether any Table present in DB or not:
SELECT count(*) count FROM dba_tables where table_name = 'TABLE_NAME'
Above query will return count 1 if table 'TABLE_NAME' is present in Database
Look in the schema, might event be able to use sys.objects and check for a type at the same time.....
Something like