cursor for sql server2005
This is my cursor procedure:
DECLARE C1 CURSOR LOCAL FOR
SELECT DISTINCT
PPTR_MATCH_REF_NO,
PPTR_LDGR_CODE,
PPTR_SLMAST_ACNO,
PPTR_PPN_STATUS
FROM GLAS_PPN_TRANSACTIONS
WHERE PPTR_COMP_CODE = @COMP_CODE
AND ISNULL(PPTR_PPN_STATUS, 'X') = 'V'
DECLARE @MATCH_REF_NO NUMERIC(10,0),
@LDGR_CODE VARCHAR(MAX),
@SLMAST_ACNO VARCHAR(MAX),
@PPN_STATUS VARCHAR(2),
@ACCT_NAME VARCHAR(MAX)
BEGIN
OPEN C1
FETCH NEXT FROM C1 INTO @MATCH_REF_NO,@LDGR_CODE,@SLMAST_ACNO,@PPN_STATUS
WHILE @@FETCH_STATUS =0
BEGIN
-- SET @MATCH_REF_NO = PPTR_MATCH_REF_NO
-- SET @LDGR_CODE = PPTR_LDGR_CODE
-- SET @SLMAST_ACNO = PPTR_SLMAST_ACNO
SELECT @ACCT_NAME = COAD_PTY_FULL_NAME
FROM GLAS_SBLGR_MASTERS,
GLAS_PTY_ADDRESS
WHERE SLMA_COMP_CODE = @COMP_CODE
AND SLMA_LDGRCTL_YEAR = DBO.GLAS_VALIDATIONS_GET_OPEN_YEAR(@COMP_CODE)
AND SLMA_LDGRCTL_CODE = @LDGR_CODE
AND SLMA_STATUS = 'A'
AND SLMA_ACNO = @SLMAST_ACNO
AND COAD_COMP_CODE = SLMA_COMP_CODE
AND COAD_ADDR_ID = SLMA_ADDR_ID
IF @PPN_STATUS = 'V'
BEGIN
SELECT @PPN_STATUS = 'VER'
END
FETCH NEXT FROM C1 INTO @MATCH_REF_NO,@LDGR_CODE,@SLMAST_ACNO,@PPN_STATUS
END
CLOSE C1
END
DEALLOCATE C1
END
How can I retrive @MATCH_REF_NO
, @LDGR_CODE
, @SLMAST_ACNO
, 开发者_开发问答@PPN_STATUS
and @ACCT_NAME
at the same time?
here 2 select statements is there how can i combine
You can simplify this by using a common-table expression (CTE) in SQL Server 2005 - you'll get something like:
WITH Select1 AS
(
SELECT DISTINCT
PPTR_MATCH_REF_NO,
PPTR_LDGR_CODE,
PPTR_SLMAST_ACNO,
PPTR_PPN_STATUS
FROM GLAS_PPN_TRANSACTIONS
WHERE PPTR_COMP_CODE = @COMP_CODE
AND ISNULL(PPTR_PPN_STATUS, 'X') = 'V'
)
SELECT
COAD_PTY_FULL_NAME
FROM
GLAS_SBLGR_MASTERS, GLAS_PTY_ADDRESS, Select1
WHERE
SLMA_COMP_CODE = Select1.COMP_CODE
AND SLMA_LDGRCTL_YEAR = DBO.GLAS_VALIDATIONS_GET_OPEN_YEAR(Select1.COMP_CODE)
AND SLMA_LDGRCTL_CODE = Select1.LDGR_CODE
AND SLMA_STATUS = 'A'
AND SLMA_ACNO = Select1.SLMAST_ACNO
AND COAD_COMP_CODE = SLMA_COMP_CODE
AND COAD_ADDR_ID = SLMA_ADDR_ID
What I cannot determine from the code you posted is how the GLAS_SBLGR_MASTERS
and the GLAS_PTY_ADDRESS
are joined (on what condition). Just specifying these two in the FROM
clause should be avoided - use the standard ANSI SQL JOIN statements:
FROM GLAS_SBLGR_MASTERS
INNER JOIN GLAS_PTY_ADDRESS ON ???????
Marc
精彩评论