Linked Server Querying [duplicate] - sql-server

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

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

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.

SQL select startwith (SQL Server) [duplicate]

This question already has answers here:
SQL Server Escape an Underscore
(8 answers)
How do I escape _ in SQL Server? [duplicate]
(3 answers)
Closed 7 years ago.
I need to find a list of all items starting with an underscore: _
The obvious doesn't work:
select * from role where Name like '_%'
This still returns all items.
The following also returns all items.
select * from role where Name like '\_%'
select * from role where Name like '__%'
anyone knows how to escape a underscore? or how else I can achieve this StartWithUnderscore functionality?
I found the answer:
select * from role where Name like '[_]%';
You can escape with any character you want eg
select * from role where Name like '!_%' ESCAPE'!'
You can read more on this page: LIKE (Transact-SQL)

SQL server 2008 not supporting LIMIT [duplicate]

This question already has answers here:
MySQL LIMIT clause equivalent for SQL SERVER
(5 answers)
Closed 9 years ago.
I am trying to use LIMIT into SQL server 2008 but its not working, what can I do, I am new to SQL I have used mySQL before.
Here is the SQL query I am trying:
SELECT * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC LIMIT 1
Try SELECT TOP 1 * ...
Also see http://www.w3schools.com/sql/sql_top.asp for more details.
In MSSQL it's apparently something like SELECT TOP x * FROM ...
SELECT TOP 1 * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC

Resources