【问题标题】:SQL Stored Procedure running exec work running with sp_executesql blank运行 exec 工作的 SQL 存储过程在 sp_executesql 空白的情况下运行
【发布时间】:2021-10-04 22:05:17
【问题描述】:

在工作室运行和测试时我有一个存储过程可以正常工作。

当在 C# 中运行时空白 - 当分析器检查 # 发送并在工作室中尝试错误时

Replace image with formatted text

Worked
    exec spGetReportDepartSalesALL  @Storeid = '1001',  @lodate = '07/28/2021 00:00:00', @hidate =  '07/28/2021 23:59:59', @Stationid = '', @departlist = '', @cashierid = '1001003'
  
Blank  
    exec sp_executesql N'exec spGetReportDepartSalesALL  @StoreID, @lodate,@hidate,@Stationid,@departlist,@cashierid,@cashierid varchar(100),@departlist nvarchar(6),@Stationid char(2),
    @StoreID char(4),@lodate char(22),@hidate char(22)', @cashierid='1001003', @departlist='', @Stationid='01', @StoreID='1001', @lodate='07/28/2021 00:00:00',
    @hidate='07/28/2021 23:59:59'

sp代码

ALTER PROCEDURE [dbo].[spGetReportDepartSalesALL]   
 -- Add the parameters for the stored procedure here  
   @storeId varchar(10),  
 @lodate datetime,  
 @hidate datetime,  
 @Stationid varchar(10),  
 @cashierid nvarchar(MAX),  
 @departlist nvarchar(MAX)   
AS  
BEGIN  
 -- SET NOCOUNT ON added to prevent extra result sets from  
 -- interfering with SELECT statements.  
 SET NOCOUNT ON;  
  
SELECT  
Departments.Dept_ID,   
Departments.Description,  
Inventory.ItemNum,  
Inventory.ItemName,  
Inventory.Vendor_Part_Num,  
Inventory.Location, SUM(Invoice_Itemized.Quantity) ASQuantity,  
SUM(Invoice_Itemized.Quantity * (Invoice_Itemized.PricePer+Invoice_Itemized.GC_Sold+Invoice_Itemized.Liability)) * (1 - Invoice_Totals.Discount) AS TotalPrice   
FROM   
Invoice_Totals INNER JOIN ((Departments INNER JOIN Inventory ON (Departments.Store_ID = Inventory.Store_ID) AND (Departments.Dept_ID = Inventory.Dept_ID))   
INNER JOIN Invoice_Itemized ON   
(Inventory.Store_ID = Invoice_Itemized.Store_ID) AND (Inventory.ItemNum = Invoice_Itemized.ItemNum)) ON (Invoice_Totals.Store_ID = Invoice_Itemized.Store_ID) AND (Invoice_Totals.Invoice_Number =   
Invoice_Itemized.Invoice_Number)   
WHERE   
Invoice_Totals.DateTime BETWEEN ( @lodate) AND (@hidate)   
AND Invoice_Totals.Status = 'C' AND Invoice_Itemized.ItemNum <>   
'GIFT_C' AND Invoice_Totals.Store_ID = @storeId AND Inventory.Store_ID = @storeId  and     
(@Stationid='' OR station_id=@Stationid) and   
  
(@departlist='' OR Departments.Dept_ID in (Select value FROM  STRING_SPLIT(@departlist,','))) and  
(@cashierid='' OR Invoice_Totals.Cashier_ID in  (Select value FROM  STRING_SPLIT(@cashierid,',')))   
  
GROUP BY Departments.Dept_ID, Departments.Description, Inventory.ItemNum, Inventory.ItemName, Inventory.Vendor_Part_Num, Inventory.Location,  Invoice_Totals.Cashier_ID,  
Invoice_Totals.Discount,Invoice_Totals.Cashier_ID   
ORDER BY Departments.Dept_ID  
   
END  

【问题讨论】:

  • 根据问题指南,请不要发布代码、数据、错误消息等的图像 - 将文本复制或输入到问题中。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。
  • @hidate='07/28/2021 23:59:59' 使用专有上限。 datetime 数据类型比这更精确,并且您忽略应该包含的值的风险很小。这是一个坏习惯。 @hidate char(22)' 不 - 这是一个日期时间。你有很多关于动态 sql 的知识。
  • 好的戴尔。感谢您的建议
  • 简单回答:第二个版本是按位置传递参数,而不是按名称。要通过名称传递,您需要@StoreID = @StoreID。你为什么还要使用第二个版本?

标签: sql-server tsql stored-procedures


【解决方案1】:

您的参数顺序错误(过程声明与 sp_executesql)。

语法是sp_executesql N'statment', [ {parameters definitions}, {parameters values} ]

参数定义必须在单独的参数中。

 exec sp_executesql N'exec spGetReportDepartSalesALL  @StoreID = @StoreID, @lodate = @lodate, @hidate = @hidate, @Stationid = @Stationid, @departlist = @departlist, @cashierid = @cashierid',
    N'@cashierid varchar(100),@departlist nvarchar(6),@Stationid char(2), @StoreID char(4),@lodate char(22),@hidate char(22)',
    @cashierid='1001003', @departlist='', @Stationid='01', @StoreID='1001', @lodate='07/28/2021 00:00:00', @hidate='07/28/2021 23:59:59'

SQL 语句可以简化如下。在简化版本中,参数的顺序很重要,并且必须与过程声明中的相同。

 exec sp_executesql N'exec spGetReportDepartSalesALL  @StoreID, @lodate, @hidate, @Stationid, @cashierid, @departlist',
    N'@cashierid varchar(100),@departlist nvarchar(6),@Stationid char(2), @StoreID char(4),@lodate char(22),@hidate char(22)',
    @cashierid='1001003', @departlist='', @Stationid='01', @StoreID='1001', @lodate='07/28/2021 00:00:00', @hidate='07/28/2021 23:59:59'

【讨论】:

  • 我添加了 sp 代码。如果我使用 sp_executesql 将值添加到 cashierid 并将它的 stationid 空白但另一个它没有问题,那么我遇到的问题。
  • @CoolTech 根据更改的问题更改了答案。要么使用带有@StoreID = @StoreID 的sql,要么根据过程声明更改参数的顺序。
【解决方案2】:

订单设置错误,为什么会出错。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多