Merging data across multiple servers - sql

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.

Related

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 .

How to join 2 tables from 2 different servers in AS400?

I am working with AS400 version 7.1.
Having the following:
ServerA (SA) - DatabaseA (DBA) - TableA (TA)
ServerB (SB) - DataBaseB (DBB) - TableB (TB)
SELECT A.*, B.*
FROM SA.DBA.TA A INNER JOIN SB.DBB.TB
ON A.PN=B.PN
WHERE A.PN='BFDKS';
What's the correct syntax to join 2 tables from two different servers in AS400?
I am getting the following error
Relational database SA not in relational database directory
I'm pretty sure this is not currently possible with Db2 for i...
3-part names are new to the i, and as far as I know are limited to
insert into mylib.mytable
(select * from remotedb.somelib.sometable);
see CREATE TABLE with remote subselect
Or in a trigger program..
see 3-part names in triggers
Db2 for LUW has such federation capabilities...
One work around I've seen is the use of a user defined table function (UDTF) to return rows from a remote Db2 for i database..
Not possible on DB2 for i. But like says Charles, you can do it :
-- run this instructions on server B
create table qtemp.SADBATA as (
SELECT A.* FROM SA.DBA.TA A
where A.PN='BFDKS'
) with data;
SELECT * FROM qtemp.SADBATA A INNER JOIN SB.DBB.TB ON A.PN=B.PN;

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.

SQL Server 2008 Compare two different database tables for column name, data type and length

I am using SQL Server 2008 R2.
I have created two different databases Master and Test. Both of them have the same tables and columns in the beginning, but then master is used for some stable environment and test is used by me for ongoing development.
Now due to development I have changed some column's data types, added some new columns to the tables, deleted some columns, added some new tables and also deleted. Now I want to make Master same as test. I cannot drop and re-create Master as it contains some sensitive live data. So I need to compare each and every table of master with test along with column name, data type, constraints and length. Is there any solution?
Resolved it my self :
SELECT
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_type
FROM [master].sys.[tables] AS T
INNER JOIN [master].sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN [master].sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
EXCEPT
SELECT
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_type
FROM test.sys.[tables] AS T
INNER JOIN test.sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN test.sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
This will give you the list of Columns along with Table Name, Column Name and Data Type that is in master but not exists or different from test.
Have you done any searching?
http://www.codeproject.com/Articles/205011/SQL-Server-Database-Comparison-Tool

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