SQL Server 2008 - 实施解码

发布于 2024-11-07 05:04:21 字数 3933 浏览 1 评论 0原文

首先,我知道有很多与使用 CASE 代替 DECODE 相关的帖子等,但它们似乎与我的担忧不符。

我正在尝试将 Oracle PL/SQL 过程转换为 SQL Server。该过程动态构建 SQL 语句,并使用 DECODE 函数创建 x-tab。过程如下:

PROCEDURE GET_XFORM_DATALOGS (
    fLOTCODE IN VARCHAR2,
    THEDATA OUT SYS_REFCURSOR) IS

    -- VARIABLE DECLARATIONS

    TYPE loc_array_type IS TABLE OF VARCHAR2(40);  -- array type
    loc_array    loc_array_type;     -- array for test names

    prod VARCHAR2(20);  --  product ID
    step VARCHAR2(20);  --  step ID
    sql_str VARCHAR2(32000);    -- SQL statement


    -- EXECUTABLE CODE

    BEGIN -- executable part starts here

        -- get the test names for the given lot code
        SELECT 
            PT_TESTNAME BULK COLLECT INTO loc_array
        FROM 
            (
                SELECT DISTINCT
                    TESTPARMS.PT_TESTNAME, 
                    TESTPARMS.PT_TESTNUM
                FROM 
                    "PRETEST".PRETEST_LOT@PRS_DBLINK LOT,
                    "PRETEST".PRETEST_MEASURE@PRS_DBLINK MEASURE,
                    "PRETEST".PRETEST_TEST_PARMS@PRS_DBLINK TESTPARMS
                WHERE 
                    LOT.PT_LOTSQ = MEASURE.PT_LOTSQ AND
                    MEASURE.PT_LOTSQ = TESTPARMS.PT_LOTSQ AND
                    MEASURE.PT_TESTNUM = TESTPARMS.PT_TESTNUM AND
                    LOT.PT_LOTID = fLOTCODE
                ORDER BY
                PT_TESTNUM
            );

        -- build the SQL string
        sql_str := '';
        sql_str := sql_str ||   'SELECT ';
        sql_str := sql_str ||   '   PRETEST_LOT.PT_LOTID, ';
        sql_str := sql_str ||   '   PRETEST_LOT.PT_LOCTYPE, ' ;
        sql_str := sql_str ||   '   PRETEST_LOT.PT_TESTDATE, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_WAFERID, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_XCOORD, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_YCOORD, ';

        -- add the decodes for column headings
        FOR i IN loc_array.first..loc_array.last LOOP
            sql_str := sql_str || 'max ( decode ( PRETEST_TEST_PARMS.PT_TESTNAME, '''
                || loc_array(i) || ''', PRETEST_MEASURE.PT_MEAS_VALUE, null ) ) '
                || loc_array(i);
                IF (i < loc_array.last) THEN
                    sql_str := sql_str || ', ';
                END IF;
        END LOOP;

        -- build the remainder of the SQL
        sql_str := sql_str || ' FROM ';
        sql_str := sql_str || '     "PRETEST".PRETEST_LOT@PRS_DBLINK PRETEST_LOT, ';
        sql_str := sql_str || '     "PRETEST".PRETEST_MEASURE@PRS_DBLINK PRETEST_MEASURE, ';
        sql_str := sql_str || '     "PRETEST".PRETEST_TEST_PARMS@PRS_DBLINK PRETEST_TEST_PARMS ';

        sql_str := sql_str || ' WHERE  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOTSQ = PRETEST_MEASURE.PT_LOTSQ AND ';
        sql_str := sql_str || '     PRETEST_MEASURE.PT_LOTSQ = PRETEST_TEST_PARMS.PT_LOTSQ AND ';
        sql_str := sql_str || '     PRETEST_MEASURE.PT_TESTNUM = PRETEST_TEST_PARMS.PT_TESTNUM AND ';
        sql_str := sql_str || '     (PRETEST_LOT.PT_LOTID = :fFLOTCODE)  ';

        sql_str := sql_str || ' GROUP BY ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOTID,  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOCTYPE,  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_TESTDATE,  ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_WAFERID, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_XCOORD, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_YCOORD ';

        sql_str := sql_str || ' ORDER BY  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_TESTDATE  ';

        -- run the query
        OPEN THEDATA FOR sql_str USING fLOTCODE;

END GET_XFORM_DATALOGS;

我的问题是,这可以在 SQL Server 中实现吗?我似乎无法弄清楚如何使用 CASE WHEN THEN 等根据动态数据创建列标题。

抱歉,如果我问的不清楚。感谢您提供的任何帮助。

BBz

First, I know there are plenty of posts etc relating to the use of CASE in place of DECODE, but they don't appear to fit with my concern.

I am trying to convert an Oracle PL/SQL procedure to SQL Server. The procedure builds a SQL statement dynamically, and uses the DECODE function to create a x-tab. The procedure is as so:

PROCEDURE GET_XFORM_DATALOGS (
    fLOTCODE IN VARCHAR2,
    THEDATA OUT SYS_REFCURSOR) IS

    -- VARIABLE DECLARATIONS

    TYPE loc_array_type IS TABLE OF VARCHAR2(40);  -- array type
    loc_array    loc_array_type;     -- array for test names

    prod VARCHAR2(20);  --  product ID
    step VARCHAR2(20);  --  step ID
    sql_str VARCHAR2(32000);    -- SQL statement


    -- EXECUTABLE CODE

    BEGIN -- executable part starts here

        -- get the test names for the given lot code
        SELECT 
            PT_TESTNAME BULK COLLECT INTO loc_array
        FROM 
            (
                SELECT DISTINCT
                    TESTPARMS.PT_TESTNAME, 
                    TESTPARMS.PT_TESTNUM
                FROM 
                    "PRETEST".PRETEST_LOT@PRS_DBLINK LOT,
                    "PRETEST".PRETEST_MEASURE@PRS_DBLINK MEASURE,
                    "PRETEST".PRETEST_TEST_PARMS@PRS_DBLINK TESTPARMS
                WHERE 
                    LOT.PT_LOTSQ = MEASURE.PT_LOTSQ AND
                    MEASURE.PT_LOTSQ = TESTPARMS.PT_LOTSQ AND
                    MEASURE.PT_TESTNUM = TESTPARMS.PT_TESTNUM AND
                    LOT.PT_LOTID = fLOTCODE
                ORDER BY
                PT_TESTNUM
            );

        -- build the SQL string
        sql_str := '';
        sql_str := sql_str ||   'SELECT ';
        sql_str := sql_str ||   '   PRETEST_LOT.PT_LOTID, ';
        sql_str := sql_str ||   '   PRETEST_LOT.PT_LOCTYPE, ' ;
        sql_str := sql_str ||   '   PRETEST_LOT.PT_TESTDATE, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_WAFERID, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_XCOORD, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_YCOORD, ';

        -- add the decodes for column headings
        FOR i IN loc_array.first..loc_array.last LOOP
            sql_str := sql_str || 'max ( decode ( PRETEST_TEST_PARMS.PT_TESTNAME, '''
                || loc_array(i) || ''', PRETEST_MEASURE.PT_MEAS_VALUE, null ) ) '
                || loc_array(i);
                IF (i < loc_array.last) THEN
                    sql_str := sql_str || ', ';
                END IF;
        END LOOP;

        -- build the remainder of the SQL
        sql_str := sql_str || ' FROM ';
        sql_str := sql_str || '     "PRETEST".PRETEST_LOT@PRS_DBLINK PRETEST_LOT, ';
        sql_str := sql_str || '     "PRETEST".PRETEST_MEASURE@PRS_DBLINK PRETEST_MEASURE, ';
        sql_str := sql_str || '     "PRETEST".PRETEST_TEST_PARMS@PRS_DBLINK PRETEST_TEST_PARMS ';

        sql_str := sql_str || ' WHERE  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOTSQ = PRETEST_MEASURE.PT_LOTSQ AND ';
        sql_str := sql_str || '     PRETEST_MEASURE.PT_LOTSQ = PRETEST_TEST_PARMS.PT_LOTSQ AND ';
        sql_str := sql_str || '     PRETEST_MEASURE.PT_TESTNUM = PRETEST_TEST_PARMS.PT_TESTNUM AND ';
        sql_str := sql_str || '     (PRETEST_LOT.PT_LOTID = :fFLOTCODE)  ';

        sql_str := sql_str || ' GROUP BY ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOTID,  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_LOCTYPE,  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_TESTDATE,  ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_WAFERID, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_XCOORD, ';
        sql_str := sql_str ||   '   PRETEST_MEASURE.PT_YCOORD ';

        sql_str := sql_str || ' ORDER BY  ';
        sql_str := sql_str || '     PRETEST_LOT.PT_TESTDATE  ';

        -- run the query
        OPEN THEDATA FOR sql_str USING fLOTCODE;

END GET_XFORM_DATALOGS;

The question I have is, is this going to be possible to implement in SQL Server? I can't seem to work out how to use CASE WHEN THEN etc to create column headings based on the dynamic data.

Sorry if what I've asked is unclear. Thanks for any help you can offer.

BBz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-11-14 05:04:21

那么使用 CASE 的 Oracle 等效项将是:

sql_str := sql_str || 'max ( CASE WHEN PRETEST_TEST_PARMS.PT_TESTNAME = '''
    || loc_array(i) || ''' THEN PRETEST_MEASURE.PT_MEAS_VALUE END ) '
    || loc_array(i);

因此尝试将其转换为 SQL Server。

Well the Oracle equivalent using CASE would be:

sql_str := sql_str || 'max ( CASE WHEN PRETEST_TEST_PARMS.PT_TESTNAME = '''
    || loc_array(i) || ''' THEN PRETEST_MEASURE.PT_MEAS_VALUE END ) '
    || loc_array(i);

So try converting that to SQL Server instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文