Skip to content

Commit a8bdc76

Browse files
sql format
1 parent ef77b67 commit a8bdc76

1 file changed

Lines changed: 34 additions & 42 deletions

File tree

docs/database-engine/availability-groups/windows/create-an-availability-group-transact-sql.md

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ms.author: mathoma
8181

8282
1. The following [!INCLUDE[tsql](../../../includes/tsql-md.md)] example creates these databases and alters them to use the full recovery model:
8383

84-
```
84+
```sql
8585
-- Create sample databases:
8686
CREATE DATABASE MyDb1;
8787
GO
@@ -96,18 +96,17 @@ ms.author: mathoma
9696

9797
2. The following code example creates a full database backup of *MyDb1* and *MyDb2*. This code example uses a fictional backup share, \\\\*FILESERVER*\\*SQLbackups*.
9898

99-
```
99+
```sql
100100
-- Backup sample databases:
101101
BACKUP DATABASE MyDb1
102102
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
103-
WITH FORMAT
103+
WITH FORMAT;
104104
GO
105105
106106
BACKUP DATABASE MyDb2
107107
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
108-
WITH FORMAT
108+
WITH FORMAT;
109109
GO
110-
111110
```
112111

113112
[[TopOfExample]](#ExampleConfigAGWinAuth)
@@ -124,26 +123,24 @@ ms.author: mathoma
124123

125124
1. Create a database mirroring endpoint named *dbm_endpoint* on the server instance on which you plan to create the availability group (this is an instance named `AgHostInstance` on `COMPUTER01`). This endpoint uses port 7022. Note that the server instance on which you create the availability group will host the primary replica.
126125

127-
```
126+
```sql
128127
-- Create endpoint on server instance that hosts the primary replica:
129128
CREATE ENDPOINT dbm_endpoint
130129
STATE=STARTED
131130
AS TCP (LISTENER_PORT=7022)
132-
FOR DATABASE_MIRRORING (ROLE=ALL)
131+
FOR DATABASE_MIRRORING (ROLE=ALL);
133132
GO
134-
135133
```
136134

137135
2. Create an endpoint *dbm_endpoint* on the server instance that will host the secondary replica (this is the default server instance on `COMPUTER02`). This endpoint uses port 5022.
138136

139-
```
137+
```sql
140138
-- Create endpoint on server instance that hosts the secondary replica:
141139
CREATE ENDPOINT dbm_endpoint
142140
STATE=STARTED
143141
AS TCP (LISTENER_PORT=5022)
144-
FOR DATABASE_MIRRORING (ROLE=ALL)
142+
FOR DATABASE_MIRRORING (ROLE=ALL);
145143
GO
146-
147144
```
148145

149146
3. > [!NOTE]
@@ -153,7 +150,7 @@ ms.author: mathoma
153150

154151
The following code example shows the [!INCLUDE[tsql](../../../includes/tsql-md.md)] statements for creating a login and granting it permission on an endpoint. The domain account of the remote server instance is represented here as *domain_name*\\*user_name*.
155152

156-
```
153+
```sql
157154
-- If necessary, create a login for the service account, domain_name\user_name
158155
-- of the server instance that will host the other replica:
159156
USE master;
@@ -170,9 +167,8 @@ ms.author: mathoma
170167

171168
The following code example creates an availability group named *MyAG* on the server instance on which the sample databases, *MyDb1* and *MyDb2*, were created. The local server instance, `AgHostInstance`, on *COMPUTER01* is specified first. This instance will host the initial primary replica. A remote server instance, the default server instance on *COMPUTER02*, is specified to host a secondary replica. Both availability replica are configured to use asynchronous-commit mode with manual failover (for asynchronous-commit replicas manual failover means forced failover with possible data loss).
172169

173-
```
174-
175-
-- Create the availability group, MyAG:
170+
```sql
171+
-- Create the availability group, MyAG:
176172
CREATE AVAILABILITY GROUP MyAG
177173
FOR
178174
DATABASE MyDB1, MyDB2
@@ -198,7 +194,7 @@ ms.author: mathoma
198194

199195
The following code example joins the secondary replica on `COMPUTER02` to the `MyAG` availability group.
200196

201-
```
197+
```sql
202198
-- On the server instance that hosts the secondary replica,
203199
-- join the secondary replica to the availability group:
204200
ALTER AVAILABILITY GROUP MyAG JOIN;
@@ -209,19 +205,18 @@ ms.author: mathoma
209205

210206
The following code example creates the *MyDb1* and *MyDb2* secondary databases by restoring database backups using RESTORE WITH NORECOVERY.
211207

212-
```
208+
```sql
213209
-- On the server instance that hosts the secondary replica,
214210
-- Restore database backups using the WITH NORECOVERY option:
215211
RESTORE DATABASE MyDb1
216212
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
217-
WITH NORECOVERY
213+
WITH NORECOVERY;
218214
GO
219215
220216
RESTORE DATABASE MyDb2
221217
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
222-
WITH NORECOVERY
223-
GO
224-
218+
WITH NORECOVERY;
219+
GO
225220
```
226221

227222
7. On the server instance that hosts the primary replica, back up the transaction log on each of the primary databases.
@@ -231,19 +226,18 @@ ms.author: mathoma
231226

232227
The following code example creates a transaction log backup on MyDb1 and on MyDb2.
233228

234-
```
229+
```sql
235230
-- On the server instance that hosts the primary replica,
236231
-- Backup the transaction log on each primary database:
237232
BACKUP LOG MyDb1
238233
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
239-
WITH NOFORMAT
234+
WITH NOFORMAT;
240235
GO
241236
242237
BACKUP LOG MyDb2
243238
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
244-
WITHNOFORMAT
245-
GO
246-
239+
WITHNOFORMAT;
240+
GO
247241
```
248242

249243
> [!TIP]
@@ -256,32 +250,31 @@ ms.author: mathoma
256250
> [!IMPORTANT]
257251
> When you are preparing a real secondary database, you need to apply every log backup taken since the database backup from which you created the secondary database, starting with the earliest and always using RESTORE WITH NORECOVERY. Of course, if you restore both full and differential database backups, you would only need to apply the log backups taken after the differential backup.
258252

259-
```
253+
```sql
260254
-- Restore the transaction log on each secondary database,
261255
-- using the WITH NORECOVERY option:
262256
RESTORE LOG MyDb1
263257
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
264-
WITH FILE=1, NORECOVERY
258+
WITH FILE=1, NORECOVERY;
265259
GO
266260
RESTORE LOG MyDb2
267261
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
268-
WITH FILE=1, NORECOVERY
262+
WITH FILE=1, NORECOVERY;
269263
GO
270264
```
271265

272266
9. On the server instance that hosts the secondary replica, join the new secondary databases to the availability group.
273267

274268
The following code example, joins the *MyDb1* secondary database and then the *MyDb2* secondary databases to the *MyAG* availability group.
275269

276-
```
270+
```sql
277271
-- On the server instance that hosts the secondary replica,
278272
-- join each secondary database to the availability group:
279273
ALTER DATABASE MyDb1 SET HADR AVAILABILITY GROUP = MyAG;
280274
GO
281275
282276
ALTER DATABASE MyDb2 SET HADR AVAILABILITY GROUP = MyAG;
283277
GO
284-
285278
```
286279

287280
### <a name="CompleteCodeExample"></a> Complete Code Example for Sample Configuration Procedure
@@ -307,7 +300,7 @@ ms.author: mathoma
307300
> [!NOTE]
308301
> For additional [!INCLUDE[tsql](../../../includes/tsql-md.md)] code examples of creating an availability group, see [CREATE AVAILABILITY GROUP &#40;Transact-SQL&#41;](../../../t-sql/statements/create-availability-group-transact-sql.md).
309302
310-
```
303+
```sql
311304
-- on the server instance that will host the primary replica,
312305
-- create sample databases:
313306
CREATE DATABASE MyDb1;
@@ -323,26 +316,26 @@ GO
323316
-- Backup sample databases:
324317
BACKUP DATABASE MyDb1
325318
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
326-
WITH FORMAT
319+
WITH FORMAT;
327320
GO
328321
329322
BACKUP DATABASE MyDb2
330323
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
331-
WITH FORMAT
324+
WITH FORMAT;
332325
GO
333326
334327
-- Create the endpoint on the server instance that will host the primary replica:
335328
CREATE ENDPOINT dbm_endpoint
336329
STATE=STARTED
337330
AS TCP (LISTENER_PORT=7022)
338-
FOR DATABASE_MIRRORING (ROLE=ALL)
331+
FOR DATABASE_MIRRORING (ROLE=ALL);
339332
GO
340333
341334
-- Create the endpoint on the server instance that will host the secondary replica:
342335
CREATE ENDPOINT dbm_endpoint
343336
STATE=STARTED
344337
AS TCP (LISTENER_PORT=7022)
345-
FOR DATABASE_MIRRORING (ROLE=ALL)
338+
FOR DATABASE_MIRRORING (ROLE=ALL);
346339
GO
347340
348341
-- If both service accounts run under the same domain account, skip this step. Otherwise,
@@ -400,18 +393,18 @@ GO
400393
-- Restore database backups onto this server instance, using RESTORE WITH NORECOVERY:
401394
RESTORE DATABASE MyDb1
402395
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
403-
WITH NORECOVERY
396+
WITH NORECOVERY;
404397
GO
405398
406399
RESTORE DATABASE MyDb2
407400
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
408-
WITH NORECOVERY
401+
WITH NORECOVERY;
409402
GO
410403
411404
-- Back up the transaction log on each primary database:
412405
BACKUP LOG MyDb1
413406
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
414-
WITH NOFORMAT
407+
WITH NOFORMAT;
415408
GO
416409
417410
BACKUP LOG MyDb2
@@ -423,11 +416,11 @@ GO
423416
-- using the WITH NORECOVERY option:
424417
RESTORE LOG MyDb1
425418
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
426-
WITH FILE=1, NORECOVERY
419+
WITH FILE=1, NORECOVERY;
427420
GO
428421
RESTORE LOG MyDb2
429422
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
430-
WITH FILE=1, NORECOVERY
423+
WITH FILE=1, NORECOVERY;
431424
GO
432425
433426
-- On the server instance that hosts the secondary replica,
@@ -437,7 +430,6 @@ GO
437430
438431
ALTER DATABASE MyDb2 SET HADR AVAILABILITY GROUP = MyAG;
439432
GO
440-
441433
```
442434
443435
## <a name="RelatedTasks"></a> Related Tasks

0 commit comments

Comments
 (0)