Works with dynamic sql and list of numbers as return value

Hello.

Problems:

1. How can I insert the USERNAME variable in the string so it will be replaced over time.
2. I intend to return a list of IDS as 1,4,6,7,2 I want to use later in an IN clause.

How to complete the return function with the dynamic sql output variable?
I have no preference to dynamic sql but it was just something that came into my mind
When I thought that the implementation of the obligation to choose a list of offices for specific user groups.
(select statements from the sample are cut short, they're actually really big and I want to reuse this function in my)
BI Publisher data model for multiple modells).


CREATE or REPLACE FUNCTION F_OFFICES (-input parameters)
USERNAME IN VARCHAR2
USERGROUP IN VARCHAR2,
)
-Output parameter
RETURN VARCHAR2 AS
dynSQL VARCHAR2 (1000);
BEGIN

IF USERGROUP = "local" THEN

dynSQL: = 'xxx SELECT FROM CO_B WHERE Userid = username';

ELSIF USERGROUP = "regional" THEN

dynSQL: = "SELECT...". » ;

ELSIF USERGROUP 'federal' = THEN
dynSQL: = "SELECT...". » ;

END IF;

EXECUTE IMMEDIATE dynSQL;

-RETURN?;

END F_OFFICES;


Thanks for any help.

As you have presented essentially pseudo-code we can only give you a Pseudo-solution :)

But the principle is:

...
--Output parameter
RETURN VARCHAR2 AS
    dynSQL VARCHAR2(1000);
    return_value varchar2(30):
BEGIN

  IF USERGROUP = 'local' THEN
    dynSQL:= 'SELECT xxx FROM CO_B WHERE userid = :1'; -- placeholder for parameter

   ...
  END IF;

  EXECUTE IMMEDIATE dynSQL
     using USERNAME -- pass parameters in placeholder order
     into return_value;   -- obviously this must match the projection of the dynamic query 

  RETURN return_value; 

END F_OFFICES ;

This approach is not good if you want to vary the dynamic query projection. In this case, you can use a REF CURSOR or maybe DBMS_SQL.

Cheers, APC

Tags: Database

Similar Questions

  • Tables created in a stored procedure cannot be used with dynamic SQL? The impact?

    There is a thread on the forum which explains how to create tables within a stored procedure (How to create a table in a stored procedure , however, it does create a table as such, but not how to use it (insert, select, update, etc.) the table in the stored procedure.) Looking around and in the light of the tests, it seems that you need to use dynamic SQL statements to execute ddl in a stored procedure in Oracle DB. In addition, it also seems that you cannot use dynamic SQL statements for reuse (insert, select, update, etc.) the table that was created in the stored procedure? Is this really the case?

    If this is the case, I am afraid that if tables cannot be 'created and used"in a stored procedure using the dynamic SQL, as is the case with most of the servers of DB dynamic SQL is not a part of the implementation plan and, therefore, is quite expensive (slow). This is the case with Oracle, and if yes what is the performance impact? (Apparently, with Informix, yield loss is about 3 - 4 times, MS SQL - 4 - 5 times and so on).

    In summary, tables created within a stored procedure cannot be 'used' with dynamic SQL, and if so, what is the impact of performance as such?

    Thank you and best regards,
    Amedeo.

    Published by: AGF on March 17, 2009 10:51

    AGF says:
    Hi, Frank.

    Thank you for your response. I understand that the dynamic SQL is required in this context.

    Unfortunately, I am yet to discover "that seeks to" using temporary tables inside stored procedures. I'm helping a migration from MySQL to Oracle DB, and this was one of the dilemmas encountered. I'll post what is the attempt, when more.

    In Oracle, we use [global temporary Tables | http://www.psoug.org/reference/OLD/gtt.html?PHPSESSID=67b3adaeaf970906c5e037b23ed380c2] aka TWG these tables need only be created once everything like a normal table, but they act differently when they are used. The data inserted in TWG will be visible at the session that inserted data, allowing you to use the table for their own temporary needs while not collide with them of all sessions. The data of the TWG will be automatically deleted (if not deleted programmatically) when a) a commit is issued or b) the session ends according to the parameter that is used during the creation of the TWG. There is no real need in Oracle to create tables dynamically in code.

    I noticed that many people say that the "Creation of the tables within a stored procedure" is not a good idea, but nobody seems necessarily explain why? Think you could elaborate a little bit? Would be appreciated.

    The main reason is that when you come to compile PL/SQL code on the database, all explicit references to tables in the code must correspond to an existing table, otherwise a djab error will occur. This is necessary so that Oracle can validate the columns that are referenced, the data types of those columns etc.. These compilation controls are an important element to ensure that the compiled code is as error free as possible (there is no accounting for the logic of programmers though ;)).

    If you start to create tables dynamically in your PL/SQL code, so any time you want to reference this table you must ensure that you write your SQL queries dynamically too. Once you start doing this, then Oracle will not be able to validate your SQL syntax, check the types of data or SQL logic. This makes your code more difficult to write and harder to debug, because inevitably it contains errors. It also means that for example if you want to write a simple query to get that one out in a variable value (which would take a single line of SQL with static tables), you end up writing a dynamic slider all for her. Very heavy and very messy. You also get the situation in which, if you create tables dynamically in the code, you are also likely to drop tables dynamically in code. If it is a fixed table name, then in an environment multi-user, you get in a mess well when different user sessions are trying to determine if the table exists already or is the last one to use so they can drop etc. What headache! If you create tables with table names, then variable Dynamics not only make you a lot end up creating (and falling) of objects on the database, which can cause an overload on the update of the data dictionary, but how can ensure you that you clean the tables, if your code has an exception any. Indeed, you'll find yourself with redundant tables lying around on your database, may contain sensitive data that should be removed.

    With the TWG, you have none of these issues.

    Also, what is the impact on the performance of the dynamic SQL statements in Oracle? I read some contrasting opinions, some indicating that it is not a lot of difference between static SQL and SQL dynamic in more recent versions of Oracle DB (Re: why dynamic sql is slower than static sql is this true?)

    When the query runs on the database, there will be no difference in performance because it is just a request for enforcement in the SQL engine. Performance problems may occur if your dynamic query is not binding variable in the query correctly (because this would cause difficult analysis of the query rather than sweet), and also the extra time, to dynamically write the query running.

    Another risk of dynamic query is SQL injection which may result in a security risk on the database.

    Good programming will have little need for the tables of dynamically created dynamically or SQL.

  • VPN IPSEC ASA with counterpart with dynamic IP and certificates

    Hello!

    Someone please give me config the work of the ASA for ASA Site to Site IPSEC VPN with counterpart with dynamic IP and authentication certificates.

    He works with PSK authentication. But the connection landed at DefaultRAGroup instead of DefaultL2LGroup with certificate

    authentication.

    Should what special config I ask a DefaultRAGroup to activate the connection?

    Thank you!

    The ASA uses parts of the client cert DN to perform a tunnel-group  lookup to place the user in a group.  When "peer-id-validate req" is  defined the ASA also tries to compare the IKE ID (cert DN) with the  actual cert DN (also received in IKE negotiation), if the comparison  fails the connection fails. know you could set "peer-id-validate cert"  for the time being and the ASA will try to compare the values but allow  the connection if it cannot. 

    In general I would suggest using option "cert."

    With nocheck, we are simply not strict on IKE ID matchin the certificate, which is normally not a problem of security :-)

  • String with dynamic SQL in APEX

    Hello

    I need assistance with dynamic SQL new... sigh...

    Could someone post Oracle rules use for the string of quoted text in the dynamic SQL statements?

    How to encode the following SQL statement in dynamic SQL?

    Select * from emp where State in ('CA', 'HI')

    declare
    l_sql varchar2 (400)
    Start
    l_sql: = ' select * from emp where indicate in ("' CA" '...)
    end;

    Please provide coding rules... Thank you very much

    Chris :)

    Hello

    Two single quotes will be replaced by a single quotation mark when used in a string. So:

    declare
    l_sql varchar2(400)
    begin
    l_sql := 'select * from emp where state in (''CA'' .....
    end;
    

    If you need to close the string to be concatenated to another channel, then you use an other single quotation mark to close the chain and open it again later:

    declare
    l_sql varchar2(400)
    begin
    l_sql := 'select * from emp where state in (''' || a_string_variable || ''' .....
    end;
    

    Andy

  • How to me to work with Outlook 2016 and 10 windows iCloud?

    How to me to work with Outlook 2016 and 10 windows iCloud?

    1. uninstalled and reinstalled iCloud several times - no luck

    2 reinstalled Outlook 2016 - no luck

    3. the module removed and reinstalled only - no luck

    Help please... step by step would be appreciated.

    Hi xinggirl45,

    Thanks for your question.  Can you elaborate a little bit and let us know exactly what does not work?  The resources below provide some troubleshooting steps basic to work with Outlook and iCloud for Windows.

    Get help using Outlook with iCloud for Windows

    Take care.

  • The ZTE mf622 dongle works with Windows XP, and are there any software updates for this?

    Original title: I gave myself a dongle from ZTE MF622.

    The zte mf622 dongle works with windows xp, and are there updates software for it, as in this moment, it comes up saying dial * 99 #, when I try and connect to the internet?

    Hey dude,

    You should check the hardware manufacturer's Web site not in the Windows forums.

    All the best, take care!

  • is it possible that a single screen will work with the slider and second focus mode

    Hello

    can you please tell me is it possible that a single screen will work with cursor mode and second focus mode.

    Thank you

    Hello

    I don't think that it is possible to change the mode of the development of your application outside of in the config.xml file.

    A custom extension may be able to do this, but I have not seen anyone this yet.

  • All-in-one HP Officejet 7300/7400 series, it will work with Windows 7 and how to get the OCR software work?

    I have a HP Officejet 7300/7400 series all-in-one, it will work with Windows 7 and how to get the OCR software work?

    By installing complete software functionality of the HP cd or online installation and connection of the printer only when the installation says too much

    http://h10025.www1.HP.com/ewfrf/wc/softwareCategory?OS=4062&LC=en&cc=UK&DLC=en&sw_lang=&product=391182#n193

  • Hi all that I've been a user of cc muse and created 3 versions of storage for office, ipad, phone. Recently I started working with sensitive muse and do not see a possibility to create 3 versions (in one is not good for me). I tried to enter with Fix lent

    Hi all

    I have been a user of cc muse and created 3 versions of storage for office, ipad, phone.

    Recently I started working with sensitive muse and do not see a possibility to create 3 versions (in one is not good for me).

    I tried to enter with Fix lenth (not liquid) but cannot create a version for phone.

    Thanks for help

    Adaptive and Responsive Design are both supported. Please see the discussion here reagent phone or tablet buttons update-no more?

  • I work with Lightroom 3 and want to use photoshop element 8 to edit photos.

    I work with Lightroom 3 and want to use photoshop element 8 to edit photos.  Problem in Lightroom – photo tab there is no option "EDIT IN".  What should I do?

    Hi ssft,

    Follow the steps in the article to add an external editor in Lightroom Photoshop Elements 8 preferences:

    Adobe Photoshop Lightroom Help | Editing photos Lightroom in Photoshop or Photoshop Elements

  • The HD I had my installed Photoshop has now died. I now work with another HD and reinstall Adobe Photoshop. I had paid about $10 a month. How can I get Photoshop working in my computer, but in a new HD? Thank you, Pablo

    The HD I had my installed Photoshop has now died. I now work with another HD and reinstall Adobe Photoshop. I had paid about $10 a month. How can I get Photoshop working in my computer, but in a new HD? Thank you, Pablo

    Hello

    In order to download Adobe Photoshop, you need to download the desktop app creative cloud first.

    You can please apps download Adobe Creative Cloud | Adobe Creative Cloud free trial

    After downloading the desktop application, you can download photoshop through it.

    Kind regards

    Sandrine

  • I work with SAP HR and I want to print a HRFORM; When I try I get this message: ' Hi ha a problema amb the Adobe Acrobat/Reader. TR is esta performer, tanqueu-lo i torneu - ho a provar. (507:507)

    I work with SAP HR and I want to print a HRFORM; When I try I get this message: ' Hi ha a problema amb the Adobe Acrobat/Reader. TR is esta performer, tanqueu-lo i torneu - ho a provar. (507:507).

    Thanks in advance.

    Juan

    Thank you very much, Rahul, but the problem is in the patch level of the SAP Logon for Windows: with level patch 2 it doesn't work; with the patch of level 7, I can view and print the pdf HRForm without problems.

    Kind regards

    Juan

  • I'm working with Acrobat XI and when I try to add text to the geopdf file I get a popup screen that says it is a secure document and editing is not allowed. How can I fix it?

    I'm working with Acrobat XI and when I try to add text to a geopdf file I get a popup screen that says it is a secure document and editing is not allowed. How can I fix it?

    I thought about it... need to use the defined comment tool, not the set of editing tools.

  • Using Windows XP and works with Fireworks CS6 and Dreamweaver CS6.

    I am a student and don't have Windows XP. I work with CS6 Fireworks and Dreamweaver CS6. I can just the two of them, or do I need to subscribe to the creative cloud? Also, I can't Dreamweaver CS6 on the cloud? I know that I can not use Dreamweaver CC with XP.

    Moved to this discussion in the creative forum cloud download & install.

    Afec1707 I'm sorry, but you must have Windows 7 or later version for your operating system.  You can find the system requirements for using cloud applications to creative system requirements | Creative cloud.

  • Code works with Dreamweaver CS6 and not CC

    " < p > < a href =" javascript:void (0) "onclick =" window.open ('http://www.anything.comt', "'top = 20, left = 20, toolbar = 0, statusbar = 1, scrollbars = 1, resizable = 1')" title = "title" > name < /a > < /p > working with Dreamweaver CS6 and not CC


    [Edited the title and moved to Dreamweaver - JTS]

    I'm not sure I understand your point of view.

    I just tested the behavior of Dreamweaver CC open in browser window. It creates slightly different Dreamweaver CS6 code, but it works correctly. This is what is created in the HTML code:

    My http://www.adobe.com/ mywindow "," "," toolbar = Yes, location = Yes, status = Yes, menubar = Yes, ars scrollb = yes, resizable = yes') "> link"

    CC of Dreamweaver also adds the following code to theto the page:

    The MM_openBrWindow() function definition is necessary in thefor the code to work. But it really works OK.

    In regards to the _self and _blank not working do not, I find no problem with how Dreamweaver CC manages them.

    However, if you feel happier working with Dreamweaver CS6, you can download it from the following page:

    https://creative.Adobe.com/products/Dreamweaver

    Open the menu drop-down slot "in this version" and select next Dreamweaver CS6, as shown in the screenshot:

    With Dreamweaver CS6 selected, click the big button at the top right of the page download. You can download CS6 then install it alongside CC of Dreamweaver.

Maybe you are looking for

  • Suspicious e-mail

    Hi all! I received an e-mail very suspecy to a lady saying that she was the principal of cloud Zip. That I had to pay him $35 one time, otherwise I would lose all my files. HOW if I just top ranked my storage with Apple? Thank you very much!! NAnda M

  • Microsoft license number

    I received phone calls on my home line telling me that my number of Microsoft license has been revoked.  I asked around my friends, and none of them have heard of this type of call. In the first place, I did not understand his Asian accent and when I

  • Between my Pc and a phone Nokia N70 my blue tooth does not

    Between my PC and a phone Nokia N70 my Bluetooth does not.

  • stimulate a testing system

    I wonder if this is possible.I work with labview 7.0If my system I using labviewo 7.0 hooked up with a computer dell; with a chassis pxi with about 6 pci cards.the dell computer I have 2 serial ports.My question is...If I run labview 7.0 on my labtop

  • Clear cursors programmatically

    Hello!  I'm trying to find the right way by which sliders should be removed by LV program in 2012. There is an old thread on this topic: http://forums.ni.com/t5/LabVIEW/resetting-cursor-lists-programmatically/m-p/1490310/highlight/true#M... I origina