Column name in SQL Server fully qualified name - sql

I read in SQL Server documentation that I can specify column names in FROM clause of a simple SELECT query.
But when I try to run this query:
select * from my_db.dbo.test_table.test;
I get the following error:
Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in
sys.servers. Verify that the correct server name was specified. If
necessary, execute the stored procedure sp_addlinkedserver to add the
server to sys.servers.
I know this is a T-SQL page, and I'm trying to run .
Why does this happen?
I'm using SQL Server version 2017 via Microsoft SQL Server Management Studio version 2017 as well.

try rewriting from this
select * from my_db.dbo.test_table.test;
to this
select test_table.test,* from my_db.dbo.test_table;
the way it was written with that many Periods it assume you are trying to fully qualify the table so in what you had tried to the server is treating it as follows
my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror
the fields you want to show should directly follow the Keyword Select so if you only wanted 2 fields you could write
select test,test2 from my_db.dbo.test_table;
or simpler if you only have the one server
select test,test2 from dbo.test_table;
or if you only have the defailt Schema dbo (Database Base Owner)
select test,test2 from test_table;
I hope thaelps

You cannot specify columns in the FROM clause, but you can specify columns in the SELECT clause.
select test from my_db.dbo.test_table

Please run sp_addlinkedserver and sp_addlinkedsrvlogin SPs:
EXEC sp_addlinkedserver #server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'
After that, you can try to run: select * from my_db.dbo.test_table
Ref: https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/
Also, even though it is SQL Server 2014 related maybe helps, please see: Could not find server 'server name' in sys.servers. SQL Server 2014

Related

Microsoft SQL Server and Oracle SQL Developer

I linked an Oracle Database to my SQL Server in Microsoft SQL Server Management Studio 18. Server Objects -> Linked Servers.
I have a SQL Statement that when I run on the Oracle Developer Tool/Platform it returns the information as expected. But when I run the exact same query on the SQL Server it returns the incorrect results (The actual values in the rows and columns do not match).
What I know.
The table I am query in lives in the Oracle Database.
I can get the same/matching results on the Oracle Developer and SQL Server if I exclude in my WHERE statement anything involving a DATE.
Any thoughts?
The example of the query below.
Works on Oracle Developer but not on MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE('01/03/2020', 'DD/MM/YYYY') AND TO_DATE('10/12/2020','DD/MM/YYYY');
The example of the query below.
Works on both Oracle Developer and MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and BATCHID = 'ThisBAtchID';
You cannot use ORACLE specific functions like TO_DATE in SQL Server calls. You have to execute them remotely using OPENQUERY. OPENQUERY in MSDN
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT * FROM TABLE1
WHERE status = ''Deviation'' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE(''01/03/2020'', ''DD/MM/YYYY'') AND TO_DATE(''10/12/2020'',''DD/MM/YYYY'');');

How to create a table using the INSERT INTO clause using linked servers in SQL Server Management Studio

I have a server called GreatPlains and I would like to create a new table (not already defined) using the INSERT INTO clause onto my local server's reporting database. We have a linked server set up for the GreatPlains server and our main production server.
Simplified version of current query:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I'm also getting the error:
The object name 'local.Reporting.dbo.NewTable' contains more than the
maximum number of prefixes. The maximum is 2.
There are two mistakes in your query
1.INTO clause support maximum of 2 prefixes. You cannot include SERVER NAME
DATABASE_NAME.SCHEMA_NAME.TABLE_NAME
2.Unwanted INSERT ketword
So your query should be
SELECT *
INTO [Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
I think you have an extra insert:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
If the table is already defined and has the same columns in the same order, then you can do:
INSERT INTO [local].[Reporting].[dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders];
If you are running the script in your local server and table already exists in the database,Use the below script.
USE [Reporting]
GO
INSERT INTO [dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders]
If you don't have the table in your database,use the below script.
USE [Reporting]
GO
SELECT *
INTO dbo.[NewTable]
FROM [linked].[Main].[dbo].[Orders]

SQL Server Querying Online

I have two SQL Server database servers in two different server computers.
Server A - 192.168.1.100
Server B - 192.168.2.102
I need to execute a query from server A to retrieve data from a table in the Server B.
How to write the SQL select statement to perform this?
Are there any server configurations to allow these type of querying?
Add a linked server here are commands for 1 way of doing this. replace the user and password values with an appropriate SQL credential.
EXECUTE master.dbo.sp_addlinkedserver #server = N'192.168.2.102', #srvproduct=N'SQL Server'
EXECUTE master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'192.168.2.102',#useself=N'False',#rmtuser='ASQLLogin',#rmtpassword='Password'
then simply query like you would a normal table but append the linked server in front as commented above linked_server.db_name.schema_name.table_name like so:
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName]
You can even join it to your local server A if you want.
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName] b
INNER JOIN SomeTableOnServerA a
ON b.ID = a.ID

SQL Server 2008 stored procedure problem

I created a stored procedure on my MFG4 server in the user database having 2 Sections.
First section has query like:
Select * From MFG4.User.Usr.EmpMast
And second has
Select * From MFG5.User.Usr.EmpMast
Now I have problem that when the MFG5 server does not have the user database it displays an error even if it does not run first section where query is all right!!!
What should I do now???
Try This:
SELECT * FROM [MFG4].[User].[dbo].[EmpMast]

SQL Server: How to get a subitem of sp_helplanguage?

Question: I can get the SQL Server database language by querying:
SELECT ##language
And I can get further info via
EXEC sp_helplanguage
How can I query for a column of sp_helplanguage where name= ##language
I do SELECT * FROM sp_helplanguage WHERE name='DEUTSCH'
but that obviously doesn't work.
What's the correct way to query it ?
You need to query the underlying system catalog table directlry:
SELECT * FROM sys.syslanguages WHERE name='DEUTSCH'