Incoming linked servers information in SQL Server - sql-server-2005

Is there anyway that we can list out the linked servers coming to a SQL server? I am trying to catch all the connections coming to a SQL Server. Please help!!
Thanks

Execute prcoedure: sp_linkedservers
MSFT article describing this procedure
or maybe if you want more info:
SELECT *
FROM sys.Servers a
LEFT OUTER JOIN sys.linked_logins b ON b.server_id = a.server_id
LEFT OUTER JOIN sys.server_principals c ON c.principal_id = b.local_principal_id

Related

Is there a way to query using TSQL what package/tier a Azure SQL server is running on?

For instance I would like to know whether the instance I'm connecting to is running on S0, S1, S2... from a TSQL query.
Please try the following query:
SELECT d.name,
slo.*
FROM sys.databases d
JOIN sys.database_service_objectives slo
ON d.database_id = slo.database_id;

Translating MS Access Query for SQL Server

I am trying to recreate a query that was done in MS Access, and now is being handled in a SQL Server environment. I understand that some of the SQL syntax is different in Access than it is in SQL Server. Is there somewhere online that points out the main differences, or will help translate one to the other?
This is a query that I need to update to use in SQL Server:
UPDATE
dbo.TPR00100
INNER JOIN (
dbo.UPR10302
LEFT JOIN dbo.B3980280 ON dbo.TPR10302.COMPTRNM = dbo.B3980280.COMPTRNM
) ON dbo.TPR00100.STAFFID = dbo.TPR10302.STAFFID
SET
dbo.B3980280.COMPTRNM = dbo.TPR10302.comptrnm,
dbo.B3980280.BI_LOC_ID = dbo.TPR00100.locatnid
WHERE
(((dbo.B3980280.COMPTRNM) Is Null))
What are they key aspects that need to be handled differently in a SQL Server transaction for this query?
If find it handy to use an updateable CTE for this:
with cte as (
select
b39.comptrnm b39_comptrnm
b39.bssi_loc_id b39_bssi_loc_id,
tpr.comptrnm tpr_comptrnm,
tpr.locatnid tpr_locatnid
from dbo.tpr00100 tpr
inner join dbo.upr10302 upr on tpr.staffid = upr.staffid
inner join dbo.b3980280 b39 on tpr.comptrnm = b39.comptrnm
where b39_comptrnm is null
)
update cte
set b39_comptrnm = tpr_comptrnm, b39_bssi_loc_id = tpr_locatnid
Note: I am not really sure why the table to update is left joined in the original query, so I turned it to an `inner join .

Merging data across multiple servers

OK, so I have this query that is used to record the stored procedure usage. The problem is, I used the merge statement and it does not work across multiple servers to insert into a central table. I basically need to have this generate the information on each server and then insert the data in a table on a central server. Any input would be great on how to correct this issue. We are using SQL 2008 R2.
MERGE INTO [DBA].dbo.SP_Exec_Stats_Table STAT
USING
(
SELECT db.name as [DatabaseName]
,OBJECT_NAME(d.object_id, d.database_id) [ProcedureName]
,d.last_execution_time
,o.modify_date
,d.total_elapsed_time/d.execution_count AS [avg_elapsed_time]
,d.execution_count
,##SERVERNAME as [ServerName]
,d.object_id
FROM sys.dm_exec_procedure_stats AS d inner join sys.databases as db on db.database_id = d.database_id
inner join sys.objects o on o.object_id = d.object_id
WHERE d.database_id not in(1,2,3,4,5)
) as SRC
ON STAT.[Object_id] = SRC.Object_id
WHEN MATCHED
AND STAT.[Last_Exec_Time] <> SRC.last_execution_time THEN
UPDATE SET
[Last_Exec_Time] = SRC.last_execution_time
WHEN NOT MATCHED THEN
INSERT (DatabaseName
,object_id
,[ProcedureName]
,[Last_Exec_Time]
,[Modified_Date]
,[Avg_Elapsed_Time]
,[Exec_Count]
,[Server_Name]
)
VALUES (SRC.DatabaseName
,SRC.object_id
,SRC.[ProcedureName]
,SRC.last_execution_time
,SRC.modify_date
,SRC.[Avg_Elapsed_Time]
,SRC.execution_count
,SRC.[ServerName]
) ;
By re-writing the query to pull the information from the remote server and inserting it into the local server I was able to bypass the limitations of the MERGE statement. I changed the 'from' to include the fully qualified name of the remote server and was able to pull the data into the local server.

SQL Query across multiple SQL servers

I have 2 SQL servers. I need a SQL query that can join 2 tables that are in two different server.
Like
SELECT *
FROM Server1.Db1.dbo.table1 A
INNER JOIN Server2.Db1.dbo.table2 B ON A.Id = B.Id
and I do not have the server names, instead I am using IP address of the servers. Do I need to enable these SQL servers as linked server to allow such cross server queries?
You can proceed with Linked Servers using sp_addlinkedserver.
Once done, you can query your data as you mentioned;
SELECT *
FROM [Db1].[dbo].table1 A
INNER JOIN [Server2].[Db1].[dbo].table2 B
ON A.Id = B.Id
Yes, add as linker server is one option. You also can join the remote table by use [ip address].dbname.dbo.table name s well.

Join two tables at the same server

I have two databases at the same server 192.168.1.100 DB1 and DB2
When I'm trying to execute :
select h.code,eh.Defaultname From hotels h JOIN [192.168.1.100].[dbo].[DB2].Hotels eh ON h.code = eh.code
I get
Could not find server '192.168.1.100' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
I don't understand :/
I'm using ms sql server 2005 and this query is fired when I'm at DB1.
What's the reason of this , and how to fix it ? :/
Since the databases are on the same SQL Server instance, you don't need to use a linked server, so don't specify the IP, just the other database's name.
select h.code,eh.Defaultname
From hotels h
JOIN [DB2].dbo.Hotels eh ON h.code = eh.code
JOIN [DB2].[dbo].[Hotels] AS eh