使用表變量,例如:
DECLARE
@v_xs_scope TABLE (
RowNum INT IDENTITY,
SCOPE_KEY INT,
ENRICHED_DATE DATE,
ENRICHED_TIME TIME,
BENCHMARK_DATE DATE,
PROSDEALID INT
)
DECLARE
@v_measure_map TABLE (
RowNum INT IDENTITY,
v_upload_code INT
)
INSERT INTO @v_xs_scope (
SCOPE_KEY, ENRICHED_DATE, ENRICHED_TIME, BENCHMARK_DATE, PROSDEALID
)
SELECT
SCOPE_KEY, ENRICHED_DATE, ENRICHED_TIME, BENCHMARK_DATE, PROSDEALID
FROM xaction_scope
WHERE
TXN_ID = V_TXN_ID;
INSERT INTO @v_measure_map(v_upload_code)
SELECT v_upload_code
FROM v_measure_map
DECLARE
@l1_CurrentRowNum INT,
@l1_MaxRowNum INT,
@l2_CurrentRowNum INT,
@l2_MaxRowNum INT,
@v_upload_code INT,
@v_scope_key INT
SELECT
@l1_CurrentRowNum = 1,
@l1_MaxRowNum = MAX(RowNum)
FROM @v_measure_map
WHILE @l1_CurrentRowNum <= @l1_MaxRowNum
BEGIN
SELECT @v_upload_code = v_upload_code
FROM @v_measure_map
WHERE
RowNum = @l1_CurrentRowNum
SELECT
@l2_CurrentRowNum = 1,
@l2_MaxRowNum = MAX(RowNum)
FROM @v_xs_scope
WHILE @l2_CurrentRowNum <= @l2_MaxRowNum
BEGIN
SELECT @v_scope_key = scope_key
FROM @v_xs_scope
WHERE
RowNum = @l2_CurrentRowNum
SELECT @v_sql_count = COUNT(*)
FROM xaction_Level_info b
WHERE
b.UPLOAD_CODE = @v_upload_code
AND b.SCOPE = @v_scope_key
SET @l2_CurrentRowNum = @l2_CurrentRowNum + 1
END
SET @l1_CurrentRowNum = @l1_CurrentRowNum + 1
END
或遊標,例如:
DECLARE
@v_upload_code INT,
@v_scope_key INT
DECLARE curs_1 CURSOR FOR
SELECT
v_upload_code
FROM v_measure_map
OPEN curs_1;
FETCH curs_1 INTO @v_upload_code;
WHILE (@@FETCH_STATUS<>-1)
BEGIN
DECLARE curs_2 CURSOR FOR
SELECT
SCOPE_KEY
FROM xaction_scope
WHERE
TXN_ID = V_TXN_ID
OPEN curs_2;
FETCH curs_2 INTO @v_scope_key;
WHILE (@@FETCH_STATUS<>-1)
BEGIN
SELECT @v_sql_count = COUNT(*)
FROM xaction_Level_info b
WHERE
b.UPLOAD_CODE = @v_upload_code
AND b.SCOPE = @v_scope_key
FETCH curs_2 INTO @v_scope_key;
END
CLOSE curs_2;
DEALLOCATE curs_2;
FETCH curs_1 INTO @v_upload_code;
END
CLOSE curs_1;
DEALLOCATE curs_1;