Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_MSrepl_addlogreader_agent

  No additional text.


Syntax
CREATE PROCEDURE sys.sp_MSrepl_addlogreader_agent
(
	@job_login					nvarchar(257),
	@job_password				sysname,
	@job_name					sysname,
	@publisher_security_mode	smallint,
	@publisher_login			sysname,
	@publisher_password 		sysname,
	@publisher					sysname,
	@publisher_type 			sysname
)
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @retcode			int,
			@procedure			nvarchar(4000),
			@distributor_rpc	sysname,
			@distribution_db	sysname,
			@publisher_db		sysname,
			@logreader_exists	bit,
			@publisher_engine_edition int,
			@publisher_local	sysname
	
	-- Security Check
	EXEC @retcode = sys.sp_MSreplcheck_publish
	IF @@ERROR <> 0 OR @retcode <> 0
		RETURN 1
	
	-- defaults
	SELECT @publisher_db		= DB_NAME(),
			@logreader_exists	= 0,
			@publisher_engine_edition = sys.fn_MSrepl_editionid()

	-- must be tran published
	IF sys.fn_MSrepl_istranpublished(DB_NAME(),1) <> 1
	BEGIN
		RAISERROR (14013, 16, -1)
		RETURN 1
	END

	-- SQLSERVER	
	IF @publisher_type = N'MSSQLSERVER'
	BEGIN
		SELECT @publisher = publishingservername()
	
		
		--	Required :
		--		A. @publisher_security_mode
		--		B. if A = 1 then @publisher_login
		--		C. @job_login
		

		SELECT @publisher_security_mode = ISNULL(@publisher_security_mode, 1)
		
		IF @publisher_security_mode not in (0, 1)
		BEGIN
			-- The specified '@publisher_security_mode' is invalid (valid values are: 0, 1).
			RAISERROR(14266, 16, -1, '@publisher_security_mode', '0, 1')
			RETURN 1
		END

		-- make sure that the login is valid
		SELECT @publisher_login = RTRIM(LTRIM(ISNULL(@publisher_login, '')))
		IF @publisher_security_mode = 0 AND @publisher_login = ''
		BEGIN
			-- '@publisher_login cannot be null or empty when @publisher_security_mode is set to 0 (SQL Server authentication).'
			RAISERROR(21694, 16, -1, '@publisher_login', '@publisher_security_mode')
			RETURN 1
		END
	END
	-- HETERO
	ELSE
	BEGIN
		IF @publisher_security_mode IS NOT NULL
			OR @publisher_login IS NOT NULL
			OR @publisher_password IS NOT NULL
		BEGIN
			-- For heterogeneous publications, the @publisher_security_mode, @publisher_login and @publisher_password parameter(s) should be specified when calling 'sp_adddistpublisher'.
			RAISERROR(22535, 16, -1, '@publisher_security_mode, @publisher_login and @publisher_password', 'sp_adddistpublisher')
			RETURN 1
		END
	END

	-- if we do not require yukon security then check paramters.
	IF sys.fn_yukonsecuritymodelrequired(NULL) = 0
	BEGIN
		IF @job_login IS NULL
		BEGIN
			IF @job_password IS NOT NULL
			BEGIN
				-- Parameter '@job_login' can be set to 'NULL' only when '@job_password' is set to 'NULL'.
				RAISERROR(21678, 16, -1, '@job_login', 'NULL', '@job_password', 'NULL')
				RETURN 1
			END
		END
		ELSE
		BEGIN
			IF @job_password IS NULL
			BEGIN
				-- Parameter '@job_password' can be set to 'NULL' only when '@job_login' is set to 'NULL'.
				RAISERROR(21678, 16, -1, '@job_password', 'NULL', '@job_login', 'NULL')
				RETURN 1
			END

			IF sys.fn_replisvalidwindowsloginformat(@job_login) != 1
			BEGIN
				-- '@job_login' must be a valid Windows Login in the form : 'MACHINE\Login' or 'DOMAIN\Login'. Please see the documentation for 'sp_addlogreader_agent'.
				RAISERROR(21797, 16, -1, '@job_login', 'sp_addlogreader_agent')
				RETURN 1
			END
		END
	END
	-- if yukon sec required then a valid windows login/password is required
	ELSE
	BEGIN
		IF @job_login IS NULL
			OR @job_password IS NULL
		BEGIN
			-- Only members of the sysadmin fixed server role can perform this operation without specifying @job_login or @job_password.
			RAISERROR(21832, 16, -1, '@job_login or @job_password')
			RETURN 1
		END

		IF sys.fn_replisvalidwindowsloginformat(@job_login) != 1
		BEGIN
			-- '@job_login' must be a valid Windows Login in the form : 'MACHINE\Login' or 'DOMAIN\Login'. Please see the documentation for 'sp_addlogreader_agent'.
			RAISERROR(21797, 16, -1, '@job_login', 'sp_addlogreader_agent')
			RETURN 1
		END
	END

	-- retrieve the distribution database name			
	EXEC @retcode = sys.sp_helpdistributor @rpcsrvname = @distributor_rpc OUTPUT,
											@distribdb = @distribution_db OUTPUT,
											@publisher = @publisher
	IF @@error <> 0 or @retcode <> 0 or @distribution_db is NULL
	BEGIN
		-- "The Distributor has not been installed correctly."
		RAISERROR(20036, 16, -1)
		RETURN 1
	END

	IF @job_name IS NULL
	BEGIN
		SELECT @procedure = QUOTENAME(RTRIM(@distributor_rpc)) + '.' + QUOTENAME(RTRIM(@distribution_db)) + '.sys.sp_MSreplagentjobexists'
		EXEC @retcode = @procedure @type = 2,
									@exists = @logreader_exists output,
									@publisher = @publisher,
									@publisher_db = @publisher_db
		IF @retcode <> 0 OR @@ERROR <> 0
			RETURN 1
		
		-- exit if it already exists	
		IF @logreader_exists = 1
		BEGIN	
			-- "The logreader agent already exists. Use 'sp_changelogreader_agent' to change any settings/properties."
			RAISERROR(21831, 16, -1, 'logreader agent', 'sp_changelogreader_agent')
			RETURN 1
		END
	END
	ELSE
	BEGIN
		SELECT @logreader_exists = 1
	END

	BEGIN TRANSACTION tr_add_logreader_agent
	SAVE TRANSACTION tr_add_logreader_agent

    
    -- If we're attempting to add a new logreader job, we'll first drop
    --   the CDC job if CDC is enabled, as the logreader agent will be doing it's work
    
    if [sys].[fn_cdc_is_db_enabled]() = 1 and @logreader_exists <> 1
    begin
        exec @retcode = [sys].[sp_cdc_drop_job] @job_type = N'capture'
        if @retcode <> 0 or @@error <> 0
            GOTO UNDO
    end

	-- Schedule Log Reader agent for the database
	SELECT @procedure = QUOTENAME(RTRIM(@distributor_rpc)) + '.' + QUOTENAME(RTRIM(@distribution_db)) + '.dbo.sp_MSadd_logreader_agent'

	EXEC @retcode = @procedure	@publisher					= @publisher,
								@publisher_db				= @publisher_db,
								@publication				= 'ALL',
								@local_job 					= 1,
								@publisher_security_mode 	= @publisher_security_mode,
								@publisher_login 			= @publisher_login,
								@publisher_password 		= @publisher_password,
								@job_login 					= @job_login,
								@job_password 				= @job_password,
								@name 						= @job_name,
								@job_existing 				= @logreader_exists,
								@publisher_type 			= @publisher_type,
								@internal 					= N'YUKON',
								@publisher_engine_edition = @publisher_engine_edition
	IF @@ERROR <> 0 or @retcode <> 0
		GOTO UNDO

	-- If we didn't need to create the job but
	-- we were given the job_login and job_password
	-- then we will attempt to change them or add
	-- NOTE:
	-- We do this at the very end because the actual
	-- agent must be added prior to setting the login
	-- and passwords...
	IF @logreader_exists = 1
	BEGIN
		IF @job_login IS NOT NULL
		BEGIN
			IF @publisher_type = 'MSSQLSERVER'
			BEGIN
				SELECT @publisher_local = NULL
			END
			ELSE
			BEGIN
				SELECT @publisher_local = @publisher
			END
			
			EXEC @retcode = sys.sp_changelogreader_agent @job_login = @job_login,
															@job_password = @job_password,
															@publisher = @publisher_local
			IF @@ERROR <> 0 OR @retcode <> 0
				GOTO UNDO
		END
	END

	COMMIT TRANSACTION tr_add_logreader_agent
	
	RETURN 0
UNDO:
	ROLLBACK TRANSACTION tr_add_logreader_agent
	COMMIT TRANSACTION

	RETURN 1
END

 
Last revision 2008RTM
See also

  sp_addlogreader_agent (Procedure)
sp_MSdrop_repltran (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash