2013-08-05 117 views
0

我遇到問題,將我的腳本的一部分工作在Oracle以上,用於SQL Server。我的腳本的一部分使用數組,並且我一直無法在TSQL(我不相信存在?)中找到等價物。我試圖匹配值v_measure_map(i).v_upload_code = b.UPLOAD_CODE時出現主要問題。任何提示或建議?將PL/SQL腳本轉換爲TSQL

SELECT SCOPE_KEY, 
    ENRICHED_DATE, 
    ENRICHED_TIME, 
    BENCHMARK_DATE, 
    PROSDEALID 
INTO v_xs_scope 
FROM xaction_scope 
WHERE TXN_ID = V_TXN_ID ; 


<<loop2>> FOR i IN 1..v_measure_map.count --count returns the number of rows in the table (sy_enrich_measure_map) 
    LOOP 


    <<loop3>> FOR j IN 1..v_xs_scope.count 

    LOOP 

    SELECT COUNT(*) 
    INTO @v_sql_count 
    FROM xaction_Level_info b 
    WHERE v_measure_map(i).v_upload_code = b.UPLOAD_CODE 
    AND v_xs_scope(j).v_scope_key  = b.SCOPE 
    AND 

回答

0

使用表變量,例如:

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;