I am using the with clause and I have recently run into an odd issue. For even simple queries I am getting an incorrect syntax error returned and I can't figure out why that would be.
Whenever I run code as simple as:
WITH table1 AS (Select value1, value2 from table1)
I get an 'Incorrect syntax near ')' ' error.
I was using this before without trouble, so I feel like I'm making a really obvious dumb mistake that I'm just not catching. Could anyone point out what I'm doing wrong?
It is a CTE - you need to select at the end of it.
i.e.
WITH cte AS (
SELECT
value1,
value2
FROM table1
)
SELECT * FROM cte
Additionally, having the CTE named the same as the table from which you're pulling from, is not a very good idea. You had them both as table1.
I think you are missing a part of with as ,in your case with as must follow with select:
WITH table1 AS (Select value1, value2 from table) SELECT * FROM table1
Related
I am currently learning SQL and I tried testing something but it does not work.
The query that I tried is the following:
SELECT acc_id
FROM
(
SELECT *
FROM company
);
The inner query must return the whole table and the outter query must select from that table only a specific column. Simple as it seems this produces an error. The error message is:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6" (line 6 being the last line).
I can't figure out whats the issue.
You need to give an alias to your subquery:
SELECT acc_id
FROM
(
SELECT *
FROM company
) AS some_alias;
Although your query can be simplified into:
SELECT acc_id
FROM company;
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)
My Need is to get the count of query that gets Executed. I got the Query to get Count Like
Select Count(*) from ( Select Query) from myTable
but the problem is when user enters a special character like the comma, period etc it shows error like Syntax error at or near ')'. but from the user point of view, there is no ')'.
How do solve this? Is there any other way to get the count. My final output should Syntax error at or near ',' or '.' etc if they are present in the Query
You need sub-query and for that you can use CTE(common table expression)
Select Count(*) from ( ) from alias-- your sql
this way you can do post sql server and postgre
with t1 as
(
Select Query ---- your sql code
)
select Count(*) from t1 --- count from alis
I tried many syntax for the "with" with sybase 12
;WITH myCte AS (SELECT 1 AS FOO) SELECT * FROM myCte
I also tried nearly everything in this:
http://dcx.sybase.com/1100/en/dbusage_en11/commontblexpr-s-5414852.html
I tried with and without column names for the cte , with and without aliasing columns... etc
What is wrong? I keep getting
"Incorrect syntax near the keyword With"
I checked:
select name from master..spt_values where type="W" order by name
"with" is there, so it is known.
What am i doing wrong ?
I was reviewing this question/answer
Get latest date before date value in row
I thought I would give it a try, but every time I try the 'outer apply'
I get an error: Incorrect syntax near ')'.
In my query, my 'from' statement is not as simple as the example in the question & answer.
My 'from' has multiple joins and then at the every end of my joins I'm trying this:
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
)
Any additional help is much appreciated
I'm using SQLserver 2008 R2
Make sure you alias your outer apply. I don't know why SQL Server is always so unhelpful with this error.
outer apply
(
SELECT top 1 *
from <mytable>
where <mytable.column> in ('1','3')
) AS obligatory_alias
I would actually do something like the following
select ColumnsFromTableAYouCareAbout, max(b.datecolumn) as lastdate from mytable a
inner join mytable b on b.datecolumn < a.datecolumn
group by ColumnsFromTableAYouCareAbout