Obtain string of IN variable name in PL/SQL
Hi,
Odd question. Is there a way to access an IN variable's object name in a procedure?
eg.
procedure (var1 IN VARCHAR2(6),
var2 IN VARCHAR2(6),
var3 IN VARCHAR2(6))
IS....You run this procedure and pass in parameters of:
var1 => 'abcdef',
var2 => 'defghi',
var3 => 'ghikjl';
In the code, I want to access the number in the string of the +variable name: var1+
In a naive sense,
num_of_in_var := substr(var1, 4,1) would be what i was hoping to achieve, but,
var1 is set to 'abcdef' so,
num_of_in_var := substr(var1, 4,1) would return 'd'.
Any thoughts?
Thanks
Edited by: chris001 on Nov 20, 2012 1:16 PM
Here's an example of how (what you want to do using a procedure or function), can be done using a ADT/UDT (Advance/User Defined Type) in Oracle.
This approach will not work for standard procedures and functions as this type of dynamic referencing of the code unit/object to itself, is not possible.
SQL> create or replace type TSomeObject as object(
2 name varchar2(10),
3 id integer,
4 day date,
5
6 member function PropertyByNumber( n integer ) return varchar2
7 );
8 /
Type created.
SQL>
SQL> create or replace type body TSomeObject as
2
3 member function PropertyByNumber( n integer ) return varchar2 is
4 PLSQL_PROP_GET constant varchar2(1000) :=
5 'declare
6 obj TSomeObject;
7 begin
8 obj := :1;
9 :2 := to_char( obj.#PROPERTY# );
10 end;';
11
12 type TStrings is table of varchar2(30);
13 property TStrings;
14 dynamicBlock varchar2(1000);
15 res varchar2(4000);
16 begin
17 select
18 a.attr_name bulk collect into property
19 from user_type_attrs a
20 where a.type_name = 'TSOMEOBJECT'
21 order by
22 a.attr_no;
23
24 dynamicBlock := replace( PLSQL_PROP_GET, '#PROPERTY#', property(n) );
25 execute immediate dynamicBlock
26 using in self,
27 out res;
28
29 return( res );
30 end;
31
32 end;
33 /
Type body created.
SQL>
SQL> declare
2 obj TSomeObject;
3 begin
4 obj := new TSomeObject( 'John Doe', 123, trunc(sysdate) );
5
6 for i in 1..3 loop
7 dbms_output.put_line( 'property '||i||'='||obj.PropertyByNumber(i) );
8 end loop;
9 end;
10 /
property 1=John Doe
property 2=123
property 3=2012/11/23 00:00:00
PL/SQL procedure successfully completed.
SQL> Simplistic example (the PropertyByNumber could be a static class method defined in the abstract parent class) - and one that requires the object to essentially duplicate itself via a bind variable call to dynamic code. Not really the best of approaches, but demonstrates the flexibility (to do even interestingly weird stuff) in PL/SQL.
Similar Messages
-
im looping through databases on a server & building a connection string to each database.
$SQLConn.ConnectionString = "Server=$SrvName; Database=$DBName; User ID =DBLogin; Password=myPassword;"
The problem is i get this error:
Only variable names (i.e.: $variable) may be used as the target of an assignment statement
I can put the code into an Inlinescript, but then I lose the ability to perform paralellism. Is there any way to construct the connection string in PS Workflow without using an Inlinescript?Hi Winston,
Why not just wrap the InlineScript blocks in a Parallel block, to cause them to execute in parallel?
For example:
workflow foo {
parallel {
inlinescript {
start-sleep -Seconds (Get-Random -Minimum 1 -maximum 5)
"a"
inlinescript {
start-sleep -Seconds (Get-Random -Minimum 1 -maximum 5)
"b"
Sometimes outputs "a b" and sometimes outputs "b a" -
Generate variable name from a String
I've got a String "MyVariable" and I need to reference a static variable named MyStaticClass.MyVariable. How do you turn a String into a variable name?
You can't easily do it... you can using reflection:
MyStaticClass.class.getField().getXXX("MyVariable") //depending on what type the field is...But this seems like it may be a flawed design. Maybe you should try making a static HashMap in the class, use a Static init block and static get/set methods:
public class StaticClass
static Map properties;
static
properties = new HashMap();
properties.put("MyVariable", "Some Value");
properties.put("OtherVariable", new Integer(3));
public static Object get(String property)
return properties.get(property);
public static void set(String property, Object value)
//Maybe some code checking to make sure object is right type
//or don't use a set method at all to make the properties immutable
properties.put(property, value);
} -
Concatinating Strings into Variable Names
Hi
I'm not sure if this is even possible but here goes.
Is there anyway of concatinating (sp? Join together!) two strings and then use the new string as a variable name.
The reason I ask is that, depending upon what character I read into my program I will want to modify an array of data associated with that character.
For example if the character read in was @ I would want to modify the array data@. So my question is, is it possible to read in a character such as @, join it with the string "data" and then use the resulting string as a variable name?
Apologies if this has a simple solution but I'm struggling to come up with it by myself.
Many thanks.Hey
Thanks for the advice. The problem is that I'm storing the arrays in a hash map already. This is because each character has more than one piece of data associated with it , and as far as I can tell each key entry in a hash table has one and only one piece of data associated with it. But if there is a way of storing multiple pieces of information with a particular key, then that could be a possible solution....
Any suggestions? -
Management Report - Cannot assign an empty string to host variable 10
We ran a Management Report for the first time after completing the User/Role/Profile Full Sync and the user/Role/Profile Batch Risk Analysis. The job failed with the following error:
Oct 16, 2008 12:13:29 PM com.virsa.cc.xsys.bg.BatchRiskAnalysis runBkgMgmReport
WARNING: Exception in Management Report Job: Cannot assign an empty string to host variable 10.
com.sap.sql.log.OpenSQLException: Cannot assign an empty string to host variable 10.
We are on GRC 5.3 support pack 4. Has anybody encountered this error? What does host variable 10 refer to?I checked our USOBT_C table and we have a lot of empty values. If this is allowed to be empty in SAP and this is the table that is uploaded into CC, why would all the fields in CC tables then be defined as NOT NULL? We suspect that since we had uploaded USOBT_C under 5.2 and if this had been an issue before, it may have been corrected when we upgraded to 5.3 but then the old data is still in our system. Is there an easy way to get around or correct this?
Thanks. -
Dynamic Variable name (for int/long) from a String variable
Hi,
I want to give a int/long variable name from a String.
for ex.
String str = lookup + "Id";
lookup is a String variable coming from XML. Now, for instance lookup="name". So str = "nameId".
Now I want to create a int/long variable by nameId.
Could anybody tell me the way how to do. Please don't tell to use MAP.
Edited by: Shah on Dec 5, 2007 3:26 PMWell you can't. Use a Map.
The compiler translates variable names into slot numbers, either within an object or withing the local "stack frame" and these slot numbers are assigned names at compile time. No new slots can be created at run time. Java is not Basic.
Reflection allows you to find existing field names and methods (not local variables), so it's possible to map, for example, XML attribute names to field names or setters in an object but the names have to be known at compile time. -
Hi, I'm really new to TestStand, so this is probably an easy question:
I'm using TestStand 4.5 and I'm trying to convert the name of a variable, Locals.MaxArrayIndex, which is a numeric variable, into a String, Locals.VarString.
I know that the Str() function can be used to convert a numeric into a String, but how can I store the actual words "Locals.MaxArrayIndex" in a string?
Thanks!Hmmm...it works great for Parameters that are arrays, but for me it won't work if the Parameter is a numeric variable. Maybe I'm making a mistake somewhere else that I'm not picking up; so I attached the sequence file if you want to take a look at it.
The issue arises in the 'Else' statement. I'm pretty sure the syntax in my function and popup are correct, but the String (ParamName) that is supposed to hold the value of the Parameter variable name (DataSelect) doesn't seem to ever be filled and outputted in the error message.
I would really appreciate it if any of you guys could have a look at the file (I'm sure I'm making some kind of really rookie mistake)
Thanks!
Attachments:
GenerateError.seq 9 KB -
Putting quote round a variable name to make a string
Hi,
Hopefully this is a fairly simple task.
I would like to take a variable name, say Hyp, from my main program and call it in a class, captured in a variable called A and create a string with double quotes surrounding it... to give an answer of "Hyp" which can be used in my program for a further class call which needs the name in quotes.
I've tried using
String concat = """+A+""" but this results in an invalid AssignmentOperator
If I use
String concat = ""+A+"" I get Hyp with out the quotes - not what is required
If I use
String concat = "'"+A+"'" I get 'Hyp' which does not work with the further class which requires "Hyp"
Can anyone help?Escape it with a \ like this:
String concat = "\""+A+"\""; -
Substitute a variable name by String ?
Hi
is it possible to substitute a variable name by a String ? if yes then please tell me how :-)
thx
painYou might be able to use the Reflection API to get what you want, but every other time this question's been asked the asker really needed to use a HashMap instead...
Good Luck
Lee -
Accessing variable names using String
I have to check up to 13 JCheckBox variables to see if they are checked or not, and I have the variables named: count1, count2, count3,...,count13. I wanted to know if there was a way to go through them in a while loop until I found the last one that was checked. I want to do the following:
int i = 1;
while([count+i].isSelected()) {
i++;
int foodCount = i;
//foodCount is stored in database
I am doing this for a pet store and I don't want to have 12 extra columns in the database - I just want to have one for the overall food count. Any ideas how I can dynamically create a variable name?Put the JCheckBoxes in an arrayJCheckBox[] myCheckBoxes = {check1, check2, check3,...,check13};
JCheckBox someCheckBox = null;
for (int x=0; x<myCheckBoxes.length; x++) {
if (myCheckBoxes[x].isSelected()) someCheckBox = myCheckBox[x];
} -
Stored Procedure - variable name inside string [EXEC]
Hi everyone
I'm looking for a solution for this problem:
I've created a stored procedure, in which i need to insert a record in a table.
The point is i don't know the exact column to put my values and which values to save: it depends from the input data.
I've created a function wich creates (concats) my query.
The result is similar to:
SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (@valueABC, @valueDEF)'
This query is the value of a varchar.
I want to execute this query, so I thought to use: EXEC(@myquery)
The problem is that SQL can't find the variables @valueABC or @valueDEF if thery're inside a nvarchar.
Is there some particular syntax in order to replace the value of variables?
I hope it's clear!!
Thank you in advance!!!!!
FedericaYou need to create an entire SQL string without the variables.
SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (''' + @valueABC + ''', ''' + @valueDEF + ''')'
Please, please don't post such bad solutions!
If you use dynamic SQL, you should use a parameterised statement:
SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (@valueABC, @valueDEF)'
EXEC sp_executesql @myquery, N'@valueABC int, @valueDEF int',
@valueABC, @valueDEF
However, I would prefer not to use dynamic SQL at all, but do:
INSERT tbl(keycol, col1, col2, col3, ....)
VALUES (@keyval,
CASE @coltoinsert WHEN 'col1' THEN @val END,
CASE @coltoinsert WHEN 'col2' THEN @val END,
Although the entire idea of not knowing the columns before sounds spooky to me. A column in a table is supposed to model a unique attribute.
Erland Sommarskog, SQL Server MVP, [email protected]
sp_Executesql worked for me, than you very much!!! -
Using a variable's definition as a variable name
I have a string variable with definition I need to use as a
variable name.
For example, this is super simplified:
var newVariable = 'it is working';
var partOne:String = 'new';
var partTwo:String = 'Variable';
var partThree = partOne + partTwo;
trace("my variable is " + partThree);
Instead of tracing partThree as literally partOne + partTwo
(which traces "newVariable") i'd like it to trace the
Definition of partOne + partTwo (which is "it is working").
I hope this makes sense, help or advice is
appreciated....
var partThree = this[partOne + partTwo];
trace("my variable is " + partThree); //will trace: my
variable is it is working
TS -
Servicegen - Issue with Variable Names - WLS 8.1
Hi Experts
I am trying to expose an EJB as a Webservice.. I assembled the ear using SERVICEGEN exposed by Weblogic 8.1
To my dismay the generated WSDL (even the web-services.xml) are not having the variable names which I gave in my webservice method. Instead I get String, String0, String1 and so on...
Kindly help me get over this problem.
Thanks and Regards,
Gopal.your application miight not be using this libraries but there are many shared libraries which are configured could be using. please check whether config.xml file of your domain contains the entries for this library.Problem running simple portal tutorial question
-
How can I convert the variable expression stored as string back to variable expression
How can I convert the variable expression stored as string back to variable expression?
I am storing the expression enterd in the TSExpresssionEditControl as simple string and want to convert back to expression since I want to get the data type of that expression.pritam,
I'm not sure what you're trying to do exactly. If you are trying to get the value of a variable and you only have the name of value in a string, then you can use Evaluate() to get its value. If you want the data type, my advise is to use the GetPropertyObject() API method and just pass in the loop up string. Then you'll have a handle to the data object and then proceed from there.
Regards,
Song D
Application Engineer
National Instrument
Regards,
Song Du
Systems Software
National Instruments R&D -
Returning strings of the same name in a text file from an array
Hi,
Another quick question. I have a text file with lines consisting of one integer, and four strings. Currently, I am able to search through this file and find any of the variables listed within. However, if any of the variables are listed more than once, and I search for that particular varable name, it only prints out the first line containing that particular variable name.
Basically, how do I modify the following code so that the search method returns all the instances of a particular variable name in the text file?
My current code is as follows,
1. Search method,
public Title lookUpDirector(String requiredDetails) {
boolean found = false;
int next = 0;
while (!found && (next<TitleCount))
if ( TitleArray[next].getDirector().equals(requiredDetails))
found = true;
else next++;
if (found) return TitleArray[next];
else return null;
}2. Test main class
import simplejava.*;
public class SearchTitleDirector
public static void main(String[] args) {
String titleDetails;
SimpleReader keyboard = new SimpleReader();
SimpleWriter screen = new SimpleWriter();
SimpleReader inFile = new SimpleReader("title_test.txt");
TitleList title = new TitleList(inFile);
TitleList[] sortedArray = new TitleList[]{title};
titleDetails=keyboard.readLine("Director?");
Title search = title.lookUpDirector(titleDetails);
if (search == null)
screen.println("Not found");
else screen.println(search);
}Any help or advice appreciated!Thanks for the reply and advice.
I have tried implementing the lookUpDirector method as follows,
public TitleList lookUpDirector(String requiredDetails) {
boolean found = false;
int next = 0;
TitleList list = new TitleList();
while (!found && next<titleCount) {
if ( titleArray[next].getDirector().equals(requiredDetails)) {
list.add(titleArray[next]);
next++;
if (found) return list;
else return null;
}When I use this method with the previous test main class, it compiles and runs, but after the user inputs the name of the director to search for, the input window freezes indefinitely and no result is achieved. The output pane in NetBeans is as follows,
init:
deps-jar:
compile-single:
run-single:
Director?The only way to stop the process is to terminate it in the Runtime section of NetBeans.
I have no idea why this is happening. Any advice?
Maybe you are looking for
-
How to use the Wire-Tag in Cairngorm 3 Observer Library?
Dear Observer-Lib coders, Maybe I am posting my issue into the wrong forum, see this thread: http://forums.adobe.com/thread/756046 I'd like to know how to use the Wire-Tag mentioned in the Observer-Lib docu, see: http://sourceforge.net/adobe/cairngor
-
Display ending time in week view?
Is there any way to show the ending time for each event in week view (or anywhere?) i.e. 16:30-19:30 instead of just 16:30? I want to be able to see at a glance when my different shifts end, and at the moment I have to ad the ending time to the event
-
Hi, I am trying to synchronize two video clips, one which has two different audio tracks (CH1 on board, CH2 tie clip) and a second camera with an on board mic). So far FCPX does not synchronize these correctly. I have tried setting in points first. I
-
Macbook Air 13inch for Graphic / Web Design ?
Hi guys, I decided to come here to ask for some expert help if i should buy the macbook air or pro. But before that, let me tell you what I usually do. I use Adobe Fireworks as my primary design tool for web graphics / screen graphics, maybe use Phot
-
Hi All, How to make checklist in XI for project point of view. Please send me sample checklist. regards sai