我正在做水晶报告,用户要求做的事情就像只显示已经检查过的数据
日期自[日历]
日期到[日历]
在[Checkbox]之前显示数据
在[Checkbox]
之后显示数据显示不同的[Checkbox]
@dateTo datetime
@dateFrom datetime
@showdatab4 nvarchar // or what?
@showdataAf nvarchar //or what?
@showDiff nvarchar // or what?
Select dataBefore, dataAfter, differential, date_from ,date_to
from tableA
where date_from >= @datefrom and date_to <DATEADD(d,1,@dateTo)
我打算做的事情就像是
IF showdatab4 = check
Select dataBeforedate_from ,date_to
from tableA
where date_from >= @datefrom and date_to <DATEADD(d,1,@dateTo)
else if showdatab4 = checked and showdataAf = checked
Select dataBefore, dataAfter, date_from ,date_to from tableA
where date_from >= @datefrom and date_to <DATEADD(d,1,@dateTo)
这不够智能,是否有更好的方法来实现我的目标?
答案 0 :(得分:0)
好的,然后您可以根据条件创建动态SQL查询。假设像这样。这只是你的想法。
--EXEC SP_LoanCustomerInterest 'DETAIL','','019','9/1/2011','9/1/2011','L1'
ALTER PROC SP_LoanCustomerInterest(@DS VARCHAR(20),@AC_NO VARCHAR(50),@BR_CODE VARCHAR(10),@Edate1 VARCHAR(20),@EDATE2 VARCHAR(20),@AC_GROUP_CODE VARCHAR(MAX)='')
AS
BEGIN
--CREATED BY : NP SUBEDI FOR WEB BASED REPORT - JUNE 12 2013
SET NOCOUNT ON
DECLARE @SQL VARCHAR(MAX)=''
CREATE TABLE #TEMP(FIELD1 VARCHAR(50))
DECLARE @I INT=1
DECLARE @POS INT=1
SET NOCOUNT ON
WHILE @POS<LEN(@AC_GROUP_CODE)
BEGIN
INSERT INTO #TEMP(FIELD1)VALUES(SUBSTRING(@AC_GROUP_CODE,@I,2))
SET @POS=@POS+3
SET @I=@POS
END
If (@DS = 'DETAIL' )
BEGIN
SET @SQL = 'SELECT DISTINCT DBO.GETACNAME(LAM.AC_NO,'''+@BR_CODE+''') AS AC_NAME,AG.AC_GROUP_NAME,LT.AC_NO,LT.AC_GROUP_CODE,LT.TRAN_DATE,DBO.ENGTONEP(LT.TRAN_DATE) AS TRAN_NDATE,
(select Sum(int_amt) from loan_int_tran as LT1 where LT1.BR_CODE='''+@BR_CODE+''' AND int_type=''RI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND LT1.AC_NO=LT.AC_NO AND LT1.TRAN_DATE=LT.TRAN_DATE AND INT_OPR IN(''A'',''R'',''XA'')) as RI,
(select sum(int_amt) from loan_int_tran as LT1 where LT1.BR_CODE='''+@BR_CODE+''' AND int_type=''II'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND LT1.AC_NO=LT.AC_NO AND LT1.TRAN_DATE=LT.TRAN_DATE AND INT_OPR IN(''A'',''R'',''XA'')) as II,
(select sum(int_amt) from loan_int_tran as LT1 where LT1.BR_CODE='''+@BR_CODE+''' AND int_type=''PI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND LT1.AC_NO=LT.AC_NO AND LT1.TRAN_DATE=LT.TRAN_DATE AND INT_OPR IN(''A'',''R'',''XA'')) as PI,
(select sum(int_amt) from loan_int_tran as LT1 where LT1.BR_CODE='''+@BR_CODE+''' AND int_type=''ARI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND LT1.AC_NO=LT.AC_NO AND LT1.TRAN_DATE=LT.TRAN_DATE AND INT_OPR IN(''A'',''R'',''XA'')) as ARI,
(select sum(int_amt) from loan_int_tran as LT1 where LT1.BR_CODE='''+@BR_CODE+''' AND int_type=''OI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND LT1.AC_NO=LT.AC_NO AND LT1.TRAN_DATE=LT.TRAN_DATE AND INT_OPR IN(''A'',''R'',''XA'')) as ORI
FROM LOAN_INT_TRAN AS LT,LOAN_MAST AS LAM,AC_GROUP AS AG Where LT.BR_CODE='''+@BR_CODE+''' AND LAM.BR_CODE='''+@BR_CODE+''' AND LT.AC_NO = LAM.AC_NO And LT.AC_GROUP_CODE = AG.AC_GROUP_CODE And LT.AC_GROUP_CODE = LAM.AC_GROUP_CODE AND LT.AC_GROUP_CODE IN (SELECT FIELD1 FROM #TEMP)
AND (LT.TRAN_DATE >='''+@Edate1 +''' AND LT.TRAN_DATE <='''+@EDATE2+''')'
If @AC_NO<> ''
SET @SQL = @SQL + ' AND LT.AC_NO='''+@AC_NO+''''
END
If (@DS <> 'DETAIL')
BEGIN
SET @SQL ='Select Distinct DBO.GETACNAME(LAM.AC_NO,'''+@BR_CODE+''') AS AC_NAME,AG.AC_GROUP_NAME,LT.AC_NO,LT.AC_GROUP_CODE,
(Select sum(int_amt) from loan_int_tran as LT1 where BR_CODE='''+@BR_CODE+''' AND int_type=''RI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND AC_NO=LT.AC_NO AND AC_GROUP_CODE=LAM.AC_GROUP_CODE AND INT_OPR IN(''A'',''R'',''XA'')) as RI,
(Select sum(int_amt) from loan_int_tran as LT1 where BR_CODE='''+@BR_CODE+''' AND int_type=''II'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND AC_NO=LT.AC_NO AND AC_GROUP_CODE=LAM.AC_GROUP_CODE AND INT_OPR IN(''A'',''R'',''XA'')) as II,
(Select sum(int_amt) from loan_int_tran as LT1 where BR_CODE='''+@BR_CODE+''' AND int_type=''PI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND AC_NO=LT.AC_NO AND AC_GROUP_CODE=LAM.AC_GROUP_CODE AND INT_OPR IN(''A'',''R'',''XA'')) as PI,
(Select sum(int_amt) from loan_int_tran as LT1 where BR_CODE='''+@BR_CODE+''' AND int_type=''ARI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND AC_NO=LT.AC_NO AND AC_GROUP_CODE=LAM.AC_GROUP_CODE AND INT_OPR IN(''A'',''R'',''XA'')) as ARI,
(Select sum(int_amt) from loan_int_tran as LT1 where BR_CODE='''+@BR_CODE+''' AND int_type=''OI'' and tran_date>='''+@Edate1 +''' and tran_date<='''+@EDATE2 +''' AND AC_NO=LT.AC_NO AND AC_GROUP_CODE=LAM.AC_GROUP_CODE AND INT_OPR IN(''A'',''R'',''XA'')) as ORI
FROM LOAN_INT_TRAN AS LT,LOAN_MAST AS LAM,AC_GROUP AS AG
Where LT.BR_CODE='''+@BR_CODE+''' AND LAM.BR_CODE='''+@BR_CODE+''' AND LT.BR_CODE=LAM.BR_CODE AND LT.AC_NO=LAM.AC_NO And LT.AC_GROUP_CODE=AG.AC_GROUP_CODE And LT.AC_GROUP_CODE=LAM.AC_GROUP_CODE
AND LT.AC_GROUP_CODE IN (SELECT FIELD1 FROM #TEMP)'
If @AC_NO <> ''
SET @SQL = @SQL + ' AND LT.AC_NO='''+@AC_NO + ''''
END
EXEC sp_sqlexec @SQL
END
希望您了解上述查询。以上查询的目的是实现动态条件。 EXEC sp_sqlexec @SQL 这就是你所需要的。您可以在@SQL Variable上传递任何SQL查询。因此,根据您的条件创建@SQL并传递它。