Is it possible to query for dns Alias of Server in SQL - sql

I have a SQL Server with an alias.
example server name "USIADBT02"
example alias name "sql_test"
I want to know if there is a SQL query I can run that can tell me the server alias name based off of the current connection. Meaning, regardless of whether a connection is made using the actual server name or the alias, I want my query to return the alias name "sql test"
Not sure if this is possible but hoped someone could tell me. Thanks in advance!

No, the DNS alias is not a property that the SQL Server itself stores , and cannot be obtained by any SQL query (unless you store it in a table yourself).

Related

Create alias for SQL server to use in query

My SQL server instance name is in the format: 'My-Server\InstanceName'
I want to give the server the alias 'InstanceName'. I did this in the Configuration Manager. When I connect to the server, I'm able to connect using that alias name. However, when I want to fully qualify objects in a query (eg. select * from InstanceName.Database.dbo.Table), it does not work. It says it cannot find that server in sys.servers. Why is this? It seems like it is not carrying the alias name down into the query after it connects.
Thanks!
InstanceName and alias are two different things.
For local server use Database.dbo.Table
To connect to another server (or another instance) you should add Linked Server
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-database-engine.
Usually you do not need any alias, but if you really want to do this use example:
EXEC sp_addlinkedserver
#server=N'S1_instance1',
#srvproduct=N'',
#provider=N'SQLNCLI',
#datasrc=N'S1\instance1'

Access another database from same server?

How do you access another database from same server? Got server name using SELECT ##SERVERNAME, and then did servername.dbo.mydatabasename.mytablename but query analyzer says Invalid object name. Any ideas? Am doing
insert into Myservername.Mydatabasename.Mytablename
(Email,Username1)
Values
('h','h')
Using MS SQL Server 2008, on same server
Assuming you're using MS SQL Server, fully qualified references are in the form:
[servername].[databasename].[schema].[object]
On the same server you do not need the [servername] reference.
In your case, you reversed the databasename and schema. It should be:
servername.mydatabasename.dbo.mytablename
Your INSERT should look like:
insert into Mydatabasename.Schema.Mytablename
(Email,Username1)
Values ('h','h')
(probably your Schema here is dbo)
You would include the [servername] component when performing an operation across a linked server in which case [servername] would be the name of the linked server, which incidentally may not actually be the the same as the hostname/instance name of the remote server.
Is it not rather mydatabasename.DBO.mytablename ? And if your database is on the same server, you normally don't have to use the servername.
Should be
insert into Myservername.Mydatabasename.MySchema.Mytablename
(Email,Username1)
Values
('h','h')
Though as you're on the same server you don't need Myservername. In your example which was using three part notation it would have assumed that Mydatabasename was the schema; hence the error

Static Reference (function?) to a db in SQL 2005 & 2008

We are in desperate need to having some way to reference a static table in SQL. I'm assuming this maybe possible using a function but i'm not sure how to accomplish this.
For example I want to ...
-on SQL Server A (2005)
SELECT * FROM staticDB.someTbl
-on SQL Server B (2008)
SELECT * FROM staticDB.someTbl
then on each server have the real "staticDB" reference someplace
Hopefully this makes sense!
Thanks
EDIT
So when I have a stored procedure on our dev server it points to the dev db (via the function/var). Then on the prd server it points to prd db. Instead of having to remember to replace them every time. We can have one reference to this function/var and it would just work across both servers.
Something like a connection string equivalent, but for SQL.

SQL Server 2005 Linked server not finding tables

I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;

How can I connect to an external database from a sql statement or a stored procedure?

When running a SQL statement or a stored procedure on a database, can you connect to an external database and pull data from there?
something like:
SELECT a.UserID, b.DataIWantToGet
FROM mydb.Users as a, externaldb.Data as b
You'll need to setup a Linked Server instance. Then you can reference the external database as though it were a SQL Server database.
Yep -- there's two methods: either use the function OPENROWSET, or use linked servers. OPENROWSET is useful for ad-hoc single statements, but if you're going to be doing this regularly, read up on linked servers as they allow you to do exactly what you've specified in your SQL Statement ... e.g.,
SELECT database.owner.table for local data
SELECT server.database.owner.table for remote data
And yes, you can mix and match to do joins twixt local and remote. Note though that you'll need to be caureul if you do joins against large tables that exist on the remote server as the query could take a long time to exexute...
Yes, you can. You should take a look at linked servers for starters. You can also use OPENROWSET to hit them directly with no linked server.
Easiest way :
Click connect to server
when it asks for server name use:
192.168.X.X,1433\SQLEXPRESS insted of YOURPC\SQLEXPRESS
(The ip and opened port of target sql server)
Type correct username and password
Done!