将单引号添加到动态 SQL

Add single quote to dynamic SQL

我无法将单引号添加到此存储过程最后一行的最后一个单词

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

换行

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

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