【问题标题】:Add single quote to dynamic SQL将单引号添加到动态 SQL
【发布时间】:2020-09-24 20:48:32
【问题描述】:

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

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

【问题讨论】:

  • 使用sp_executesql和参数。
  • 还有quotename()
  • select quotename('sometext', '''') 但我会使用 sp_executesql 并参数化我的查询。

标签: sql sql-server stored-procedures dynamic


【解决方案1】:

换行

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

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

【讨论】:

    猜你喜欢
    • 2010-11-10
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2013-03-09
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多