/
Chapter_27.txt
141 lines (106 loc) · 3.61 KB
/
Chapter_27.txt
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-----------------------------------------------------------------------
-- Source Code: SQL Server 2008 Transact-SQL Recipes, Joseph Sack
-----------------------------------------------------------------------
-- Do not execute the following code in a single batch. These samples
-- are provided in order to follow along with specific recipes.
-----------------------------------------------------------------------
-- Creating a Linked Server to Another SQL Server Instance
EXEC sp_addlinkedserver
@server= 'JOEPROD',
@srvproduct= 'SQL Server'
EXEC sp_addlinkedserver
@server= 'JOEPROD\NODE2',
@srvproduct= 'SQL Server'
-- Configuring Linked Server Properties
EXEC sp_serveroption
@server = 'JOEPROD\NODE2' ,
@optname = 'query timeout',
@optvalue = 60
-- Viewing Linked Server Information
SELECT name, query_timeout, lazy_schema_validation
FROM sys.servers
WHERE is_linked = 1
-- Dropping a Linked Server
EXEC sp_dropserver
@server= 'JOEPROD',
@droplogins= 'droplogins'
-- Adding a Linked Server Login Mapping
EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'JOEPROD\NODE2',
@useself = 'false' ,
@locallogin = NULL, -- Applies to all local logins
@rmtuser = 'test',
@rmtpassword = 'test1!'
-- Viewing Linked Logins
SELECT s.name LinkedServerName, ll.remote_name, p.name LocalLoginName
FROM sys.linked_logins ll
INNER JOIN sys.servers s ON
s.server_id = ll.server_id
LEFT OUTER JOIN sys.server_principals p ON
p.principal_id = ll.local_principal_id
WHERE s.is_linked = 1
-- Dropping a Linked Server LoginMapping
EXEC sp_droplinkedsrvlogin
@rmtsrvname= 'JOEPROD\NODE2' ,
@locallogin= NULL
-- Executing Distributed Queries Against a Linked Server
SELECT object_name, counter_name, instance_name,
cntr_value, cntr_type
FROM JOEPROD.master.sys.dm_os_performance_counters
WHERE counter_name = 'Active Transactions' AND
instance_name = '_Total'
EXEC [JOEPROD\NODE2].master.dbo.sp_monitor
-- Creating and Using an Alias to Reference Four-Part Linked Server Names
CREATE SYNONYM dbo.PerfInfo
FOR JOEPROD.master.sys.dm_os_performance_counters
SELECT cntr_value
FROM dbo.PerfInfo
WHERE counter_name = 'Active Transactions' AND
instance_name = '_Total'
DROP SYNONYM dbo.PerfInfo
CREATE SYNONYM dbo.PerfInfo
FOR [JOEPROD\NODE2].master.sys.dm_os_performance_counters
-- Executing Distributed Queries Using OPENQUERY
SELECT cntr_value
FROM OPENQUERY ( [JOEPROD] ,
'SELECT object_name, counter_name, instance_name, cntr_value, cntr_type
FROM master.sys.dm_os_performance_counters
WHERE counter_name = ''Active Transactions'' AND
instance_name = ''_Total''' )
-- Executing Ad Hoc Queries Using OPENROWSET
SELECT *
FROM OPENROWSET
('SQLNCLI','TESTSRV\NODE2';'test';'test1!',
'SELECT * FROM AdventureWorks.HumanResources.Department
WHERE GroupName = ''Research and Development''')
-- Reading Data froma File Using OPENROWSET BULK Options
/*
21,Sales Phone Rep,2005-06-01 00:00:00
20,Sales Phone Manager,2005-06-01 00:00:00
*/
/*
10.0
3
1 SQLCHAR 0 2 "," 1 ContactTypeID ""
2 SQLCHAR 0 20 "," 2 Name SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 19 "\r\n" 3 ModifiedDate ""
*/
SELECT ContactTypeID, Name, ModifiedDate
FROM OPENROWSET( BULK 'C:\Apress\Recipes\ContactType.txt',
FORMATFILE = 'C:\Apress\Recipes\ContactTypeFormat.Fmt',
FIRSTROW = 1,
MAXERRORS = 5,
ERRORFILE = 'C:\Apress\Recipes\ImportErrors.txt' )
AS ContactType
-- Create a table to hold import documents
CREATE TABLE dbo.ImportRepository
(ImportHistoryID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ImportFile varchar(max) NOT NULL)
GO
INSERT dbo.ImportRepository
(ImportFile)
SELECT BulkColumn
FROM OPENROWSET( BULK 'C:\Apress\Recipes\ContactType.txt',
SINGLE_CLOB) as ContactTypeFile
SELECT ImportFile
FROM dbo.ImportRepository