Skip to content

Commit 87b7076

Browse files
authored
support lower-than-2012 instances (using RAISERROR instead of THROW) (#9659)
1 parent 0edb622 commit 87b7076

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

private/functions/New-DbaLogShippingPrimaryDatabase.ps1

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,28 @@ function New-DbaLogShippingPrimaryDatabase {
244244
}
245245

246246
# ensure unexpected non-success results are caught and thrown as errors:
247-
$Query += "
248-
IF (@SP_Add_RetCode <> 0)
249-
BEGIN
250-
DECLARE @msg VARCHAR(1000);
251-
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_primary_database ('
252-
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
253-
THROW 51000, @msg, 1;
254-
END"
247+
if ($server.Version.Major -ge 11) {
248+
$Query += "
249+
IF (@SP_Add_RetCode <> 0)
250+
BEGIN
251+
DECLARE @msg VARCHAR(1000);
252+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_primary_database ('
253+
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
254+
THROW 51000, @msg, 1;
255+
END
256+
"
257+
} else {
258+
$Query += "
259+
IF (@SP_Add_RetCode <> 0)
260+
BEGIN
261+
DECLARE @msg VARCHAR(1000);
262+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_primary_database ('
263+
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
264+
RAISERROR (@msg, 16, 1) WITH NOWAIT;
265+
RETURN;
266+
END
267+
"
268+
}
255269

256270
# Execute the query to add the log shipping primary
257271
if ($PSCmdlet.ShouldProcess($SqlServer, ("Configuring logshipping for primary database $Database on $SqlInstance"))) {

private/functions/New-DbaLogShippingSecondaryDatabase.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,28 @@ function New-DbaLogShippingSecondaryDatabase {
237237
$Query += ";"
238238
}
239239

240-
$Query += "
240+
if ($ServerSecondary.Version.Major -ge 11) {
241+
$Query += "
242+
IF (@SP_Add_RetCode <> 0)
243+
BEGIN
244+
DECLARE @msg VARCHAR(1000);
245+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_secondary_database ('
246+
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
247+
THROW 51000, @msg, 1;
248+
END
249+
"
250+
} else {
251+
$Query += "
241252
IF (@SP_Add_RetCode <> 0)
242253
BEGIN
243254
DECLARE @msg VARCHAR(1000);
244-
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_seondary_database ('
255+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_secondary_database ('
245256
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
246-
THROW 51000, @msg, 1;
247-
END"
257+
RAISERROR (@msg, 16, 1) WITH NOWAIT;
258+
RETURN;
259+
END
260+
"
261+
}
248262

249263
# Execute the query to add the log shipping primary
250264
if ($PSCmdlet.ShouldProcess($SqlServer, ("Configuring logshipping for secondary database $SecondaryDatabase on $SqlInstance"))) {

private/functions/New-DbaLogShippingSecondaryPrimary.ps1

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,28 @@ function New-DbaLogShippingSecondaryPrimary {
205205
}
206206

207207
# catch any non-success non error and throw:
208-
$Query += "
209-
IF (@SP_Add_RetCode <> 0)
210-
BEGIN
211-
DECLARE @msg VARCHAR(1000);
212-
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_secondary_primary ('
213-
+ CAST(@SP_Add_RetCode AS VARCHAR(5)) + ').';
214-
THROW 51000, @msg, 1;
215-
END"
208+
if ($ServerSecondary.Version.Major -ge 11) {
209+
$Query += "
210+
IF (@SP_Add_RetCode <> 0)
211+
BEGIN
212+
DECLARE @msg VARCHAR(1000);
213+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_secondary_primary ('
214+
+ CAST(@SP_Add_RetCode AS VARCHAR(5)) + ').';
215+
THROW 51000, @msg, 1;
216+
END
217+
"
218+
} else {
219+
$Query += "
220+
IF (@SP_Add_RetCode <> 0)
221+
BEGIN
222+
DECLARE @msg VARCHAR(1000);
223+
SELECT @msg = 'Unexpected result executing sp_add_log_shipping_secondary_primary ('
224+
+ CAST (@SP_Add_RetCode AS VARCHAR(5)) + ').';
225+
RAISERROR (@msg, 16, 1) WITH NOWAIT;
226+
RETURN;
227+
END
228+
"
229+
}
216230
# Execute the query to add the log shipping primary
217231
if ($PSCmdlet.ShouldProcess($SqlServer, ("Configuring logshipping making settings for the primary database to secondary database on $SqlInstance"))) {
218232
try {

0 commit comments

Comments
 (0)