forked from bulentgucuk/DBA-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreate Linked Server to Azure Database.sql
More file actions
51 lines (39 loc) · 1.36 KB
/
Create Linked Server to Azure Database.sql
File metadata and controls
51 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Create new linked server
*/
-- Make a link to the cloud
EXEC sp_addlinkedserver
@server=N'AzureNFLDB',
@srvproduct=N'Azure SQL Db',
@provider=N'SQLNCLI',
@datasrc=N'xxxxxx.database.windows.net', -- azure db address
@catalog='xxxxxx'; -- azure db name
GO
--Set up login mapping
EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'AzureNFLDB',
@useself = 'FALSE',
@locallogin=NULL,
@rmtuser = 'xxxxxx', -- remote login
@rmtpassword = 'xxxxxxqqqqwwwwww' -- password
GO
-- Test the connection
exec sp_testlinkedserver AzureNFLDB;
GO
-- Sample remote queries to linked server
/***
--https://msdn.microsoft.com/en-us/library/ms188427.aspx
SELECT * FROM OPENQUERY (AzureNFLDB , 'select objectname, indexname, commandtype, starttime, endtime from dbo.CommandLog
where starttime > ''20160405''');
SELECT * FROM OPENQUERY (AzureNFLDB , 'declare @d date = getdate() select objectname, indexname, commandtype, starttime, endtime from dbo.CommandLog
where starttime > @d');
DECLARE @Sname SYSNAME = 'AzureNFLDB';
IF EXISTS (SELECT *
FROM sys.servers
WHERE name = @Sname
)
BEGIN
DELETE OPENQUERY (AzureNFLDB , 'SELECT ID FROM dbo.CommandLog WHERE starttime < DATEADD(DAY,-30,GETDATE());');
END
DELETE OPENQUERY (AzureNFLDB , 'DECLARE @d DATETIME = DATEADD(DAY, -30, GETDATE()) SELECT ID FROM dbo.CommandLog WHERE starttime < @d');
***/