MS SQLServer Searching to see if a value is contained in other values - sql-server-2008-r2

I come from a background in PostgreSQL.
If I wanted to search all of the columns in all of the tables for any column name that contained "the" somewhere in the value, I would do this:
select *
from information_schema.columns
where column_name ilike '%the%'
This does not work in SQL Server 2008 R2, does anyone have any suggestions? I am running this query so far:
select t.name as table_name, c.name as column_name
from sys.tables as t
inner join
sys.all_columns as c
on
c.object_id = t.object_id
where t.name ilike '%the%'
order by c.name, t.name;
The where t.name ilike '%the%' is what makes the query fail with the following error message:
An expression of non-boolean type specified in a context where a
condition is expected, near 'ilike'.
Does anyone have any suggestions?
Thank you

ilike doesn't exist in sql server, you can use like instead.

Related

COBOL - DCLGEN Host Variable Are Ambiguous

Hi I am trying to run a SQL select query with inner join on tbl1 & tbl2
the DCLGEN of 2 table i.e. DCLTBL1 & DCLTBL2 have few similar column name, due to this I am getting Error message as HOST variables Unresolved as the HOST variables are Ambiguous during compilation.
sql query:
EXEC SQL
SELECT A.COLUMN1, A.COLUMN2
FROM TBL1 A INNER JOIN TBL2 B ON A.COLUMN1 = B.COLUMN2
WHERE A.COLUMN1 = :HOST-VARIABLE1
AND A.COLUMN2 = :HOST-VARIABLE2
END-EXEC.
what could be done to resolve this issue?
I Db2 on IBM Z allows for qualifying your host variables.
Try :HOST-VARIABLE1.:TBL1-DCLGEN-STRUCTURE
I might have that backwards.

Multiple Selection in Datagrip (Running a CTE that's not in your nested query)

I'm looking for a way to run a nested correlated query that requires a CTE created above the script. For example if I had:
with first_cte as (
select *
from a_table
where 1=1
)
select * from
(select
column_1,
column_2,
column_3
from b_table b
inner join first_cte f on f.user_id = b.user_id
where 1=1) x
If I just wanted to test the nested query, it will say that first_cte doesn't exist. Is there a way to highlight the CTE so that it will run when I'm testing nested queries?
I'm using PostgreSQL btw. Thanks!!!
There are Execute selection and Execution options features in DataGrip.

Postgresql DISTINCT and other fields gives Missing FROM-clause

I want to search DISTINCT on a Postgresql 9.4 table with about 300 000 records. It takes almost 8 seconds. I read on this post that using this could speed it up. And it really did. Down to 0.26 sec.
SELECT COUNT(*) FROM (SELECT DISTINCT column_name FROM table_name) AS temp;
Is much faster than
COUNT(DISTINCT(column_name))
When I write this I get the result but I want to add a WHERE clause.
This works but takes over 7 sec.
SELECT COUNT(DISTINCT(species)) FROM darwincore2
WHERE darwincore2.dataeier ILIKE '%nnog%'
This works (0.26 sec.) but fails when I add the WHERE clause:
SELECT COUNT(*) FROM (SELECT DISTINCT species FROM darwincore2) as temp
WHERE darwincore2.dataeier ILIKE '%nnog%'
with:
ERROR: missing FROM-clause entry for table "darwincore2"
Anyone know how I can fix this? Or am I trying to do something that does not work??
The WHERE clause should be in the subquery:
SELECT COUNT(*)
FROM (
SELECT DISTINCT species
FROM darwincore2
WHERE darwincore2.dataeier ILIKE '%nnog%'
) as temp

How do I execute the result of a query using the psql client?

Using the psql client version 8.4.20, I created this select command that generates a bunch of other select commands:
mydatabase=# select concat('select count(*) from ', table_schema, '.', table_name, ';') from information_schema.tables where table_name like '%stockindex_alias%';
select count(*) from de_de.merged_stockindex_alias_de_de;
select count(*) from en_us.merged_stockindex_alias_en_us;
select count(*) from es_la.merged_stockindex_alias_es_la;
select count(*) from fr_fr.merged_stockindex_alias_fr_fr;
select count(*) from nl_nl.merged_stockindex_alias_nl_nl;
select count(*) from pt_br.merged_stockindex_alias_pt_br;
select count(*) from zh_hk.merged_stockindex_alias_zh_hk;
I know I can use \g to store those seven statements into a file, then execute the file with \i.
How can I execute the result of the query (those seven statements) in a single command without the intermediate file? I've tried \set, EXECUTE, searched the web, but can't get it right.
EDIT: The previous select statements erroneously had the word "table" in them, which I have fixed.
I don't know if it works in 8.4; it works in 9.2.
DO $$
DECLARE
x text;
BEGIN
FOR x IN (select concat('select count(*) from ', table_schema, '.', table_name, ';') from information_schema.tables where table_name like '%stockindex_alias%' LOOP
EXECUTE x;
END LOOP;
END;
$$;
Basically you create an anonymous function block and execute it. Function blocks (or whatever the correct name is) allow for variable declaration and dynamic execution.
If you can get by with an approximate count, you can use the system catalogs:
SELECT s.nspname AS locale, c.reltuples AS count
FROM pg_class c
JOIN pg_namespace s ON s.oid = c.relnamespace
WHERE c.relname LIKE '%stockindex_alias%';
This will be most accurate immediately after a VACUUM ANALYZE (assuming you are a superuser or own all the affected tables) and gradually reducing in accuracy as the affected tables are modified.

Why subquery doesn't work in ON clause in DB2

Why this simple query works fine in oracle but doesn't work in DB2:
select *
from
sysibm.dual d1
left join sysibm.dual d2 on 1=1 and exists (select 1 from sysibm.dual)
Moving subquery-involving condition to where clause may help, but that will restrain outer join into inner.
When I try to run the query you have, I get a -338 error, which according to Information Center (see link), there are the following restrictions on the ON clause:
An ON clause associated with a JOIN operator or in a MERGE statement
is not valid for one of the following reasons.
* The ON clause cannot include any subqueries.
* Column references in an ON clause must only reference columns
of tables that are in the scope of the ON clause.
* Scalar fullselects are not allowed in the expressions of an ON clause.
* A function referenced in an ON clause of a full outer join
must be deterministic and have no external action.
* A dereference operation (->) cannot be used.
* A SQL function or SQL method cannot be used.
* The ON clause cannot include an XMLQUERY or XMLEXISTS expression.
I'm not sure if it's possible with your query, but do you think perhaps you could re-write something like this:
select *
from
sysibm.dual d1
left join (
SELECT dl.*,
CASE WHEN EXISTS (SELECT 1 FROM sysibm.dual)
THEN 1
ELSE 0
END AS jn
FROM sysibm.dual dl
) D2
on 1=1 and 1=d2.jn
This works in DB2 V10.1!
No fixpack installed.