Select syntax issue related to ; [duplicate] - sql-server

This question already has answers here:
SQL Server and nested SELECT: ...Incorrect syntax near ')'?
(2 answers)
Closed 1 year ago.
select count(*) from
(select distinct acceptor_id, requestor_id from request_accepted);
When I am running the above, am getting the following error
Msg 102, Level 15, State 1, Line 541
Incorrect syntax near ';'
Any suggestions. Am using SQL Server

You need an alias to the subquery, like:
select count(*) from (select distinct acceptor_id, requestor_id from request_accepted) as subQuery;

Related

How to clone the table in SQL Server? [duplicate]

This question already has answers here:
How to create a table from select query result in SQL Server 2008 [duplicate]
(6 answers)
Closed 10 months ago.
I'm a new in Microsoft SQL, so just lab with the database name "test" , and 1 table imported from excel named "dbo.Sheet1$", so i would like to clone the current table, I wrote the query script like below:
use test;
create table newtable as select * from dbo.Sheet1$
I got the message:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'select'.
Could you please help assist on this ?
Maybe try this
use test;
select * into newtable from dbo.Sheet1$
you can read more about SELECT INTO here

Incorrect syntax near the keyword 'AS'. in SQL Server [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 4 years ago.
I have problem with my SQL Server query:
UPDATE latihan AS t1 ,
(SELECT Equipment, system_status, functloc
FROM latihan
WHERE system_status='ESTO') AS t2
SET t1.functloc = t2.functloc
WHERE t1.supereq = t2.equipment
I just want update the functloc on equipment based functloc on supereq.
The error is:
[Err] 42000 - [SQL Server]Incorrect syntax near the keyword 'AS'.
42000 - [SQL Server]Incorrect syntax near the keyword 'AS'.
I think you want something like this:
update t1 set
functloc = t2.functloc
from latihan t1
inner join (
select Equipment, system_status, functloc
from latihan
where system_status='ESTO'
) t2 on t2.equipment = t1.supereq

Query not running in SQL Server [duplicate]

This question already has answers here:
How do I 'subtract' sql tables?
(14 answers)
Closed 4 years ago.
I have this select statement that I am running in SQL Server. But it's throwing an error:
select count(*)
from
(select zip from A
minus
select zip from B)
Error:
Incorrect syntax near select
What is the issue here? I have also tried aliasing the subquery but same error happens.
There is nothing called minus in SQL Server, you need to use except.
Note, except in SQL Server is equivalent to minus of Oracle
Following query will work.
select count(*) ct
from
(
select zip from A
except
select zip from B
)t
Another issue with your code is that you need to give a alias name to the inner table you are creating.

Running Common Table expression in netezza as a script with Update at the end of CTE [duplicate]

This question already has an answer here:
Convert a Recursive CTE in SQL Server to netezza
(1 answer)
Closed 6 years ago.
I am trying to run a CTE in script form
e.g
with CTE as
(select * from sometable);
on running this code part alone in the I am getting the error
^ found "" (at char 152) expecting SELECT' or'('' */
Please let me know how to run this query. thanks
You are getting this error because you aren't doing anything with the CTE you have defined, but the syntax requires something.
Add a SELECT after the CTE clause in the parentheses and it should work fine.
with CTE as
(select * from sometable)
select * from CTE;

SQL Server 2005 PIVOT Syntax Error

I receive the following error
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
When I run the following query
SELECT TOP 100 *
FROM
(
SELECT [TimeStamp],[MeterID],[Value]
FROM access_AMIData) AS source
PIVOT
(
SUM(Value)
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
) as pvt
ORDER BY TimeStamp
The error comes with this line:
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
If I change this line to:
FOR MeterID IN (1,2,3,4)
It works perfectly...how can I specify a select Query inside the IN?
You can't, at least not in SQL 2005. You could include it in the WHERE clause of your inner query, but you have to hard-code the column names, unless of course you use dynamic SQL.

Resources