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

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

Related

Select syntax issue related to ; [duplicate]

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;

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.

Linked Server Querying [duplicate]

This question already has answers here:
SQL Server Linked Server Example Query
(16 answers)
Closed 5 years ago.
I have the table name which is in the linked server.How to write a T-SQL query to find the database name (In linked server) from that table name?
I have no idea about linked server querying.Kindly help me
Thank you.
SELECT * FROM <linkedservername>.INFORMATION_SCHEMA.TABLES
or
SELECT * FROM <linkedservername>.INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'XXXXX'
or
select * from <linkedservername>.master.sys.databases
for get a list of all Databases
SELECT * FROM (nameoflinkedserver).INFORMATION_SCHEMA.TABLES
SELECT * FROM LinkServerName.DatabaseName.SchemaName.TableName
Example
Select * from MYLINKSERVER.MYDATABASE.MYSCHEMA.MYTABLE

Get all the Table names in particular database SQL Server [duplicate]

This question already has answers here:
How do I get list of all tables in a database using TSQL?
(20 answers)
Closed 6 years ago.
I am using SQL Server 2014 and want to get all the tables name in a particular database STUDENT through a SQL query.
How is it possible?
Thanks
You want to query sys.objects and look for everything with the type description 'USER_TABLE'. You could use a query like this;
SELECT
*
FROM STUDENT.sys.objects
WHERE type_desc = 'USER_TABLE'
The FROM clause has the usual format: DatabaseName.SchemaName.TableName.
Or as marc_s mentions, you can use sys.tables instead;
SELECT
*
FROM STUDENT.sys.tables

Syntax Error with my SQL Create Table As [duplicate]

This question already has answers here:
How to create a table from select query result in SQL Server 2008 [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to create a new table in Microsoft SQL Server Management Studio based on two existing tables.
When I execute the query below, I get an error saying that there is an:
Incorrect syntax near the keyword 'SELECT'.
SQL code:
CREATE TABLE NEW_TABLE AS
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
I looked at various other problems, but could not find a solution to mine. Do you have any idea what could be wrong with the syntax?
Alternatively, you can use SELECT * INTO new_table statement just like this.
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C INTO NEW_TABLE
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
this statement will also create a new table as you required.

Resources