I have the following SQL query
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE 'A%'
It works as expected. But the following line results in an error message.
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE (select '%arthur%' from dual)
The error message is the following:
ORA-00933 SQL Command not properly ended
I have tried to close the query with ";" but it is still gives the same error. What could cause this error?
Edit: I need LIKE and can't use IN, lets assume we have only one 'arthur' in the users database.
Your query appears to be correct syntactically. The subquery is a scalar subquery, which returns only one column and at most one row.
In general, you can use LIKE with a subquery using EXISTS:
SELECT u.*
FROM users u
WHERE 1 = 1 AND
EXISTS (SELECT 1
FROM t
WHERE u.user_name LIKE t.col
);
If your actual query is more complicated, then this might solve your actual problem.
i have tried same query as per your,it worked fine.
SELECT * FROM employee e where 1=1 and e.name like (select '%abc%' from dual);
Related
I have a stored procedure that returns a single value. I want to do a SELECT query that both selects the stored procedure's return value, as well as use that value in the WHERE clause.
I tried doing this:
SELECT u.name, myproc(u.id) as p FROM users WHERE p = 'something';
But that didn't work, at least, not on PostgreSQL. I got the error: ERROR: column "p" does not exist
The only way I could get this to work is call the procedure twice, e.g.
SELECT u.name, myproc(u.id) FROM users WHERE myproc(u.id) = 'something';
But that seems wasteful and unnecessary to call the function twice. Is there any way to do this without calling the function twice?
You could use a common table expression (or a subquery);
WITH cte AS (
SELECT u.name, myproc(u.id) as p
FROM users u
)
SELECT * FROM cte WHERE p = 'something'
Basically, a common table expression works like a subquery, you do a SELECT and give the result a name, then you can select from the result as if it were a table.
This is cleaner than a CTE or a subquery
select name, p
from
users
cross join
myproc(id) m(p)
where p = 'something';
Also a CTE is an optimization barrier to the planner. If you opt for a separate query do it in a subquery.
I'm getting a HQL error every time I try to run this subquery. This type of query should work in SQL right? How is HQL handling this type of query differently?
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like HQL does not support IN?
I'm getting a error: "Cannot recognize input near 'SELECT'"
In hql if you want to select all fields, you have two ways:
1-You can remove select clause like:
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
or
2-If you want to use select clause, you must use an alias:
SELECT t
FROM Table t
WHERE t.user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like your syntax is incorrect. Try this:
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
There is no "*" in HQL. Only alias should be present.
SELECT t
FROM Table t
HQL supports "In".
Also, make sure, the java class field name is "user_Id". You may be using the DB column name here.
I'm using oracle 10g and i've a question for you.
Is it possible to "insert" a subquery into a LIKE() operator ?
Exemple : SELECT* FROM users u WHERE u.user_name LIKE ( subquery here );
What i've tried before ->
SELECT * FROM dictionary WHERE TABLE_NAME
LIKE (Select d.TABLE_NAME from dictionary d
where d.COMMENTS LIKE '%table%'
)
WHERE ROWNUM < 100;
It tolds me that my query didnt wokrs -> ORA-00933: la commande SQL ne se termine pas correctement (The sql query doesn't finish correctly) and the last WHERE is out.
I know this is a stupid query, but that just a question that i'm looking for an answer =)
I am guessing you want to do this because you want to compare multiple values at the same time. Using a subquery (as in your example) won't solve that problem.
Here is another approach:
select *
from users u
where exists (<subquery here> where u.user_name like <whatever>)
Or using an explicit join:
select distinct u.*
from users u join
(subquery here
) s
on u.user_name like s.<whatever>
Yeah, why not?
SELECT * FROM users u WHERE u.user_name LIKE (select '%arthur%' from dual);
Example at SQL Fiddle.
IF your subquery return more than 1 row and if this row isn't a string it will not work in your case use IN instead of LIKE
Only if your subquery returns one value
I am using the following query in an MS Access database:
SELECT SD.RollNo, SD.Name , ED.ExamName, (
SELECT count(*)
FROM (
SELECT DISTINCT innerED.StudentId
FROM ExamDetails innerED
WHERE innerED.StudentId=SD.StudentId
)
) AS StudentId
FROM StudentDetails SD
LEFT OUTER JOIN ExamDetails ED
ON SD.StudentId= ED.StudentId
Whenever I execute this query, a dialog box comes up and asks for the value of the parameter SD.StudentId. Why is it asking for this, and how do I stop it from doing so?
MS Access does not understand the SELECT statement on the Count (*) Aggregate. To Access the SQL Statement looks like this.
SELECT DISTINCT innerED.StudentId
FROM ExamDetails innerED
WHERE innerED.StudentId=SD.StudentId
Because the alias AS STUDENTID comes after the end of the statement, this Select statement doesn't recognize it, so it has no idea what .StudendID is so it assumes it's a parameter.
MS Access, when faced with a parameter that has not been identified in the query itself will prompt the user for a value.
Rewrite the query so that this Select statement can identify all the table sources.
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.