我想打印以下带有参数值的存储过程中使用的SQL查询。当前它正在打印不带参数的SQL查询
执行存储过程时的值替换。它按原样打印参数(例如AND TP.F_LANGUAGE = @LANGUAGE
)
CREATE PROCEDURE [dbo].[SEARCH]
@LANGUAGE VARCHAR(2),
@SUBFORMAT NVARCHAR(2000),
@ICO NVARCHAR(4000),
AS
SET NOCOUNT ON
DECLARE @sqlQuery AS NVARCHAR(MAX)
DECLARE @pdfQuery AS NVARCHAR(MAX)
DECLARE @htmlQuery AS NVARCHAR(MAX)
DECLARE @param AS NVARCHAR(3000)
DECLARE @paramDefinition AS NVARCHAR(3000)
DECLARE @AllSubformats AS SubformatType
DECLARE @InputSubformats AS SubformatType
INSERT INTO @AllSubformats(Val)
SELECT S.Val
FROM (SELECT IsNull(TLK.f_value, '') As FValue
FROM T_LOOKUP TLK
WHERE TLK.f_parent = 'WEBVIEWER_INT_SUB') TLV
CROSS APPLY dbo.dfnSplit(TLV.FValue, ',') S;
IF (@SUBFORMAT <> '-1')
BEGIN
INSERT INTO @InputSubformats(Val)
SELECT S.Val
FROM dbo.dfnSplit(@SUBFORMAT, ',') S
END;
SET @pdfQuery = 'SELECT TOP 1001 TP.F_PRODUCT AS ID,
TP.F_PRODUCT_NAME AS NAME,
FROM PDF_DETAILS TP '
WHERE TP.F_PRODUCT<>'''''
SET @param = ' AND TP.F_AUTHORIZED IN(1,3) AND EXISTS (SELECT 1 FROM @AllSubformats ASF WHERE ASF.Val = TP.F_SUBFORMAT)'
IF NOT(@LANGUAGE IS NULL OR @LANGUAGE = '')
SET @param = @param + ' AND TP.F_LANGUAGE = @LANGUAGE '
IF NOT(@SUBFORMAT IS NULL OR @SUBFORMAT = '')
SET @param = @param + ' AND EXISTS (SELECT 1 FROM @InputSubformats ISF WHERE ISF.Val = TP.F_SUBFORMAT) '
IF NOT(@ICO IS NULL OR @ICO = '')
SET @param = @param + ' AND (TP.F_ICO_DATA LIKE @ICO) '
SET @ParamDefinition = ' @SUBFORMAT NVARCHAR(2000),
@LANGUAGE VARCHAR(2), @ICO NVARCHAR(4000),@AllSubformats SubformatType READONLY, @InputSubformats SubformatType READONLY '
SET @sqlQuery = @pdfQuery + @param
EXECUTE SP_EXECUTESQL @sqlQuery, @paramDefinition, @SUBFORMAT, @LANGUAGE, @ICO, @AllSubformats, @InputSubformats
Print @sqlQuery
请帮助。我想调试并执行SQL查询。
答案 0 :(得分:1)
您可以打印一个REPLACE函数的结果。您需要为字符串中的每个参数嵌套一个REPLACE:
PRINT REPLACE(
REPLACE(@sqlQuery, '@Parameter1', @Parameter1)
, '@Parameter2', @Parameter2
);
请注意,这假定您所有的参数都是字符串类型。如果它们中的任何一个不是字符串,则需要将它们投射到REPLACE中的字符串中。