这是我的光标程序:
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
如何同时检索@MATCH_REF_NO
,@LDGR_CODE
,@SLMAST_ACNO
,@PPN_STATUS
和@ACCT_NAME
?
这里有两个选择语句,我该如何结合
答案 0 :(得分:2)
您可以通过在SQL Server 2005中使用公用表表达式(CTE)来简化此操作 - 您将获得类似的内容:
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
我从您发布的代码中无法确定的是GLAS_SBLGR_MASTERS
和GLAS_PTY_ADDRESS
是如何结合的(在什么条件下)。应该避免在FROM
子句中指定这两个 - 使用标准的ANSI SQL JOIN语句:
FROM GLAS_SBLGR_MASTERS
INNER JOIN GLAS_PTY_ADDRESS ON ???????
马克