Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:41:17+00:00 2026-05-22T02:41:17+00:00

First, I know there are plenty of posts etc relating to the use of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:41:18+00:00Added an answer on May 22, 2026 at 2:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I know there are excellent implementations (Qt, Boost, cpp-event, etc.), but
I know there exists a command like jQuery.noConflict. But for this it first registers
I know there a similar threads around, but this is really the first time
I know there are several other posts with solutions to this, but my current
I know that there have been plenty of topics describing this topic but I
First, I know there is a lot of duplicate answers already, but I can't
First of all I know there is a spark VolumeBar component but, for design
I know there are a bunch of similar questions, but they didn't help me
I know there are many threads that talk about this problem but I don't
I know there have been quite a few posts on this but none seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.