USE [] DECLARE @msg VARCHAR(MAX) DECLARE @crlf CHAR(1) SET @crlf = CHAR(10) SET @msg = 'Current user must have following permissions: ' SET @msg = @msg + '[CREATE PROCEDURE, CREATE SERVICE, CREATE QUEUE, SUBSCRIBE QUERY NOTIFICATIONS, CONTROL, REFERENCES] ' SET @msg = @msg + 'that are required to start query notifications. ' SET @msg = @msg + 'Grant described permissions with following script: ' + @crlf SET @msg = @msg + 'GRANT CREATE PROCEDURE TO [];' + @crlf SET @msg = @msg + 'GRANT CREATE SERVICE TO [];' + @crlf SET @msg = @msg + 'GRANT CREATE QUEUE TO [];' + @crlf SET @msg = @msg + 'GRANT REFERENCES ON CONTRACT::[DEFAULT] TO [];' + @crlf SET @msg = @msg + 'GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [];' + @crlf SET @msg = @msg + 'GRANT CONTROL ON SCHEMA::[] TO [];' PRINT @msg IF OBJECT_ID('.', 'P') IS NULL BEGIN EXEC (' CREATE PROCEDURE . AS BEGIN -- Service Broker configuration statement. -- Setup Service Broker IF EXISTS (SELECT * FROM sys.databases WHERE name = '''' AND is_broker_enabled = 0) BEGIN ALTER DATABASE [] SET SINGLE_USER WITH ROLLBACK IMMEDIATE ALTER DATABASE [] SET ENABLE_BROKER; ALTER DATABASE [] SET MULTI_USER WITH ROLLBACK IMMEDIATE -- FOR SQL Express ALTER AUTHORIZATION ON DATABASE::[] TO [] END -- Create a queue which will hold the tracked information IF NOT EXISTS (SELECT * FROM sys.service_queues WHERE name = ''.'') CREATE QUEUE .[] -- Create a service on which tracked information will be sent IF NOT EXISTS(SELECT * FROM sys.services WHERE name = ''.'') CREATE SERVICE [] ON QUEUE .[] ([DEFAULT]) -- Notification Trigger check statement. IF OBJECT_ID (''.'', ''TR'') IS NOT NULL RETURN; -- Notification Trigger configuration statement. DECLARE @triggerStatement NVARCHAR(MAX) DECLARE @select NVARCHAR(MAX) DECLARE @sqlInserted NVARCHAR(MAX) DECLARE @sqlDeleted NVARCHAR(MAX) SET @triggerStatement = N'' CREATE TRIGGER [] ON .[] AFTER INSERT, UPDATE, DELETE AS SET NOCOUNT ON; --Trigger
is rising... IF EXISTS (SELECT * FROM sys.services WHERE name = '''''''') BEGIN DECLARE @message NVARCHAR(MAX) SET @message = N'''''''' IF ( EXISTS(SELECT 1)) BEGIN DECLARE @retvalOUT NVARCHAR(MAX) %inserted_select_statement% IF (@retvalOUT IS NOT NULL) BEGIN SET @message = N'''''''' + @retvalOUT END %deleted_select_statement% IF (@retvalOUT IS NOT NULL) BEGIN IF (@message = N'''''''') BEGIN SET @message = N'''''''' + @retvalOUT END ELSE BEGIN SET @message = @message + @retvalOUT END END IF (@message != N'''''''') BEGIN SET @message = @message + N'''''''' END END --Beginning of dialog... DECLARE @ConvHandle UNIQUEIDENTIFIER --Determine the Initiator Service, Target Service and the Contract BEGIN DIALOG @ConvHandle FROM SERVICE [] TO SERVICE '''''''' ON CONTRACT [DEFAULT] WITH ENCRYPTION=OFF, LIFETIME = 60; --Send the Message SEND ON CONVERSATION @ConvHandle MESSAGE TYPE [DEFAULT] (@message); --End conversation END CONVERSATION @ConvHandle; END '' SET @select = STUFF((SELECT '','' + ''['' + COLUMN_NAME + '']'' FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE NOT IN (''text'',''ntext'',''image'',''geometry'',''geography'') AND TABLE_SCHEMA = '''' AND TABLE_NAME = ''
'' AND TABLE_CATALOG = '''' FOR XML PATH ('''') ), 1, 1, '''') SET @sqlInserted = N''SET @retvalOUT = (SELECT '' + @select + N'' FROM INSERTED FOR XML PATH(''''row''''), ROOT (''''inserted''''))'' SET @sqlDeleted = N''SET @retvalOUT = (SELECT '' + @select + N'' FROM DELETED FOR XML PATH(''''row''''), ROOT (''''deleted''''))'' SET @triggerStatement = REPLACE(@triggerStatement , ''%inserted_select_statement%'', @sqlInserted) SET @triggerStatement = REPLACE(@triggerStatement , ''%deleted_select_statement%'', @sqlDeleted) EXEC sp_executesql @triggerStatement END ') END