在工作中我们使用基于公司的测试框架。您只需将测试查询保存到特定表中的特定列,然后执行存储过程,该过程调用sp_executesql来执行查询,然后记录结果和所有结果。 今天我使用with子句构建测试查询。在框架内执行查询后,每次使用WITH语句运行查询时都会收到此错误: “关键字'With'附近的语法不正确”。 但是当我像sql select一样运行查询时,我得到的结果,所以查询工作正常。我只是无法使用sp_executesql语句运行查询。 我的问题是,甚至可以在sp_executesql命令中使用with子句运行查询吗?或者我该如何处理这种情况?有什么提示吗?
这是我的疑问:
With source_state as (
select
DISPLAY_VALUE as DISPLAY_VALUE
,FL_SIEBEL as FL_SIEBEL
,ID_CONSENT_ACTION as ID_CONSENT_ACTION
,LANG_INDEPENDENT_CODE as LANG_INDEPENDENT_CODE
from [dbo].[DM_CONSENT_ACT]
),
target_state as (
select
DISPLAY_VALUE as DISPLAY_VALUE
,FL_SIEBEL as FL_SIEBEL
,ID_CONSENT_ACTION as ID_CONSENT_ACTION
,LANG_INDEPENDENT_CODE as LANG_INDEPENDENT_CODE
--select *
from [dbo].[DM_LD_CONSENT_ACT2]
where isnull(dt_eff_to,@param1)>@param2
)
, pocet as
(SELECT * FROM source_state EXCEPT SELECT t.* FROM target_state as t )
, pocet2 as
(SELECT * FROM target_state EXCEPT SELECT t.* FROM source_state as t )
select (select COUNT(*) from pocet) + (select COUNT(*) from pocet2)
这是我运行代码的存储过程的一部分:
DECLARE tables_cursor_params CURSOR
FOR
SELECT NAME
FROM TF_PARAMS
OPEN tables_cursor_params
DECLARE @Name varchar(100)
FETCH NEXT FROM tables_cursor_params INTO @Name
WHILE (@@FETCH_STATUS <> -1)
BEGIN
declare @getParam nvarchar(max)
declare @sqlGetParam nvarchar(max) = 'select @getParam = VALUE from [dbo].[TF_PARAMS] where NAME = '''+@Name+''''
print @sqlGetParam
declare @Param varchar(max)
exec sp_executesql @sqlGetParam, N'@getParam varchar(max) out', @Param out
set @v_sql_a_replaced = replace(@v_sql_a_replaced, '@'+@Name, ''''+@Param+'''')
print @v_sql_a_replaced
FETCH NEXT FROM tables_cursor_params INTO @Name
END
CLOSE tables_cursor_params
DEALLOCATE tables_cursor_params
干杯! 约瑟夫