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.
Related
When I run this query in Redshift:
select sd.device_id
from devices.s_devices sd
left join devices.c_devices cd
on sd.device_id = cd.device_id
I get an error like this:
ERROR: Spectrum nested query error
DETAIL:
-----------------------------------------------
error: Spectrum nested query error
code: 8001
context: A subquery that refers to a nested table cannot refer to any other table.
query: 0
location: nested_query_rewriter.cpp:726
process: padbmaster [pid=6361]
-----------------------------------------------
I'm not too sure what this error means. I'm only joining to one table I'm not sure which "other table" it's referring to, and I can't find much info about this error on the web.
I've noticed if I change it from left join to join, the error goes away, but I do need to do a left join.
Any ideas what I'm doing wrong?
Redshift reference mentions:
If a FROM clause in a subquery refers to a nested table, it can't refer to any other table.
In your example, you're trying to join two nested columns in one statement.
I would try to first unnest them separately and only then join:
with
s_dev as (select sd.device_id from devices.s_devices sd),
c_dev as (select cd.device_id from devices.c_devices cd)
select
c_dev.device_id
from c_dev
left join s_dev
on s_dev.device_id = c_dev.device_id
The solution that worked for me, was to create a temporary table with the nested table's data and then join the temp table with the rest of the tables I needed to.
For example, if the nested table is spectrum.customers, the solution will be:
DROP TABLE IF EXISTS temp_spectrum_customers;
CREATE TEMPORARY TABLE
temp_spectrum_customers AS
SELECT c.id, o.shipdate, c.customer_id
FROM spectrum.customers c,
c.orders o;
SELECT tc.id, tc.shipdate, tc.customer_id, d.delivery_carrier
FROM temp_spectrum_customers tc
LEFT OUTER JOIN orders_delivery d on tc.id = d.order_id;
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.
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.
SELECT pl_id,
distinct ON (store.store_ID),
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID;
while running this query in postgres server i am getting the below error..How to use the distinct clause..in above code plan 1 is the schema name.
ERROR: syntax error at or near "distinct" LINE 2: distinct ON
(store.store_ID),
You are missing an order by where the first set of rows should be the ones specified in the distinct on clause. Also, the distinct on clause should be at start of the selection list.
Try this:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID
order by store_ID, pl_id;
While using inner join on Pro*C I am getting the below error:
PCC-S-02201, Encountered the symbol "inner" when expecting one of the following:
I've just used a simple inner join. When I searched for solution, I was told that 10g doesn't support these kind of syntax and I should use dynamic SQL instead. Is that true? How to achieve inner join using dynamic SQL?
ProC 10g version doesn't allow inner/outer joins. If you want to have these, you will have to upgrade your ProC compiler.
If you use, 11g you can use the solution suggested here: http://forums.oracle.com/forums/thread.jspa?threadID=665519
Use the old syntax.
Instead of: SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.PK = TABLE2.FK
Use this: SELECT * FROM TABLE1, TABLE2 WHERE TABLE1.PK = TABLE2.FK
For OUTER JOINS just use the (+) sign on the side you want to be nullable:
Instead of: SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.PK = TABLE2.FK
Use this: SELECT * FROM TABLE1, TABLE2 WHERE TABLE1.PK = TABLE2.FK (+)