0

私はクリスタルレポートをやっています、ユーザーはすでにチェックしたデータだけを表示するようなことをするように要求します

[カレンダー] からの日付

[カレンダー]までの日付

[チェックボックス]より前のデータを表示

[チェックボックス] の後にデータを表示

別のものを表示する [チェックボックス]

@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)

これは十分な知性ではありません。私の目標を達成するためのより良い方法はありますか?

4

1 に答える 1

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 変数に任意の SQL クエリを渡すことができます。したがって、条件に基づいて @SQL を作成し、それを渡します。

于 2013-06-13T04:57:03.207 に答える