向动态sql添加单引号

zbsbpyhn  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(316)

在将单引号添加到此存储过程最后一行的最后一个单词时遇到问题

PROCEDURE PROC1(@ProductName nvarchar(40))
AS 
BEGIN
    DECLARE @sql nvarchar(MAX), @debug    bit = 1

    SET @sql = 'Select [Created] as [Date],[SerialId],[ProductId], CreatedById as [UserId]
                from [Customer].[dbo].[QuotationFlat] QF inner join [Customer].[dbo].[Product] Pt on Pt.Status=QF.' + @ProductName +'
                where '+ @ProductName +' = 1 and Productid = (

    SELECT Productid
    FROM Product
    WHERE Productid = (SELECT MAX(Productid) AS Productid
                       FROM Product 
                       WHERE ProductName = '  + @ProductName + '))'

    SELECT @sql
    -------------Last line Issue------------------
    FROM Product 
    WHERE ProductName = Test))

    ----------But there should be single quote around word TEST
zd287kbt

zd287kbt1#

换行

FROM Product where ProductName = '  + @ProductName + '))'

FROM Product where ProductName = '''  + @ProductName + '''))'

相关问题