Error with FROM clause containing a subquery - sql-server

I was running this query in MSSQL:
SELECT * FROM (SELECT * FROM ABC)
It gives an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
This same command runs just fine on a DB2 database. I know this query doesn't make any sense I was just testing functionality.
So, are there certain features, eg., SELECT in FROM clause, that are not supported in MSSQL that are supported in DB2?

You just need to give the subquery an alias like so:
SELECT * FROM (SELECT * FROM ABC) subTable
Which translates to:
SELECT * FROM (SELECT * FROM ABC) as subTable
The AS is optional.

Related

Copy one table data into another db by using INTO Select

I am getting this error :
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'in'
My code:
SELECT *
INTO EmployeesBackup IN 'DB2.mbd'
FROM Employee
Your syntax is off, and perhaps you intended to do an INSERT INTO ... SELECT:
INSERT INTO DB2.EmployeesBackup
SELECT *
FROM DB1.Employee;
This would work assuming that both databases are on the same server, and that your backup table EmployeesBackup has the same definition as the Employee table.
Try this:
INSERT INTO [DBName].dbo.EmployeesBackup
SELECT * FROM [DBName].dbo.Employee
Please note that, Either both database should be available on the same SQL Server OR if one of them is available on the different server then it should be Linked

Except operation in TSQL returning error

I need to apply an EXCEPT (same as MINUS in Oracle) operation in TSQL. I tried with a simple code
select * from Table1
except
select * from Table1 where calndr_dt between '2014-10-01' and '2014-10-10'`
Each sub query is executing fine. But when joined with EXCEPT, it is returning the following error message.
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 2, column: 1: Incorrect syntax near 'except'.
Remove the ORDER BY clause from the first query, if you have this clause.

How can I move one of two perfectly identical rows into a new table through a query?

MS SQL Server 2008
I Use following Query to filter duplicate rows into a single row
Query 1 - SELECT DISTINCT * FROM flatfile_old
But now i want to move those query results into a new table and i try following things
Query 2 - SELECT * INTO flatfile_new FROM Flatfile_old WHERE 1 = 2
Query 3 - INSERT INTO flatfile_new (SELECT DISTINCT * FROM flatfile_old)
But Query No 3 throws error like follows
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
')'.
Please help
Just remove the brackets ( ... ) like this:
INSERT INTO flatfile_new
SELECT DISTINCT * FROM flatfile_old

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.

Try to create a table from Select - SqL Server 2008 throws error

Hi I am trying to create a table using inner select statement...
for example:
CREATE TABLE JmxMonSer AS (SELECT * FROM services WHERE monitoring_enabled = 1);
But keep getting error:
Incorrect Syntax near keyword 'AS', Severity 15
please advice
I'm almost positive that SQL Server doesn't have a CREATE TABLE AS (SELECT... syntax, but you can use SELECT INTO:
SELECT *
INTO JmxMonSer
FROM services
WHERE monitoring_enabled=1
Check the MSDN documentation for proper uses of the CREATE TABLE statement.
How about:
SELECT * into JmxMonSer FROM services WHERE monitoring_enabled=1
If the table already exists (and the columns types and ordering line up), you can use:
INSERT INTO JmxMonSer SELECT * FROM services WHERE monitoring_enabled=1
The below syntax is for using sub-query instead of using static table name...
because sometime the required result set is coming from different queries..
SELECT *
INTO JmxMonSer
From (SELECT * FROM services WHERE monitoring_enabled = 1) as X
Try using SELECT INTO:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
src: http://www.w3schools.com/sql/sql_select_into.asp

Resources