Our product inserts application data into a table utilizing two different databases [DB1],[DB2]. I've been asked to implement a high availability purge without having the Development Team update connection strings in code to point to a single database, as would be best practice. I have attempted the following:
Create a view on both [DB1] and [DB2] pointing to a primary table.
Created a secondary table that is identical.
Created stored procedures on both [DB1] and [DB2] to alter their respective views (Why can't SQL Server alter a view in a stored procedure?)
Create a job on [DB2] utilizing stored procedures to alter where the view points so while data is being purged from the primary table, inserts will still continue into the secondary.
The above works great on [DB2] and alters the view, changing where data is inserted. However, when the job (running on [DB2]) runs the step to update the view on [DB1] the following is thrown:
Subquery returned more than 1 value. This is not permitted when the subquery follows = != < <= > >= or when the subquery is used as an expression. [SQLSTATE 21000] (Error 512) The statement has been terminated. [SQLSTATE 01000] (Error 3621). The step failed.,00:00:00,16,3621,,,,0
I've attempted two different ways -
EXEC sp_executesql N'ALTER VIEW [dbo].[ApplicationEvent]
AS [some sql]'
and
DECLARE @sql varchar(max);
SET @sql = 'ALTER VIEW [dbo].[ApplicationEvent]
AS [some sql]'
exec(@sql);
both return the same error.
The views need to be pointed away at the same time, and only pointed back once the purge process is complete on the primary table. This is the reason for running both steps from the same job.
Is there away around the error that only occurs against the linked server execute call?
Thank you in advance for any assistance!
Additional Thoughts: I've already attempted to disable all triggers on the tables to see if that was the issue per another thread I saw here. Both created SP's work fine if run from their home Databases. Linked calls throw the error.
EDIT: while I still do not know why this error is being thrown... I've found a workaround for now.
After running a job that does as much as it can on [DB2] I have the job call another job on [DB1] that completes the tasks on DB that were erring out.
EXEC Server2.msdb.dbo.sp_start_job @job_name = N'Job2';
Doing this back and forth a few times got me to the result I wanted. I still would prefer to do this all in one job if possible as this seems a little hackish.
Stored Proc for altering View:
/****** Object: StoredProcedure [dbo].[AlterApplicaitonMessageViewSecondary] Script Date: 11/18/2014 4:01:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AlterApplicaitonMessageViewSecondary]
AS
DECLARE @sql11 varchar(max);
Set @sql11 = 'ALTER VIEW [dbo].[ApplicationMessage2]
AS
--Select from local member table.
SELECT [Id]
,[EventId]
,[Timestamp]
,[Message]
,[Severity]
,[MerchantId]
,[ProviderId]
,[VisitId]
,[UserId]
,[Server]
,[Process]
,[ReferenceId]
FROM [DB1].[ApplicationEvent].[dbo].[ApplicationMessageSecondary]
with (nolock)';
exec(@sql11);