【问题标题】:dynamic query showing 'Unclosed quotation mark after the character string '),动态查询显示'字符串后的未闭合引号'),
【发布时间】:2016-11-04 11:35:18
【问题描述】:

我有一个存储过程,其中出现错误“字符串后面的未闭合引号”在脚本中遇到困难。请帮我找出我的代码有什么问题。

这是我的代码。

ALTER PROCEDURE [dbo].[usp_Transfer] 
@orgid bigint,  
 @SearchString nvarchar (500) = null,   
 @LocationId bigint = 0, 
 @ownerid bigint,
 @OrderList varchar(MAX)
AS  
BEGIN  
 -- SET NOCOUNT ON added to prevent extra result sets from  
 -- interfering with SELECT statements.\  
 SET NOCOUNT ON;  
 DECLARE @SQL varchar(MAX)
BEGIN 
  SET @SQL = 'SELECT ProductID = ii.ProductId,
   InvItemId = convert(bigint,0),Name = p.Name,
      PrimaryImageID = p.PrimaryImageID,ProductNumberLabel = p.ProductNumberLabel,ProductNumber = p.ProductNumber,
      category = isnull(c.Name,''),
      qty = ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00),
      SalePrice= ISNULL(p.SalePrice, 0.00),
      EnteredQuantity=(case when (ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) > 1) then 1.00 else ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) end)
      ,Discount=0,u.UnitName,
      u.UnitID 

   FROM dbo.[Inven] ii  

   Left Join dbo.[Product] p on ii.ProductId = p.ProductId  and p.activestatus=1

   Left Join dbo.[category] c on p.DefaultCategoryId = c.CategoryId 

   Left Join dbo.[Unit] u on p.UnitId=u.UnitId and u.Activestatus=1 

   WHERE p.OrganizationID = @orgid 
   AND ii.InventoryID IN(1634)  
   AND ii.ActiveStatus = 1   
   AND p.ActiveStatus = 1  
   AND p.IsDisabled = 0  
   And p.CanSale = 1
   AND ii.InventoryID IN (' + @OrderList + ') 

   group by ii.ProductId, p.Name, p.PrimaryImageID, p.ProductNumberLabel, p.ProductNumber, c.Name,p.SalePrice,u.unitname,u.UnitID  
   having ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0) > 0  
   Order by p.ProductNumber, p.Name, c.Name '
  --EXEC(@SQL)  
  PRINT(@SQL)   
END  
END 

【问题讨论】:

  • 如果解决一个像引号这样的错误,声明变量@orgid然后转换错误会出现很多错误

标签: asp.net sql-server sql-server-2008


【解决方案1】:

有两点值得注意。

首先,@OrderList 是否包含引号?

第二,这一行:

...'  WHERE p.OrganizationID = @orgid '

应该是:

....'WHERE p.OrganizationID = ' + @orgid + '...'

测试其中任何一个是否是问题的原因的简单方法是将两者都注释掉,运行它并查看它是否有效,如果有效,然后一次评论它们,看看哪个给你错误。

最后,您可以重写此查询并完全避免使用动态查询。我想看看你已经完成的查询是因为IN (' + @OrderList + ') 子句。这些帖子可能会帮助您修改该部分:

Parameterize an SQL IN clause

SQL Server - In clause with a declared variable

【讨论】:

  • 我不认为从连接字符串构建 SQL 算作可靠的建议。 OP应该使用参数,使用sp_executesql并不难,所以没有任何借口不这样做。
  • @Tomalak 是的,但我没有建议他这样做,同时我也没有建议他不要这样做。我正在使用提供的代码,只是想在空闲时间提供一些快速建议。我会更新一个建议。
【解决方案2】:

如下更新您的 SP:

注意:如果解决一个像引号一样的错误,声明变量@orgid然后转换错误

您的初始错误是由于:category = isnull(c.Name,''),请将其替换为category = isnull(c.Name,'''')

alter PROCEDURE [dbo].[usp_Transfer] 
@orgid bigint=1,  
 @SearchString nvarchar (500) = null,   
 @LocationId bigint = 0, 
 @ownerid bigint=1,
 @OrderList varchar(MAX)='1'
AS  
BEGIN  
 -- SET NOCOUNT ON added to prevent extra result sets from  
 -- interfering with SELECT statements.\  
 SET NOCOUNT ON;  
 DECLARE @SQL varchar(MAX)
BEGIN 
  SET @SQL = 'SELECT ProductID = ii.ProductId,
   InvItemId = convert(bigint,0),Name = p.Name,
      PrimaryImageID = p.PrimaryImageID,ProductNumberLabel = p.ProductNumberLabel,ProductNumber = p.ProductNumber,
      category = isnull(c.Name,''''),
      qty = ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00),
      SalePrice= ISNULL(p.SalePrice, 0.00),
      EnteredQuantity=(case when (ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) > 1) then 1.00 else ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0.00) end)
      ,Discount=0,u.UnitName,
      u.UnitID 

   FROM dbo.[Inven] ii  

   Left Join dbo.[Product] p on ii.ProductId = p.ProductId  and p.activestatus=1

   Left Join dbo.[category] c on p.DefaultCategoryId = c.CategoryId 

   Left Join dbo.[Unit] u on p.UnitId=u.UnitId and u.Activestatus=1 

   WHERE p.OrganizationID = '+CAST(@orgid AS VARCHAR(10))+' 
   AND ii.InventoryID IN(1634)  
   AND ii.ActiveStatus = 1   
   AND p.ActiveStatus = 1  
   AND p.IsDisabled = 0  
   And p.CanSale = 1
   AND ii.InventoryID IN (' + @OrderList + ') 

   group by ii.ProductId, p.Name, p.PrimaryImageID, p.ProductNumberLabel, p.ProductNumber, c.Name,p.SalePrice,u.unitname,u.UnitID  
   having ISNULL((SUM(ii.[QuantityOnHand]) - SUM(ii.[QuantitySold])), 0) > 0  
   Order by p.ProductNumber, p.Name, c.Name '
  EXEC(@SQL)  
  PRINT(@SQL)   
END  
END 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多