では、その条件に基づいて動的 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 を作成し、それを渡します。