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 ?
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;
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 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
I was wondering if this was possible. I have an existing query that uses the WITH clause to apply some aggregated data to a SELECT query like so: (massively simplified)
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
SELECT y, z FROM alias
I now want to INSERT the results of this query into another table.
I have tried the following:
INSERT INTO tablea(a,b)
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
SELECT y, z FROM alias
but I get the error:
Incorrect syntax near ';'.
So I have tried without the semicolon but got the error:
Incorrect syntax near the keyword 'WITH'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Is what I am trying to do possible with different some different syntax?
You will need to place the INSERT INTO right after the CTE. So the code will be:
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
INSERT INTO tablea(a,b)
SELECT y, z
FROM alias
See SQL Fiddle with Demo
Another way without using a CTE is by wrapping it in a subquery,
INSERT INTO tablea(a,b)
SELECT y, z
FROM
(
SELECT y,z FROM tableb
) alias
Semicolon is used to terminate the statement. So when you use ;WITH, terminates the previous statement. However, that's not why you are getting the error here. The problem here is with your INSERT INTO statement, which is looking for VALUES or SELECT syntax.
INSERT INTO statement can be used in 2 ways - by providing VALUES explicitly or by providing a result set using SELECT statement.
In my case the suggested answer was unappliable, I could think it is a metter of SQL Server Version which in my case is SQL Server 2016. Alternatively you could use temp tables through this snippet of code:
;WITH alias (y,z)
AS (SELECT y,z FROM tableb)
SELECT Y,Z
INTO #TEMP_TABLE
FROM alias
Z
with cte as
(
select rowid from batchinfo where datapath like '%thc%'
)
select * from qvalues where rowid in cte
i am getting this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'cte'.
does anyone know what am doing wrong?
you are treating a CTE as a subquery, where instead it should be used more like a table.
try this
;with cte as
(
select rowid from batchinfo where datapath like '%thc%'
)
select * from qvalues
INNER JOIN cte on cte.rowid=qvalues.rowid
As casually alluded to in Al W's answer (and Tony's comment). The fact that the error is being described as occurring on line 6 means that it's not the first statement in the batch. Which means that you need to have a semicolon before the WITH keyword:
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
Also, from Transact-SQL Syntax conventions:
Although the semicolon is not required for most statements in this version of SQL Server, it will be required in a future version.
So it's worth getting in the habit of putting the semicolons in.