【发布时间】: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