I created a database table both on production and staging server with same columns and constraints.
CREATE TABLE dbo.test
(
[COLUMN1] [NVARCHAR](255) NOT NULL,
[COLUMN2] [NVARCHAR](255) NULL,
PRIMARY KEY CLUSTERED ([COLUMN1] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I have created an ASP.NET MVC 5 project in C#. Using the "Generate Database from Model" feature, I have created a model view controller.
I create a stored procedure in the SQL Server backend which checks server name whether dev or prod and then if dev, copies values in dev to prod (basically sync tables from dev to prod). Same is true for prod.
CREATE PROCEDURE [dbo].[uspSyncTables]
AS
BEGIN TRY
SET NOCOUNT ON
DECLARE @truncateEnvironment VARCHAR(500),
@syncFromEnv VARCHAR(50), @syncToEnv VARCHAR(50),
@delTable VARCHAR(500), @tablename NVARCHAR(1000)
SET @tablename='mydb.dbo.test'
IF(@@SERVERNAME = 'Prod\INSTANCE01')
BEGIN
SET @syncFromEnv = '[prod\INSTANCE01]'
SET @syncToEnv = '[dev\INSTANCE01]'
END
ELSE
BEGIN
SET @syncFromEnv = '[dev\INSTANCE01]'
SET @syncToEnv = '[Prod\INSTANCE01]'
END
-- Variables
DECLARE @sql NVARCHAR(MAX), @message NVARCHAR(4000)
BEGIN
SET @delTable = @syncToEnv+'.'+ @tablename
SET @sql='DELETE FROM '+ @delTable
PRINT @sql
EXEC(@SQL)
END
SET @sql = 'INSERT INTO ' +@delTable + ' SELECT * FROM ' + @syncFromEnv +'.'+ @tablename
EXEC(@SQL)
PRINT 'Rows inserts into : ' + CONVERT(VARCHAR, @@ROWCOUNT)
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH
I created a submit button in ASP.NET MVC (Index.html
) and in the controller, I am calling this stored procedure.
I am getting an error
System.Data.SqlClient.SqlException: MSDTC on server 'PROD\INSTANCE01' is unavailable
I am thinking this maybe because I call remote table to sync.