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

  • Home
  • SEARCH
  • 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 8128633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:57:44+00:00 2026-06-06T07:57:44+00:00

I’ve a query that creates a SQL Statement as a field. I want to

  • 0

I’ve a query that creates a SQL Statement as a field. I want to execute this statement and return the recordset in SSRS report.

select 'select '||FILE_ID||' FILE_ID,'||
ltrim(sys_connect_by_path('REC_FLD_'||FIELD_NUMBER||' "'||FIELD_NAME||'"',','),',')||
' from RESPONSE_DETAILS where FILE_ID=' ||FILE_ID||';'
from (select t.*,count(*) over (partition by FILE_ID) cnt from RESPONSE_METADATA t)
where cnt=FIELD_NUMBER start with FIELD_NUMBER=1 
connect by prior FILE_ID=FILE_ID and prior FIELD_NUMBER=FIELD_NUMBER-1

This generates a SQL stetment – however I want this SQL to be executed.

This is an extension of this question.

I’ve tried to use execute immediate , cursors, dbms_sql but it does not produce output. Using it on toad. All it says is “PL/SQL procedure successfully completed”

Using the following

Declare 
  sql_stmt  VARCHAR2(3000);
  l_cursor  SYS_REFCURSOR;
  TYPE RefCurTyp    IS REF CURSOR;
  v_cursor          RefCurTyp;
  CURSOR c1 is
    select 'select '||FILE_ID||' FILE_ID,'||
    ltrim(sys_connect_by_path('REC_FLD_'||FIELD_NUMBER||' "'||FIELD_NAME||'"',','),',')||
    ' from RESPONSE_DETAILS where FILE_ID=' ||FILE_ID||';'
    from (select t.*,count(*) over (partition by FILE_ID) cnt from RESPONSE_METADATA t)
    where cnt=FIELD_NUMBER start with FIELD_NUMBER=1 
    connect by prior FILE_ID=FILE_ID and prior FIELD_NUMBER=FIELD_NUMBER-1;
BEGIN
  open c1;
  FETCH C1 into sql_stmt ;
  dbms_output.put_line(sql_stmt);
  close c1; 
  EXECUTE IMMEDIATE sql_stmt;
  open v_cursor for sql_stmt;
  return l_cursor;
  close l_cursor ;
END;
  • 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-06-06T07:57:47+00:00Added an answer on June 6, 2026 at 7:57 am

    An anonymous PL/SQL block cannot return any data to the caller. If you want to return a SYS_REFCURSOR to the calling application, you would need to create a function (or a procedure). For example

    CREATE OR REPLACE FUNCTION get_results
      RETURN sys_refcursor
    IS
      l_sql_stmt  VARCHAR2(3000);
      l_cursor    SYS_REFCURSOR;
    BEGIN
      select 'select '||FILE_ID||' FILE_ID,'||
              ltrim(sys_connect_by_path('REC_FLD_'||FIELD_NUMBER||' "'||FIELD_NAME||'"',','),',')||
             ' from RESPONSE_DETAILS where FILE_ID = ' ||FILE_ID||';'
        into l_sql_stmt
        from (select t.*,count(*) over (partition by FILE_ID) cnt from RESPONSE_METADATA t)
       where cnt=FIELD_NUMBER 
       start with FIELD_NUMBER=1 
     connect by prior FILE_ID=FILE_ID 
            and prior FIELD_NUMBER=FIELD_NUMBER-1;
    
      dbms_output.put_line(l_sql_stmt);
      open l_cursor for sql_stmt;
      return l_cursor;
    END;
    

    I am assuming from your code that you expect your SELECT statement to return a single SQL statement– your code is fetching only one row from a query that potentially returns multiple SQL statements. I’m assuming that you only fetch one because you only expect the SELECT statement to return one row. Otherwise, since your query lacks an ORDER BY, you are executing arbitrarily one of N SQL statements that your code is generating.

    If you are regularly going to be calling this method, you would almost certainly want to use bind variables in your dynamic SQL statement for the file_id rather than generating non-sharable SQL statements. I haven’t made that change here.

    There is another StackOverflow thread on calling a stored function returning a sys_refcursor from SSRS.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.