diff --git a/CHANGELOG.md b/CHANGELOG.md
index c15e559ca..a20787a4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ Represents the **NuGet** versions.
## v5.17.1
- *Fixed:* The database console `Script` command execution has been updated to output to the correct directory path.
- *Fixed:* The `Agent` code-generation artefacts have been further simplified/improved.
+- *Fixed:* The database code-generation support for replacement is now optional (see new `Replace` property) versus always (defaults to `true`).
## v5.17.0
- *Enhancement:* The `Common`-project related code-generated artefacts have all been updated to leverage `global using` and file scoped namespace declarations.
diff --git a/docs/Database-Query-Config.md b/docs/Database-Query-Config.md
index dc927828f..01c063ba3 100644
--- a/docs/Database-Query-Config.md
+++ b/docs/Database-Query-Config.md
@@ -46,6 +46,7 @@ Property | Description
**`name`** | The name of the primary table of the query. [Mandatory]
`schema` | The schema name of the primary table of the view.
† Defaults to `CodeGeneration.Schema`.
`alias` | The `Schema.Table` alias name.
† Will automatically default where not specified.
+`viewReplace` | Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.
† Defaults to `CodeGeneration.Replace`.
diff --git a/docs/Database-StoredProcedure-Config.md b/docs/Database-StoredProcedure-Config.md
index 79f4f8d8c..534c80e37 100644
--- a/docs/Database-StoredProcedure-Config.md
+++ b/docs/Database-StoredProcedure-Config.md
@@ -66,6 +66,7 @@ Property | Description
**`type`** | The stored procedure operation type. Valid options are: `Get`, `GetColl`, `Create`, `Update`, `Upsert`, `Delete`, `Merge`.
† Defaults to `GetColl`.
**`paging`** | Indicates whether standardized paging support should be added.
† This only applies where the stored procedure operation `Type` is `GetColl`.
`storedProcedureName` | The `StoredProcedure` name in the database.
† Defaults to `sp` + `Table.Name` + `Name`; e.g. `spTableName` or `spPersonGet`.
+`replace` | Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.
† Defaults to `CodeGeneration.Replace`.
diff --git a/docs/Database-Table-Config.md b/docs/Database-Table-Config.md
index beabec95f..d2d2c57d9 100644
--- a/docs/Database-Table-Config.md
+++ b/docs/Database-Table-Config.md
@@ -63,6 +63,7 @@ Property | Description
**`name`** | The name of the `Table` in the database. [Mandatory]
**`schema`** | The name of the `Schema` where the `Table` is defined in the database.
† Defaults to `CodeGeneration.Schema`.
`alias` | The `Schema.Table` alias name.
† Will automatically default where not specified.
+`replace` | Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.
† Defaults to `CodeGeneration.Replace`.
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableCreate.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableCreate.sql
index f573103c8..3f61bcea0 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableCreate.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableCreate.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableCreate]
+CREATE PROCEDURE [Test].[spTableCreate]
@TableId AS UNIQUEIDENTIFIER = NULL OUTPUT,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableDelete.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableDelete.sql
index f22bf89e2..76f8a0279 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableDelete.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableDelete.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableDelete]
+CREATE PROCEDURE [Test].[spTableDelete]
@TableId AS UNIQUEIDENTIFIER,
@UpdatedBy AS NVARCHAR(250) NULL = NULL,
@UpdatedDate AS DATETIME2 NULL = NULL
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGet.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGet.sql
index d8fc77c70..0853baeba 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGet.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGet.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableGet]
+CREATE PROCEDURE [Test].[spTableGet]
@TableId AS UNIQUEIDENTIFIER
AS
BEGIN
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetAll.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetAll.sql
index a798e22fb..e0d115371 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetAll.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetAll.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableGetAll]
+CREATE PROCEDURE [Test].[spTableGetAll]
AS
BEGIN
/*
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetByArgs.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetByArgs.sql
index 73c85b72e..ad9f1614e 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetByArgs.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableGetByArgs.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableGetByArgs]
+CREATE PROCEDURE [Test].[spTableGetByArgs]
@Name AS NVARCHAR(50) NULL = NULL,
@MinCount AS INT,
@MaxCount AS INT NULL = NULL
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableMerge.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableMerge.sql
index ea30783e6..216badae8 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableMerge.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableMerge.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableMerge]
+CREATE PROCEDURE [Test].[spTableMerge]
@List AS [Test].[udtTableList] READONLY
AS
BEGIN
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpdate.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpdate.sql
index f4079cbe8..2ac9207bf 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpdate.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpdate.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableUpdate]
+CREATE PROCEDURE [Test].[spTableUpdate]
@TableId AS UNIQUEIDENTIFIER,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpsert.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpsert.sql
index d643ee255..464a912db 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpsert.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Stored Procedures/Generated/spTableUpsert.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER PROCEDURE [Test].[spTableUpsert]
+CREATE PROCEDURE [Test].[spTableUpsert]
@TableId AS UNIQUEIDENTIFIER,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
diff --git a/samples/Demo/Beef.Demo.Database/Schema/Test/Views/Generated/vwTestQuery.sql b/samples/Demo/Beef.Demo.Database/Schema/Test/Views/Generated/vwTestQuery.sql
index 777148921..ffd9c098c 100644
--- a/samples/Demo/Beef.Demo.Database/Schema/Test/Views/Generated/vwTestQuery.sql
+++ b/samples/Demo/Beef.Demo.Database/Schema/Test/Views/Generated/vwTestQuery.sql
@@ -1,4 +1,4 @@
-CREATE OR ALTER VIEW [Test].[vwTestQuery]
+CREATE VIEW [Test].[vwTestQuery]
AS
/*
* This is automatically generated; any changes will be lost.
diff --git a/samples/Demo/Beef.Demo.Database/database.beef-5.yaml b/samples/Demo/Beef.Demo.Database/database.beef-5.yaml
index 75474c755..2a09ab68a 100644
--- a/samples/Demo/Beef.Demo.Database/database.beef-5.yaml
+++ b/samples/Demo/Beef.Demo.Database/database.beef-5.yaml
@@ -6,7 +6,7 @@ collectionType: UDT
queries:
# New "Query" configuration.
-- { name: Table, schema: Test, view: true, viewName: vwTestQuery, excludeColumns: [ CreatedBy, UpdatedBy ], permission: TestSec,
+- { name: Table, schema: Test, view: true, viewName: vwTestQuery, viewReplace: false, excludeColumns: [ CreatedBy, UpdatedBy ], permission: TestSec,
joins: [
{ name: Person, schema: Demo, excludeColumns: [ CreatedDate, UpdatedDate ], aliasColumns: [ RowVersion^RowVersionP ],
on: [
@@ -35,7 +35,7 @@ tables:
- { name: Table3, schema: Test, get: true, create: true, update: true, delete: true, upsert: true }
# Special test to generate all variations.
-- { name: Table, schema: Test, create: true, update: true, upsert: true, delete: true, merge: true, udt: true, getAll: true, getAllOrderBy: [ Name Des ], excludeColumns: [ Other ], orgUnitImmutable: true, permission: TestSec,
+- { name: Table, schema: Test, create: true, update: true, upsert: true, delete: true, merge: true, udt: true, getAll: true, replace: false, getAllOrderBy: [ Name Des ], excludeColumns: [ Other ], orgUnitImmutable: true, permission: TestSec,
storedProcedures: [
{ name: GetByArgs, type: GetColl, excludeColumns: [ Count ],
parameters: [
diff --git a/tools/Beef.CodeGen.Core/Config/Database/CodeGenConfig.cs b/tools/Beef.CodeGen.Core/Config/Database/CodeGenConfig.cs
index ef27d6c62..bf26cd9bc 100644
--- a/tools/Beef.CodeGen.Core/Config/Database/CodeGenConfig.cs
+++ b/tools/Beef.CodeGen.Core/Config/Database/CodeGenConfig.cs
@@ -46,6 +46,14 @@ public class CodeGenConfig : ConfigRootBase, ISpecialColumnNames
Description = "This is used as the default `Schema` for all child objects.")]
public string? Schema { get; set; }
+ ///
+ /// Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.
+ ///
+ [JsonPropertyName("replace")]
+ [CodeGenProperty("Key", Title = "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
+ Description = "Defaults to `true`.")]
+ public bool? Replace { get; set; }
+
#endregion
#region Infer
@@ -397,6 +405,7 @@ protected override async Task PrepareAsync()
await LoadDbTablesConfigAsync().ConfigureAwait(false);
Schema = DefaultWhereNull(Schema, () => "dbo");
+ Replace = DefaultWhereNull(Replace, () => true);
EfModel = DefaultWhereNull(EfModel, () => false);
PathBase = DefaultWhereNull(PathBase, () => $"{Company}.{AppName}");
diff --git a/tools/Beef.CodeGen.Core/Config/Database/QueryConfig.cs b/tools/Beef.CodeGen.Core/Config/Database/QueryConfig.cs
index 0deb76d2b..e4f0939fe 100644
--- a/tools/Beef.CodeGen.Core/Config/Database/QueryConfig.cs
+++ b/tools/Beef.CodeGen.Core/Config/Database/QueryConfig.cs
@@ -126,6 +126,14 @@ public class QueryConfig : ConfigBase, ITableRefer
Description = "Defaults to `Schema`.")]
public string? ViewSchema { get; set; }
+ ///
+ /// Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.
+ ///
+ [JsonPropertyName("viewReplace")]
+ [CodeGenProperty("Key", Title = "Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.",
+ Description = "Defaults to `CodeGeneration.Replace`.")]
+ public bool? ViewReplace { get; set; }
+
#endregion
#region Auth
@@ -345,6 +353,7 @@ protected override async Task PrepareAsync()
Alias = DefaultWhereNull(Alias, () => new string(StringConverter.ToSentenceCase(Name)!.Split(' ').Select(x => x[..1].ToLower(System.Globalization.CultureInfo.InvariantCulture).ToCharArray()[0]).ToArray()));
ViewName = DefaultWhereNull(ViewName, () => "vw" + Name);
ViewSchema = DefaultWhereNull(ViewSchema, () => Schema);
+ ViewReplace = DefaultWhereNull(ViewReplace, () => Parent!.Replace);
if (!string.IsNullOrEmpty(Permission) && Permission.Split(".", StringSplitOptions.RemoveEmptyEntries).Length == 1)
Permission += ".Read";
diff --git a/tools/Beef.CodeGen.Core/Config/Database/StoredProcedureConfig.cs b/tools/Beef.CodeGen.Core/Config/Database/StoredProcedureConfig.cs
index f2d119956..cb54e47c0 100644
--- a/tools/Beef.CodeGen.Core/Config/Database/StoredProcedureConfig.cs
+++ b/tools/Beef.CodeGen.Core/Config/Database/StoredProcedureConfig.cs
@@ -94,6 +94,14 @@ public class StoredProcedureConfig : ConfigBase
Description = "Defaults to `sp` + `Table.Name` + `Name`; e.g. `spTableName` or `spPersonGet`.")]
public string? StoredProcedureName { get; set; }
+ ///
+ /// Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.
+ ///
+ [JsonPropertyName("replace")]
+ [CodeGenProperty("Key", Title = "Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.",
+ Description = "Defaults to `CodeGeneration.Replace`.")]
+ public bool? Replace { get; set; }
+
#endregion
#region Additional
@@ -301,6 +309,7 @@ protected override async Task PrepareAsync()
{
CollectionType = DefaultWhereNull(CollectionType, () => Parent!.CollectionType);
StoredProcedureName = DefaultWhereNull(StoredProcedureName, () => $"sp{Parent!.Name}{Name}");
+ Replace = DefaultWhereNull(Replace, () => Parent!.Replace);
Type = DefaultWhereNull(Type, () => "GetColl");
OrgUnitImmutable = DefaultWhereNull(OrgUnitImmutable, () => Parent!.OrgUnitImmutable);
Permission = DefaultWhereNull(Permission?.ToUpperInvariant(), () => Parent!.Permission == null ? null : Parent!.Permission!.ToUpperInvariant() + "." + Type switch
diff --git a/tools/Beef.CodeGen.Core/Config/Database/TableConfig.cs b/tools/Beef.CodeGen.Core/Config/Database/TableConfig.cs
index 57421dc94..d1c112e3e 100644
--- a/tools/Beef.CodeGen.Core/Config/Database/TableConfig.cs
+++ b/tools/Beef.CodeGen.Core/Config/Database/TableConfig.cs
@@ -85,6 +85,14 @@ public class TableConfig : ConfigBase, ITableRefer
Description = "Will automatically default where not specified.")]
public string? Alias { get; set; }
+ ///
+ /// Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.
+ ///
+ [JsonPropertyName("replace")]
+ [CodeGenProperty("Key", Title = "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
+ Description = "Defaults to `CodeGeneration.Replace`.")]
+ public bool? Replace { get; set; }
+
#endregion
#region Columns
@@ -504,6 +512,7 @@ protected override async Task PrepareAsync()
throw new CodeGenException(this, nameof(Name), $"Specified Schema.Table '{CodeGenConfig.FormatSchemaTableName(Schema, Name)}' not found in database.");
Alias = DefaultWhereNull(Alias, () => new string(StringConverter.ToSentenceCase(Name)!.Split(' ').Select(x => x[..1].ToLower(System.Globalization.CultureInfo.InvariantCulture).ToCharArray()[0]).ToArray()));
+ Replace = DefaultWhereNull(Replace, () => Parent!.Replace);
EfModel = DefaultWhereNull(EfModel, () => Parent!.EfModel);
EfModelName = DefaultWhereNull(EfModelName, () => Root.RenameForDotNet(Name));
OrgUnitImmutable = DefaultWhereNull(OrgUnitImmutable, () => Parent!.OrgUnitImmutable);
diff --git a/tools/Beef.CodeGen.Core/Schema/database.beef-5.json b/tools/Beef.CodeGen.Core/Schema/database.beef-5.json
index 980c1eb89..8c94c8e4a 100644
--- a/tools/Beef.CodeGen.Core/Schema/database.beef-5.json
+++ b/tools/Beef.CodeGen.Core/Schema/database.beef-5.json
@@ -12,6 +12,11 @@
"title": "The name of the \u0060Schema\u0060 where the artefacts are defined in, or should be created in, the database.",
"description": "This is used as the default \u0060Schema\u0060 for all child objects."
},
+ "replace": {
+ "type": "boolean",
+ "title": "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
+ "description": "Defaults to \u0060true\u0060."
+ },
"columnNameIsDeleted": {
"type": "string",
"title": "The column name for the \u0060IsDeleted\u0060 capability.",
@@ -223,6 +228,11 @@
"title": "The \u0060Schema.Table\u0060 alias name.",
"description": "Will automatically default where not specified."
},
+ "replace": {
+ "type": "boolean",
+ "title": "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
+ "description": "Defaults to \u0060CodeGeneration.Replace\u0060."
+ },
"includeColumns": {
"type": "array",
"title": "The list of \u0060Column\u0060 names to be included in the underlying generated output.",
@@ -440,6 +450,11 @@
"title": "The \u0060StoredProcedure\u0060 name in the database.",
"description": "Defaults to \u0060sp\u0060 \u002B \u0060Table.Name\u0060 \u002B \u0060Name\u0060; e.g. \u0060spTableName\u0060 or \u0060spPersonGet\u0060."
},
+ "replace": {
+ "type": "boolean",
+ "title": "Indicates whether the existing \u0060StoredProcedure\u0060 database object should be replaced/altered or whether the object is dropped and recreated.",
+ "description": "Defaults to \u0060CodeGeneration.Replace\u0060."
+ },
"reselectStatement": {
"type": "string",
"title": "The SQL statement to perform the reselect after a \u0060Create\u0060, \u0060Update\u0060 or \u0060Upsert\u0060 stored procedure operation \u0060Type\u0060.",
@@ -777,6 +792,11 @@
"title": "The schema name for the \u0060View\u0060.",
"description": "Defaults to \u0060Schema\u0060."
},
+ "viewReplace": {
+ "type": "boolean",
+ "title": "Indicates whether the existing \u0060View\u0060 database object should be replaced/altered or whether the object is dropped and recreated.",
+ "description": "Defaults to \u0060CodeGeneration.Replace\u0060."
+ },
"permission": {
"type": "string",
"title": "The permission to be used for security permission checking.",
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableCreate_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableCreate_sql.hbs
index 0d96d2c54..254c90cc5 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableCreate_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableCreate_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableDelete_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableDelete_sql.hbs
index 79b647e9b..adc0441cd 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableDelete_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableDelete_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#unless @last}},{{/unless}}
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableGetColl_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableGetColl_sql.hbs
index 1eb58aa31..aba56d14c 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableGetColl_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableGetColl_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#if Parent.Paging}},{{else}}{{#unless @last}},{{/unless}}{{/if}}{{#if Collection}}{{#ifeq CollectionType 'JSON'}} -- JSON Array{{/ifeq}}{{/if}}
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableGet_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableGet_sql.hbs
index dc40ec4e5..2b79dbebf 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableGet_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableGet_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#unless @last}},{{/unless}}
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableMerge_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableMerge_sql.hbs
index 91afca63e..72d362456 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableMerge_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableMerge_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableUpdate_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableUpdate_sql.hbs
index 718929adb..281bcdcc8 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableUpdate_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableUpdate_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbSpTableUpsert_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbSpTableUpsert_sql.hbs
index acee19953..1b8e13770 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbSpTableUpsert_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbSpTableUpsert_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
+CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
diff --git a/tools/Beef.Database.SqlServer/Templates/DbVwQuery_sql.hbs b/tools/Beef.Database.SqlServer/Templates/DbVwQuery_sql.hbs
index 25f310873..4c3ece329 100644
--- a/tools/Beef.Database.SqlServer/Templates/DbVwQuery_sql.hbs
+++ b/tools/Beef.Database.SqlServer/Templates/DbVwQuery_sql.hbs
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
-CREATE OR ALTER VIEW [{{ViewSchema}}].[{{ViewName}}]
+CREATE {{#if ViewReplace}}OR ALTER {{/if}}VIEW [{{ViewSchema}}].[{{ViewName}}]
AS
/*
* This is automatically generated; any changes will be lost.