0

我在转换我的脚本的一部分时遇到问题,这些脚本可以OracleSQL Server. 我的脚本部分使用了数组,我无法在TSQL(我不相信存在一个?)中找到等价物。我的主要问题发生在尝试匹配值时v_measure_map(i).v_upload_code = b.UPLOAD_CODE。有什么提示或建议吗?

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 
4

1 回答 1

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;
于 2013-08-06T09:53:35.823 回答