如何从第一个日期到最后一个日期对用户输入参数进行编码(SQL 查询管理器)

How can code User input parameter from first date to last date (SQL query manager)

我需要帮助,我编写代码并且必须使用 The Last Date from user parameter >> [%1] in statement 但是当我执行 ..the filled window 强制我从最后一个日期到第一个日期填写。应该是从第一个日期到最后一个日期。

当我执行...“用户 window”弹出窗口并需要输入从第一个日期到最后一个日期的日期(例如:01/08/20 - 30/09/20)但是从上面编码的事实,

用户必须输入最后日期到第一个日期(例如:30/09/20 - 01/08/20)<<这会让用户感到困惑
我附上图片你可以看清楚>>

Select 
    T0.docnum , T0.docdate , Sum(T1.Quantity), 
    Sum(case when T0.docdate between dateadd(month,0,DATEADD(DAY,1-DATEPART(day,'[%1]'),'[%1]')) and eomonth(dateadd(month,0,DATEADD(DAY,1-DATEPART(day,'[%1]'),'[%1]'))) then T1.Quantity*T2.U_hmc_vollts else 0 end) as 'm12_Lts'  ,
    sum(case when T0.docdate between dateadd(month,-1,DATEADD(DAY,1- 
    DATEPART(day,'[%1]'),'[%1]')) and eomonth(dateadd(month,-1,DATEADD(DAY,1- 
    DATEPART(day,'[%1]'),'[%1]'))) then T1.Quantity*T2.U_hmc_vollts else 0 end) as 'm11_Lts'
From    OINV T0 
    left join inv1 T1 on T0.DocEntry = T1.DocEntry 
    left join oitm T2 on T1.ItemCode = T2.ItemCode 
Where 
    T0.docdate >= '[%0]' and T0.Docdate <= '[%1]' 
Group By 
    T0.docnum, T0.Docdate

order by 添加到您的 SQL 声明中。

https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql?view=sql-server-ver15

尝试插入

/*SELECT FROM [dbo].[OWTR] p1*/

declare @FROM as Datetime
declare @TO as datetime
/* WHERE */
set @FROM = /* p1.docdate */ '[%0]'
set @TO = /* p1.docdate */  '[%1]'

在查询的顶部(table 并不重要) 然后在查询的其余部分中将 [%0] 替换为 @FROM 并将 [%1] 替换为 @TO。

完整代码:

/*SELECT FROM [dbo].[OWTR] p1*/

 declare @FROM as Datetime
 declare @TO as datetime
 /* WHERE */
 set @FROM = /* p1.docdate */ '[%0]'
 set @TO = /* p1.docdate */  '[%1]'



Select 
T0.docnum , T0.docdate , Sum(T1.Quantity), 
Sum(case when T0.docdate between dateadd(month,0,DATEADD(DAY,1- 
DATEPART(day,@TO),@TO)) and eomonth(dateadd(month,0,DATEADD(DAY,1- 
 DATEPART(day,@TO),@TO))) then T1.Quantity*T2.U_hmc_vollts else 0 end) as 
'm12_Lts'  ,
sum(case when T0.docdate between dateadd(month,-1,DATEADD(DAY,1- 
DATEPART(day,@TO),@TO)) and eomonth(dateadd(month,-1,DATEADD(DAY,1- 
DATEPART(day,@TO),@TO))) then T1.Quantity*T2.U_hmc_vollts else 0 end) as 
'm11_Lts'
From    OINV T0 
left join inv1 T1 on T0.DocEntry = T1.DocEntry 
left join oitm T2 on T1.ItemCode = T2.ItemCode 
Where 
T0.docdate >= @FROM and T0.Docdate <= @TO 
Group By 
T0.docnum, T0.Docdate

告诉我你过得怎么样。