Dynamic ordering ambiguous column issue

I'm working on a stored procedure that will accept a column name from a web application.  The stored procedure will use the @SortColumn to sort the results of the query.  I'm receiving ambiguous column errors when the column values are passed in
to the query.  The original query is written using dynamic sql but I created a simple query to reproduce the issue.   Can someone review the sample below and let me know if there is a way to resolve the error. 
In the example below, the Over(Order by Emp_ID) section is causing the error. The order by is expecting e.Emp_ID as the order by value.  Is there anyway for me to rewrite the logic to allow the correct column alias to be using in the order by statement? 
Ultimately, I would like to pass in a paramter for the order by column.  Ex.   Over(Order by ' + @SortColunm + '
SELECT RowNum
,Emp_ID
,First_Name
,Last_Name
From (Select Distinct ROW_NUMBER() OVER(Order by Emp_ID) as RowNum,
e.Emp_ID as Emp_ID
,e.First_Name as First_Name
,e.Last_Name as Last_Name
FROM Employee e
LEFT OUTER JOIN Team t ON e.team_id = t.team_Id AND e.Emp_id = t.Emp_Id
LEFT OUTER JOIN AccrualType at ON e.Accrual_Type = at.Type
LEFT OUTER JOIN ClosingProcess cp ON e.Emp_id = cp.Emp_ID
where Last_Name like 's%'
) As Employees
order by Emp_ID

I've updated my query to use a cte and also included the Row_Number logic.  I'm now receiving "Invalid column name 'RowNum".  Can anyone explain what I'm doing wrong in the code below?   I'm trying to create a stored procedure
that accepts a parameter to handle paging and sorting.  The dynamic sql is needed for the SortExpression.
declare @SortExpression varchar(50) = 'Last_Name';
declare @DynSql varchar(max)='';
declare @Emp_ID NVARCHAR(50) = NULL
declare @First_Name VARCHAR(50) = NULL
declare @Last_Name VARCHAR(50) = 's'
declare @StartIndex INT
declare @MaximumRows INT
set @DynSql=
With Employees as
( Select Distinct
e.Emp_ID as Emp_ID
,First_Name
,Last_Name
FROM Employee e
LEFT OUTER JOIN Team t ON e.team_id = t.team_Id AND e.Emp_id = t.Emp_Id
LEFT OUTER JOIN AccrualType at ON e.Accrual_Type = at.Type
LEFT OUTER JOIN ClosingProcess cp ON e.Emp_id = cp.Emp_ID
SELECT ROW_NUMBER() Over (Order By ' + @SortExpression + ') As RowNum,
Emp_ID
,First_Name
,Last_Name
FROM Employees
WHERE RowNum BETWEEN ' + CAST(@StartIndex as varchar(10)) + ' AND ' + '(' + CAST(@StartIndex as varchar(10))+ CAST(@MaximumRows as varchar(10))+ ') - 1 '
If @Emp_ID is not null
Set @DynSql = @DynSql + ' And (Emp_ID = @Emp_ID)'
If @First_Name is not null
Set @DynSql = @DynSql + ' And (First_Name = @First_Name)'
If @Last_Name is not null
Set @DynSql = @DynSql + ' And (Last_Name = @Last_Name)'
exec (@DynSql)

Similar Messages

  • Dynamic internal table column issue

    Hi
    i have ALV report with dynamic internal table.after i build the internal table and fieldcatalog i have problem  i.e. when grid is displayed then one of the column value is coming in the next column.i populated col_pos in field catalog also and in the debug mode data is populated correctly for respective columns in fieldcatalog and dynamic internal table. But when it is displayed i have this problem.
    any inputs on this?

    Hi Moorthy,
    Did you perform an ALV consistency check?
           Check the below given links as well.
    The Consistency Check - ALV Grid Control (BC-SRV-ALV) - SAP Library
    SAP ALV Consistency Check
    Regards,
    Philip.

  • TRY CATCH with dynamic ORDER BY

    Some stored procs need a dynamic ORDER BY, unfortunately.
    Can I use a TRY CATCH with ORDER BY?
    SELECT ...
    FROM ...
    WHERE...
    BEGIN TRY
    ORDER BY ...complex stuff
    END TRY
    BEGIN CATCH
    ORDER BY ColumnA
    END CATCH

    Hi mmmtbig,
    Syntactically, it is ok to use any valid sql statement in the TRY AND CATCH block.
    SELECT 1 as num
    BEGIN TRY
    SELECT 1/0 AS seq UNION SELECT 2 ORDER BY seq;
    END TRY
    BEGIN CATCH
    PRINT ERROR_MESSAGE();
    SELECT 1 AS seq UNION SELECT 2 ORDER BY seq;
    END CATCH
    For more details, You can reference
    TRY...CATCH.
    The confusion about your question is the "dynamic ORDER BY". What do you mean by that, can you elaborate your requirement, like in what scenario you'd like to use the code block in your post and what issue have stopped you from doing that?
    If you have any feedback on our support, please click
    here.
    Eric Zhang
    TechNet Community Support

  • I am unable to get order by column name in oracle report parameter form

    i created query like following in query builder
    SELECT CASE_NO, COURT_ID, CASE_TYPE,
    INITCAP(PLAINTIFF) PLAINTIFF,INITCAP( DEFENDENT) DEFENDENT,
    INITCAP(COUNSEL) COUNSEL, START_DATE, PREVIOUS_HEARING_DATE,
    NEXT_HEARING_DATE,INITCAP( DESCRIPTION) DESCRIPTION,
    INITCAP(RELIEF) RELIEF,INITCAP(EXTENT) EXTENT,
    DECREE_DATE,INITCAP(STATUS) STATUS,INITCAP( LOCATION) LOCATION,
    LEGAL_FILE_NO, MSNO
    FROM L_CASE_MASTER_MAIN
    WHERE to_char(NEXT_HEARING_DATE,'DD-MON-YYYY')=:P_NHD ORDER BY :P_COL
    and i created parameter form for these two bind variables :P_NHD,:P_COL
    in,:P_COL i wrote lov as Select trim(COLUMN_NAME) from user_tab_columns where table_name='L_CASE_MASTER_MAIN'  to get all the columns.
    in parameter form i am able to get all the columns but when i am generating report it is not giving results as per my ordered column.
    kindly let me know the solution
    Report Version :Oracle Reports 11g
    Db Version:Oracle  11g

    Hi,
    Ordering the column first takes place from the Data Model Itself.
    Please Check your column ordering in the report data model. If it is not ordered in a right way then Rearrange it.
    Remove your order by in query...
    If any issues... let me know
    Regards,
    Soofi

  • Dynamic Order By on Report Builder 10g

    Hi,
    How can I do a dynamic order by clause using user parameters on Report Builder 10g? I need to set dynamically the column and the direction on the clause.
    Thanks.
    Bruno Galletti
    Brazil.

    Hi Bruno / Rainer,
    I am implementing the same solution, and after adding the lexical parameter reference in the SQL statement and calling the report from Oracle Forms the report failed with the following error on the report server:
    Terminated with error: <br>REP-300: missing expression select wcsr_no , wcsr_date , wcsr_rec_week_zone , wcsr_loc_house_num || ' ' || wcsr_loc_pre_dir || ' ' || wcsr_loc_street_name || ' ' || wcsr_loc_street_type || ' ' || wcsr_loc_post_dir || ' ' || wcsr_loc_unit || ' ' || wcsr_loc_city || ' ' || wcsr ==>
    No change has been done to the SQL statement except adding the &p_orderby parameter at the end of the query.
    Any suggestions? Are all parameters defined as "bind" parameters on the Repotr Object Navigator.?
    Thanks
    Gabriel Aguirre
    [email protected]

  • How do I change the order of columns in the responses table?

    How do i change the order of columns in the responses table in FormsCentral?  The default setting puts them in reverse order!

    Hi,
    The issue with the default ordering of columns in the response table is something that we hope to address in the next update to FormsCentral. In the meantime, you can reorder the columns by selecting them then click/drag using the gray area above the column name. As shown below, an orange marker will show you where the columns will be placed when you release the mouse button.
    Sorry for the inconvenience.
    Regards,
    Brian

  • Dynamic order by with updateable report

    I have a region of type sql query (updateable report)
    I want to add a dynamic order by clause based on the value of a page item
    ie order by nvl(:PX_ORDER, 1)
    I can get the dynamic ordering to work using the 'function returning sql query' reqion type but having a little problem with the sql query type above.
    nb. Have also tried ORDER BY decode(:PX_ORDER, 2, 2, 1) but no luck.

    Thanks Vikas,
    I was using column numbers in the decode. Needed to specify the actual column_name including any "" surrounding it.
    decode(:PX_ORDER, 2, "COL2_NAME", "COL1_NAME")
    works
    any ideas on how to dynamically flip the ASC, DESC ordering in a SQL statement?? The 'ASK TOM' note you mentioned above states you should use dynamic query strings if this functionality is required.
    Unfortunately updateable report regions can't be created using dynamic query strings.

  • Ambiguous Column Naming Error In Query

    I'm getting a strange Oracle error when I try and run a (relatively)
    straightforward query.
    This is the error I get:
    ORA-12801: error signaled in parallel query server P000
    ORA-00960: ambiguous column naming in select list
    This is the query that Kodo is generating:
    SELECT DISTINCT
         t0.EVENT_ID, t0.ACTIVE_YN, t0.CREAT_BY, t0.CREAT_DT,
    t0.REPORTED_CURNCY_CD,
         t0.PUB_DESCR_TX, t0.FREQUENCY, t0.MOD_BY, t0.MOD_DT, t0.EVENT_NAME,
    t0.CLI_ID,
         t0.ORG_UNIT_ID, t0.RATING_CD, t0.TOTAL_MONEY, t0.RISK_CLASS_ID, t0.TYPE,
         t0.STATUS_ID, t0.LOSS_LOC_CUR, t0.EVENT_TYPE, t0.EVENT_NAME
    FROM
         DEF_EVENT_DATE t2, EVENT t0, EVENT_DATE t1
    WHERE
         (((t2.DATE_TYPE_CD = 'Start Occurrence Date' AND
         t1.VALUE_DT <= {ts '2002-10-09 14:17:02.422'}) AND
         t0.CLI_ID = 2963) AND
         t0.EVENT_ID = t1.EVENT_ID AND
         t1.DATE_TYPE_CD = t2.DATE_TYPE_CD)
    ORDER BY
         t0.EVENT_NAME ASC
    t0.EVENT_NAME is being selected twice. I am guessing this has something to
    do with the ORDER BY clause.
    But in conjunction with the DISTINCT keyword this is breaking the query.
    This can be shown in the following simple example:
    SELECT DISTINCT
         t0.EVENT_NAME, t0.EVENT_NAME
    FROM
         EVENT t0
    ORDER BY
         t0.EVENT_NAME ASC;
    This throws the same 'ambiguous column' error as before.
    My question is why is the DISTINCT keyword being used in this query and
    how do I fix it?
    Thanks in advance,
    Simon

    i'm not sure,
    but the problem you reported tends to be an oracle problem.
    (selecting a column twice should be not a problem)
    the ORA-error you reported is a parallel query error ...
    and parallel queries are bound to some special conditions ...
    try: alter table <table> parallel 1
    and apply your test again.
    "Simon Horne" <[email protected]> schrieb im Newsbeitrag
    news:ao1js8$i6r$[email protected]..
    I'm getting a strange Oracle error when I try and run a (relatively)
    straightforward query.
    This is the error I get:
    ORA-12801: error signaled in parallel query server P000
    ORA-00960: ambiguous column naming in select list
    This is the query that Kodo is generating:
    SELECT DISTINCT
    t0.EVENT_ID, t0.ACTIVE_YN, t0.CREAT_BY, t0.CREAT_DT,
    t0.REPORTED_CURNCY_CD,
    t0.PUB_DESCR_TX, t0.FREQUENCY, t0.MOD_BY, t0.MOD_DT, t0.EVENT_NAME,
    t0.CLI_ID,
    t0.ORG_UNIT_ID, t0.RATING_CD, t0.TOTAL_MONEY, t0.RISK_CLASS_ID, t0.TYPE,
    t0.STATUS_ID, t0.LOSS_LOC_CUR, t0.EVENT_TYPE, t0.EVENT_NAME
    FROM
    DEF_EVENT_DATE t2, EVENT t0, EVENT_DATE t1
    WHERE
    (((t2.DATE_TYPE_CD = 'Start Occurrence Date' AND
    t1.VALUE_DT <= {ts '2002-10-09 14:17:02.422'}) AND
    t0.CLI_ID = 2963) AND
    t0.EVENT_ID = t1.EVENT_ID AND
    t1.DATE_TYPE_CD = t2.DATE_TYPE_CD)
    ORDER BY
    t0.EVENT_NAME ASC
    t0.EVENT_NAME is being selected twice. I am guessing this has something to
    do with the ORDER BY clause.
    But in conjunction with the DISTINCT keyword this is breaking the query.
    This can be shown in the following simple example:
    SELECT DISTINCT
    t0.EVENT_NAME, t0.EVENT_NAME
    FROM
    EVENT t0
    ORDER BY
    t0.EVENT_NAME ASC;
    This throws the same 'ambiguous column' error as before.
    My question is why is the DISTINCT keyword being used in this query and
    how do I fix it?
    Thanks in advance,
    Simon

  • Sales orders with credit issues taking availability stock

    Hi,
    I am doing confirmation using Global ATP in APO. The confirmation is well done, but the confirmed quantity for sales orders with clients that have credit issues is taken the available sotck for other sales orders with ok clients that I want to confirm the quantity they ordered.
    For example:
    Stock: 10 PC
    Client A order 15 PC and 10 are confirmed, but the ordered is retained becuse of credit issues with client A.
    Then client B order 3 PC, but nothing is confirmed because the ordered for client A is reserving the stock (note: this would be ok if client A had not credit issues).
    So, I want that the 3 PC for client B are confirmed and the 10 PC not reserved for the cient A while client A has credit issues.
    How could I configured, so that the orders with credit issues do not reserve stock for the incoming orders?
    Thanks a lot.

    Hi DB49,
    thanks for the quick answer.
    I checked the configuration in the R3 and seems to be fine. To prove it, I disconnect APO and did the availability check in the R3 system and it is working fine; the orders quantities that are blocked because of credit issues, do not reserve stock for incoming orders.........son new incoming orders are well confirmed.
    But after connecting APO the problem continues. I checked the next points.
    In R3 transaction CO09: takes to the APO screen and you see the ATP available quantity OK (do not consider the quantities of the credit issues orders). But at the moment that you make the order (VA01) it does not confirm any stock and if you select the line and go to the ATP screen (the same you get from the CO09) the quantities of credit issues orders are taken into account..the ATP availability quantity changes from the first going directly from the CO09.
    What could it be?
    Note: it was working fine with APO, I started to have the issue after implementing note 1262475 in APO. This note was becuase the BAPI_APOATP_CHECK was not brigning any value for the ATPQTY_***.
    Thanks a lot!!!

  • Change the order of columns in a report

    hi all.
    i can't change the order of columns in a report not just by altering the select statement. where can i change it?
    thanks.

    hi master
    sir i use 6i report i see full report but i culd not found
    report region and report attributes
    sir please give me step or idea or tree where report region and report attrinutes"
    thanking you
    aamir

  • How to restrict the GR for Production Order when Goods Issue is not done

    Hi Gurus
    How to restrict the GR for Production Order when all the required components for production order are not issued with all required quantity. Even for partial issue system should not allow GR with 101. The user status with RMWA, RMWF & CGFB is not working.
    Pls suggest best solution.
    Abhijit.

    Hi,
    You can club together the GI nad GR at the time of confirmation..
    I.e Backflush for the components and auto GR for the Product.
    So that you can stream line the Process.
    The best Option would be to use the User Exit:
    Enhancement - MBCF0002
    Functional Module - EXIT_SAPMM07M_001
    Include - ZXMBCU02
    Refer below link for further details..
    How to stop the goods receipt before issueing the goods for production orde
    Regards,
    Siva

  • In Lion 10.7.5 is there a way to view files in alphabetical order in column view?

    In Lion 10.7.5 when viewing files/folders in column view, if I select arrange by name it does not sort the names alphabetically.  Is this a bug or is there a way to view files in alphabetical order in column view?

    I've been using Column View for years and have it set to None:
    It has always shown in alphabetical order automatically.

  • Dynamic Number of Column in a table

    Hi guys,
    I have a requirement that needs dynamic number of column in a tale.
    It is possible to do this in Adobe forms.
    Thanks,
    Chirantan

    Hello. It of course is possible in Adobe.
    You need to write a simple script using JavaScript or FormCalc to hide or show columns according to some special value. You will work with the presence attribute of the object. E.g. MYFIELD.presence = "visible" or "hidden" or "invisible". You will need to change your subforms content to flowed.
    Use these guides:
    http://www.adobe.com/devnet/livecycle/articles/lc_designer_scripting_basics/lc_designer_scripting_basics.pdf
    help.adobe.com/en_US/livecycle/es/FormCalc.pdf
    Hope this helps, good luck, Otto

  • Dynamically set number-columns-spanned

    Hi all,
    I have a cross tab which has a dynamic number of columns. Each column has a header "Location" (which is determined by the data), but the table also has a master header "Volume" which should span all of the locations. How can I acheive this?
    I currently have:
    <xsl:attribute xdofo:ctx="block" name="number-columns-spanned">2</xsl:attribute> Volumne
    But this is obviously static.
    Many Thanks
    Andy

    Hi lmd2,
    Attached is a quick example of one way to do what you are asking. This is only a general example, and there may be a bettter implementation depending on the architecture of your application.  The link here also has information on setting the number of sockets for a specific sequence file. Hope this helps.
    Regards,
    Ebele O.
    National Instruments
    Attachments:
    ParallelModel.seq ‏455 KB

  • Dynamic selection of columns in report and print the output

    Hi,
    I tried to have dynamic selection of columns in SQL query using lexical parameter. The hitch is how to print those selected column in excel ouput ?
    Suppose
    I have select &col from table_name
    Initial value of &col is col1 and in data layout only col1 appears
    where &col was replaced with more than a column (col1,col2,col3)
    now how to print the column selected at the run time,
    i have coded in Before Report Trigger
    :cell_val := ' <td><rw:field id:"cl1" src="col1"></rw:field></td>
    <td><rw:field id:"cl2" src="col2"></rw:field></td>
    <td><rw:field id:"cl3" src="col3"></rw:field></td> ' ;
    and this user parameter was replaced in web source using JSP expression
    <rw:getvalue id: "v_cell" src:"cell_val"></rw:getvalue>
    <rw:foreach src:"g_val">
    <table>
    <tr>
    <% =v_cell%>
    </tr>
    </table>
    </rw:foreach>
    but in the output i can see only blank columns
    Regards
    Suresh

    The strings replaced by expression <%= v_cell %>
    should print the field values contained in col1,col2,col3 columns, but nothing is printed . i think the custom tags (rw...) are not replaced by the jsp expression properly.

Maybe you are looking for

  • Can't open Audition CC in IOS system

    The software always report a error when i open it, this is the log: Process:               Adobe Audition CC 2014 [6084] Path:                  /Applications/Adobe Audition CC 2014/Adobe Audition CC 2014.app/Contents/MacOS/Adobe Audition CC 2014 Iden

  • Mute button doesn't work to put phone in standby mode, help??

    I have a blackberry curve and the mute button will not work to put my phone in standby.  It does work to mute a phone call, however.  It has done this a couple of times, where it will work for awhile, and then stop working for awhile.  I'm not sure i

  • View of Call Waiting Option

    Is there any OS which helps to view call waiting option when someone in engaged in a call? I'm on 4.2 which doesn't spport this feature. Message Edited by tanzim on 04-04-2008 08:10 PM tanzim                                                           

  • Error when trying to sync

    I had my iPod 5th Gen set up to sync on my MacBook, but would like to sync it with my Win 7 box instead. For some reason (perhaps very simple were I not such a noob) it won't work. If I attempt a "restore" from my Windows box I get an error, and if I

  • Port# - proxy settings

    I have dreamweaver CS6 through adobe cloud. I am using in a commercial application (business). My host changed my port# from 21 to 12420. I changed all the appropriate settings and it is not working. I test the FTP connection and dreamweaver says (su