动态 sql 执行问题 sql 服务器
Dynamic sql execution issue sql server
我已经构建了一个动态 sql,它有点大。所以当我尝试执行它时,我收到了类似
的错误
Msg 156, Level 15, State 1, Procedure sp_InstallListenerNotification_1, Line 31
Incorrect syntax near the keyword 'END'.
Msg 102, Level 15, State 1, Procedure sp_InstallListenerNotification_1, Line 118
Incorrect syntax near 'END'.
我无法找出问题出在哪里以及在哪里解决。所以如果可能的话帮我编译一下。这是我的动态 sql 脚本。
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 [<username>];' + @crlf
SET @msg = @msg + 'GRANT CREATE SERVICE TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT CREATE QUEUE TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT REFERENCES ON CONTRACT::[DEFAULT] TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT CONTROL ON SCHEMA::[<schemaname>] TO [<username>];'
PRINT @msg
IF OBJECT_ID ('[bba-reman].sp_InstallListenerNotification_1', 'P') IS NULL
BEGIN
EXEC ('
CREATE PROCEDURE [bba-reman].sp_InstallListenerNotification_1
AS
BEGIN
-- Service Broker configuration statement.
-- Setup Service Broker
IF EXISTS (SELECT * FROM sys.databases
WHERE name = ''bbareman'' AND (is_broker_enabled = 0 OR is_trustworthy_on = 0))
BEGIN
IF (NOT EXISTS(SELECT * FROM sys.fn_my_permissions(NULL, ''SERVER'')
WHERE permission_name = ''CONTROL SERVER''))
BEGIN
DECLARE @msg VARCHAR(MAX)
SET @msg = ''Current user doesn''''t have CONTROL SERVER permission to enable service broker. ''
SET @msg = @msg + ''Grant sufficient permissions to current user or ''
SET @msg = @msg + ''execute ALTER DATABASE [<dbname>] SET ENABLE_BROKER with admin rights.''
RAISERROR (@msg, 16, 1)
END
ELSE
BEGIN
--ALTER DATABASE [bbareman] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--ALTER DATABASE [bbareman] SET ENABLE_BROKER;
--ALTER DATABASE [bbareman] SET MULTI_USER WITH ROLLBACK IMMEDIATE
-- FOR SQL Express
--ALTER AUTHORIZATION ON DATABASE::[bbareman] TO [sa]
--ALTER DATABASE [bbareman] SET TRUSTWORTHY ON;
END
END
-- Create a queue which will hold the tracked information
IF NOT EXISTS (SELECT * FROM sys.service_queues WHERE name = ''ListenerQueue_1'')
CREATE QUEUE [bba-reman].[ListenerQueue_1]
-- Create a service on which tracked information will be sent
IF NOT EXISTS(SELECT * FROM sys.services WHERE name = ''ListenerService_1'')
CREATE SERVICE [ListenerService_1] ON QUEUE [bba-reman].[ListenerQueue_1] ([DEFAULT])
-- Notification Trigger check statement.
IF OBJECT_ID (''[bba-reman].tr_Listener_1'', ''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 [tr_Listener_1]
ON [bba-reman].[ContentChangeLog]
AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON;
--Trigger ContentChangeLog is rising...
IF EXISTS (SELECT * FROM sys.services WHERE name = ''''ListenerService_1'''')
BEGIN
DECLARE @message NVARCHAR(MAX)
SET @message = N''''<root/>''''
IF ( EXISTS(SELECT 1))
BEGIN
DECLARE @retvalOUT NVARCHAR(MAX)
%inserted_select_statement%
IF (@retvalOUT IS NOT NULL)
BEGIN SET @message = N''''<root>'''' + @retvalOUT END
%deleted_select_statement%
IF (@retvalOUT IS NOT NULL)
BEGIN
IF (@message = N''''<root/>'''') BEGIN SET @message = N''''<root>'''' + @retvalOUT END
ELSE BEGIN SET @message = @message + @retvalOUT END
END
IF (@message != N''''<root/>'''') BEGIN SET @message = @message + N''''</root>'''' END
END
--Beginning of dialog...
DECLARE @ConvHandle UNIQUEIDENTIFIER
--Determine the Initiator Service, Target Service and the Contract
BEGIN DIALOG @ConvHandle
FROM SERVICE [ListenerService_1] TO SERVICE ''''ListenerService_1'''' 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 TABLE_NAME = ''ContentChangeLog'' AND TABLE_CATALOG = ''bbareman''
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
你不能有一个空的 BEGIN ...END
块。您需要添加至少一个可执行语句,例如 PRINT
BEGIN
--ALTER DATABASE [bbareman] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--ALTER DATABASE [bbareman] SET ENABLE_BROKER;
--ALTER DATABASE [bbareman] SET MULTI_USER WITH ROLLBACK IMMEDIATE
-- FOR SQL Express
--ALTER AUTHORIZATION ON DATABASE::[bbareman] TO [sa]
--ALTER DATABASE [bbareman] SET TRUSTWORTHY ON;
PRINT 'Dummy Output'
END
您似乎还缺少 END
。在虚拟 PRINT
.
之后需要三个 END
语句
我已经构建了一个动态 sql,它有点大。所以当我尝试执行它时,我收到了类似
的错误Msg 156, Level 15, State 1, Procedure sp_InstallListenerNotification_1, Line 31
Incorrect syntax near the keyword 'END'.
Msg 102, Level 15, State 1, Procedure sp_InstallListenerNotification_1, Line 118
Incorrect syntax near 'END'.
我无法找出问题出在哪里以及在哪里解决。所以如果可能的话帮我编译一下。这是我的动态 sql 脚本。
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 [<username>];' + @crlf
SET @msg = @msg + 'GRANT CREATE SERVICE TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT CREATE QUEUE TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT REFERENCES ON CONTRACT::[DEFAULT] TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [<username>];' + @crlf
SET @msg = @msg + 'GRANT CONTROL ON SCHEMA::[<schemaname>] TO [<username>];'
PRINT @msg
IF OBJECT_ID ('[bba-reman].sp_InstallListenerNotification_1', 'P') IS NULL
BEGIN
EXEC ('
CREATE PROCEDURE [bba-reman].sp_InstallListenerNotification_1
AS
BEGIN
-- Service Broker configuration statement.
-- Setup Service Broker
IF EXISTS (SELECT * FROM sys.databases
WHERE name = ''bbareman'' AND (is_broker_enabled = 0 OR is_trustworthy_on = 0))
BEGIN
IF (NOT EXISTS(SELECT * FROM sys.fn_my_permissions(NULL, ''SERVER'')
WHERE permission_name = ''CONTROL SERVER''))
BEGIN
DECLARE @msg VARCHAR(MAX)
SET @msg = ''Current user doesn''''t have CONTROL SERVER permission to enable service broker. ''
SET @msg = @msg + ''Grant sufficient permissions to current user or ''
SET @msg = @msg + ''execute ALTER DATABASE [<dbname>] SET ENABLE_BROKER with admin rights.''
RAISERROR (@msg, 16, 1)
END
ELSE
BEGIN
--ALTER DATABASE [bbareman] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--ALTER DATABASE [bbareman] SET ENABLE_BROKER;
--ALTER DATABASE [bbareman] SET MULTI_USER WITH ROLLBACK IMMEDIATE
-- FOR SQL Express
--ALTER AUTHORIZATION ON DATABASE::[bbareman] TO [sa]
--ALTER DATABASE [bbareman] SET TRUSTWORTHY ON;
END
END
-- Create a queue which will hold the tracked information
IF NOT EXISTS (SELECT * FROM sys.service_queues WHERE name = ''ListenerQueue_1'')
CREATE QUEUE [bba-reman].[ListenerQueue_1]
-- Create a service on which tracked information will be sent
IF NOT EXISTS(SELECT * FROM sys.services WHERE name = ''ListenerService_1'')
CREATE SERVICE [ListenerService_1] ON QUEUE [bba-reman].[ListenerQueue_1] ([DEFAULT])
-- Notification Trigger check statement.
IF OBJECT_ID (''[bba-reman].tr_Listener_1'', ''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 [tr_Listener_1]
ON [bba-reman].[ContentChangeLog]
AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON;
--Trigger ContentChangeLog is rising...
IF EXISTS (SELECT * FROM sys.services WHERE name = ''''ListenerService_1'''')
BEGIN
DECLARE @message NVARCHAR(MAX)
SET @message = N''''<root/>''''
IF ( EXISTS(SELECT 1))
BEGIN
DECLARE @retvalOUT NVARCHAR(MAX)
%inserted_select_statement%
IF (@retvalOUT IS NOT NULL)
BEGIN SET @message = N''''<root>'''' + @retvalOUT END
%deleted_select_statement%
IF (@retvalOUT IS NOT NULL)
BEGIN
IF (@message = N''''<root/>'''') BEGIN SET @message = N''''<root>'''' + @retvalOUT END
ELSE BEGIN SET @message = @message + @retvalOUT END
END
IF (@message != N''''<root/>'''') BEGIN SET @message = @message + N''''</root>'''' END
END
--Beginning of dialog...
DECLARE @ConvHandle UNIQUEIDENTIFIER
--Determine the Initiator Service, Target Service and the Contract
BEGIN DIALOG @ConvHandle
FROM SERVICE [ListenerService_1] TO SERVICE ''''ListenerService_1'''' 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 TABLE_NAME = ''ContentChangeLog'' AND TABLE_CATALOG = ''bbareman''
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
你不能有一个空的 BEGIN ...END
块。您需要添加至少一个可执行语句,例如 PRINT
BEGIN
--ALTER DATABASE [bbareman] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--ALTER DATABASE [bbareman] SET ENABLE_BROKER;
--ALTER DATABASE [bbareman] SET MULTI_USER WITH ROLLBACK IMMEDIATE
-- FOR SQL Express
--ALTER AUTHORIZATION ON DATABASE::[bbareman] TO [sa]
--ALTER DATABASE [bbareman] SET TRUSTWORTHY ON;
PRINT 'Dummy Output'
END
您似乎还缺少 END
。在虚拟 PRINT
.
END
语句