SQL 服务器 EXEC @SQL 命令导致错误
SQL Server EXEC @SQL command causing an error
IF (SELECT COUNT(*)
FROM
(SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases_A]
INTERSECT
SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases]) z) > 0
BEGIN
DECLARE @SQL Nvarchar(max)
SET @SQL=
(
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases_A]
INTERSECT
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases]
)
EXECUTE @SQL
END
如果我执行这个查询,我得到以下错误:
Msg 512, Level 16, State 1, Line 81
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 2812, Level 16, State 62, Line 87
Could not find stored procedure ''.
我该如何解决这个问题?
你的变量只能取一个值,你给它一个结果集,要让它工作你需要这样做:
IF (SELECT COUNT(*)
FROM
(SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases_A]
INTERSECT
SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases]) z) > 0
BEGIN
DECLARE @SQL Nvarchar(max) = ''
SELECT @SQL = @SQL + ISNULL(QUERY, '') + ' '
FROM
(
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' AS QUERY from [dbo].[OF_Databases_A]
INTERSECT
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases]
) a
EXECUTE (@SQL)
END
IF (SELECT COUNT(*)
FROM
(SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases_A]
INTERSECT
SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases]) z) > 0
BEGIN
DECLARE @SQL Nvarchar(max)
SET @SQL=
(
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases_A]
INTERSECT
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases]
)
EXECUTE @SQL
END
如果我执行这个查询,我得到以下错误:
Msg 512, Level 16, State 1, Line 81
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.Msg 2812, Level 16, State 62, Line 87
Could not find stored procedure ''.
我该如何解决这个问题?
你的变量只能取一个值,你给它一个结果集,要让它工作你需要这样做:
IF (SELECT COUNT(*)
FROM
(SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases_A]
INTERSECT
SELECT [Domain], [Server], [Instance], [DatabaseName]
FROM [dbo].[OF_Databases]) z) > 0
BEGIN
DECLARE @SQL Nvarchar(max) = ''
SELECT @SQL = @SQL + ISNULL(QUERY, '') + ' '
FROM
(
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' AS QUERY from [dbo].[OF_Databases_A]
INTERSECT
select 'Delete from [dbo].[OF_Databases] where domain='''+[Domain]+'''' + ' and server= '''+[Server] +'''' + ' and instance= '''+[Instance] +'''' + ' and DatabaseName= '''+[DatabaseName] +'''' + ' GO' from [dbo].[OF_Databases]
) a
EXECUTE (@SQL)
END