Using nested query twice within one query - for FROM and WHERE - sql

I am trying to figure out why the following Microsoft SQL code does not work. I simplified the query as it is quite complex. Basically the part that is not working is the second nested subquery (line FROM a) - I get an error: Invalid object name 'a'.
I would appreciate any advice on why it is not working and how I could make it work. Some background sources on why is it not working would also be helpful, as I struggle to find any information on limitations of nested queries beyond some basics.
SELECT *
FROM (
SELECT ... FROM ...
) a
WHERE x IN(
SELECT x
FROM a
WHERE v1=v2)

I managed to solve my problem thanks to the suggestion in the comments to use CTE.
So I transformed it into:
WITH CTE_1
AS
(
SELECT ... FROM ...
)
SELECT * FROM CTE_1
WHERE x IN(
SELECT x
FROM CTE_1
WHERE v1=v2)

Related

The Query is not working when I am adding a CTE command to it , although the Inside Select Query is giving the coorect result

In First Image Only Select Statement is run , it works fine .
but In Second Image when we added CTE and executed the query , it shows error.
Can you please help me with this ???
It says Incorrect syntax near ';' because you have a WITH statement ended with a ; and no further SELECT after it.
The query itself is fine, that's why it works up top. But when using WITH, you have to follow it up with more querying.
Delete the exiting ; and ddd SELECT * FROM cte1; after it.
A CTE generate a image of a table, so you need to make a select afterwards as well.
with cte1 (...) select * from cte1;

recursive query with select * raises ORA-01789

This is a minimized version of complex recursive query. The query works when columns in recursive member (second part of union all) of recursive CTE are listed explicitly:
with t (c,p) as (
select 2,1 from dual
), rec (c,p) as (
select c,p from t
union all
select t.c,t.p from rec join t on rec.c = t.p
)
select * from rec
I don't get why error ORA-01789: query block has incorrect number of result columns is raised when specified t.* instead.
with t (c,p) as (
select 2,1 from dual
), rec (c,p) as (
select c,p from t
union all
select t.* from rec join t on rec.c = t.p
)
select * from rec
Why t.* is not equivalent to t.c,t.p here? Could you please point me to documentation for any reasoning?
UPDATE: reproducible on 11g and 18 (dbfiddle).
I finally asked on AskTom forum and according to response from Oracle expert Connor McDonald, this behavior is in compliance with documentation, namely the sentence The number of column aliases following WITH query_name and the number of columns in the SELECT lists of the anchor and recursive query blocks must be the same which can be found in this paragraph.
The point is, the expansion of star expression is done after checking whether the numbers of columns are same. Hence one must list columns explicitly, shortening to star is not possible.
Seems like there could be some kind of bug to me. I modified the query slightly just to test various cases and am now able to reproduce an ORA-00600 error in my Oracle 19.6.0.0.0 database! Running the problematic query on apex.oracle.com or on livesql.oracle.com (which is running 19.8.0.0.0) also results in errors. Reporting it to Oracle now!

PostgreSQL:how to update rows in CTE

am running PostgreSQL 9.2.
below given is a sample of my huge and uglyquery
with cte as(
select ....... from aTable
),cte1 as (
select ..... from bTable inner join cte using(anID)
),update_cte as(
update cte set aField=(select somthing from cte1)
)
select * from cte
i need to create a view with the final result.
while executing the above am getting an error which is below.
ERROR: relation "cte" does not exist
I know am doing something bad.hope you can understand what am trying to achieve from the above query.
So please suggest any alternative method for it.
Replies are much appreciated.
Note : the actual query
with cte as(
select ....... from aTable
),update_cte as(
update cte set aField=(select somthing from cte1)
)
You can't do that.
An UPDATE may not reference a CTE term in PostgreSQL, as CTEs are materialized. They aren't just views over the underlying data. (That's sometimes really annoying, but that's how it is).
You can:
CREATE TEMPORARY VIEW someview AS SELECT ... FROM atable;
UPDATE someview SET afield = ...
if you want; that'll work on newer PostgreSQL versions that support automatically updatable views. I think 9.2 does.
Otherwise, I think you want something like:
WITH cte1 as (
select ..... from bTable inner join cte using(anID)
)
update aTable
set aField=(select somthing from cte1)
WHERE ... where clause from cte ...
RETURNING *;
but really, please don't call your CTE terms cte, cte1, etc. Give them useful, descriptive names that tell you what they are. It's like programs full of variables named a through x ... the next person who has to maintain your code, or anyone you ask for help, will not like it.

Not Able to Query Multiple Times from Multiple Common Table Expressions (WITH)?

I was doing some querying today in T-SQL, SQL-Server-2008 and stumbled upon something weird that I didn't understand. Using the query windows, I am trying to query from two common table expressions like so (I stripped out a lot of code to make it more obvious what I was doing):
;WITH temp1 AS (SELECT * FROM dbo.Log)
, temp2 AS (SELECT * FROM dbo.SignalCodeItems300_tbl)
SELECT * FROM temp1
SELECT * FROM temp2
However, only one of the select statements will run, the FIRST one. Regardless of which is which, only the first runs. I assume this is some sort of syntax thing that I'm missing maybe? I get the error "Invalid object name 'temp2'".
Could someone shed some light on this problem? Are there any workarounds for this?
No, this works as it should. A CTE (Common Table Expression) is only available for the first statement after the definition. So in other words, after select * from temp1, they both become unavailable.
The fix would be this:
;WITH temp1 AS (SELECT * FROM dbo.Log)
SELECT * FROM temp1
;WITH temp2 AS (SELECT * FROM dbo.SignalCodeItems300_tbl)
SELECT * FROM temp2
You might want to take a look at the MSDN documentation.
Especially:
Multiple CTE query definitions can be defined in a nonrecursive CTE.
The definitions must be combined by one of these set operators:
UNION ALL, UNION, INTERSECT, or EXCEPT.
You cannot mix and match two different schemas though, as this essentially runs as one query.
Use a view or a user-defined, table-valued function to house your query if you don't want to repeat it explicitly.

problem in select into query

Why m i getting error incorrect syntax near the keyword IN in the following query?
select * into persons_backup IN 'HRMS.mdb' from persons
Thank you
Assuming SQL Server (based on your previous question) you would need
select *
into persons_backup
from HRMS.mdb.persons
or
select *
into HRMS.mdb.persons_backup
from persons
dependant upon what you are trying to do exactly. See SELECT ... INTO syntax here
Assuming you want to add all rows from persons into another table persons_backup:
Insert into persons_backup select * from persons;
Depending on the RDBMS you use, you might have to put () around the select.