Assign Variable in Loop
Hi everybody,
I'm writting a procedure and I want to assign to some variable, a value taken fron a cursor. I have 20 variables , and it could be more, that's why I want to do that within a loop.
I tried the following:
DECLARE CURSOR retrieve_salary IS
(SELECT ID, SALARY FROM TABLE ORDER BY ID);
retrieve_salary_row retrieve_salary%ROWTYPE
ID_1 NUMBER;
ID_2 NUMBER; .........
SALARY1 NUMBER;
SALARY2 NUMBER; .....
i NUMER;
BEGIN
OPEN retrieve_salary;
i:=1;
WHILE i < 3 --or te number of variable you have to assign
LOOP
FETCH retrieve_salary INTO retrieve_salary_row;
ID_||i := retrieve_salary_row.ID;
SALARY||i := retrieve_salary_row.salary;
i:=i+1;
END LOOP;
END;
this code doens't work because of the concatenation........is there a way to do that smartly?
Thanks in advance....
Alex
Hi,
If you want to store table data in different variables then why you are approaching to VARRAY or Assiciative Array. e.g.
"afiedt.buf" 17 lines, 399 characters
1 DECLARE
2 CURSOR emp_cur
3 IS SELECT ename,sal FROM emp WHERE rownum <=3;
4 TYPE emp_type is TABLE OF emp_cur%ROWTYPE
5 INDEX BY BINARY_INTEGER;
6 arr_var emp_type;
7 i NUMBER :=0;
8 BEGIN
9 OPEN emp_cur;
10 LOOP
11 i := i + 1;
12 FETCH emp_cur INTO arr_var(i).ename,arr_var(i).sal ;
13 EXIT WHEN emp_cur%NOTFOUND;
14 DBMS_OUTPUT.PUT_LINE('Name :'||arr_var(i).ename||' Sal :'||arr_var(i).sal);
15 END LOOP;
16* END;
SQL>/
Name :SMITH Sal :800
Name :ALLEN Sal :1600
Name :WARD Sal :1250
PL/SQL procedure successfully completed.
SQL>Regards
Avinash
Similar Messages
-
Garbage Collection facilitation by assigning variables to null
Hello,
I understand Garbage Collection (GC) can be facilitated by assigning variables to null. Is this true for all variables?
I have a class and there are some member variables which are strings. Also i have some methods with some string variables declared and defined inside the method. I am assigning null to class member variables and also the string variables of the method.
is it correct to assign null and faciliate GC for the string variables declared and defined inside the method?
Class A {
String t1;
public void test() {
String t2 = "test";
// processing
t2 = null; // Will this facilitate GC?
public static void main (String[] args) {
test();
A obj = new A();
A.t1 = "testclass";
A.t1 = null; // Will this faciliate GC?
Thanks, AravinthIn fact, the java heap is divided into a space for new allocations, and two survivor spaces. The algorithm for when collection occurs in each space is complex, and tuning GC is an interesting topic all its own. Follow the best advice you have been given ... chillax. The JVM has GC under control.
<think/> Of course, having said this, you can have memory leaks if you create references that never go out of scope, even if they are no longer used. Avoid this.
� {� -
Dynamically assigning variable in xml loop
I am trying to dynamically assign a variable name so I can
build an accordian nav from and XML doc. the code looks like this:
ACnav['depthChild0']._alpha = 0;
ACnav.setStyle("color", 0x0ffffff);
ACnav.setStyle ("openEasing",
mx.transitions.easing.strong.easeout);
ACnav.setStyle ("selectionDuration",
mx.transitions.duration.slow);
navXML = new XML();
navXML.ignoreWhite = true;
navXML.load("menu1.xml");
oY=5;
var currentSection = 0;
function buildNav () {
DisposableXML = new XML();
TempXML = new XML();
button = new Array();
//buttonNum = new Array();
subitem = new Array();
menuItem = new Array();
o = new Array ();
subitemlocation = new Array();
DisposableList = new Array();
buttonList = navXML.firstChild.childNodes;
i = 0;
sectionLength = buttonList.length;
while (i<=buttonList.length) {
if (buttonList
.nodeName.toLowerCase() == "button") {
DisposableXML = buttonList;
DisposableList = DisposableXML.childNodes;
buttonName = buttonList
.attributes.name;
set ("buttonNum", "buttonNumber"+i);
trace(buttonNum);
ii = 0;
//trace(buttonName+"-"+buttonNum);
//ACnav.createChild("View", buttonNum, {label: buttonName,
icon: "mainNav"});
button = ACnav.createChild("View", buttonNum, {label:
buttonName, icon: "mainNav"});
oY=5
while (ii<=DisposableList.length) {
TempXML = DisposableList[ii];
if (DisposableList[ii].nodeName.toLowerCase() == "subitem")
subitem = TempXML.attributes.name;
//subitemlocation = TempXML.attributes.location;
subitemNum="subitemNum"+[ii]+
//trace(buttonNum+"-"+subitemNum+"-"+subitem);
menuItem = ACnav.buttonNum.createChild("subNav",
subitemNum, {childText: subitem});
menuItem
.move(0, oY);
oY=(oY+25);
//trace(buttonNum);
ii = Number(ii)+1;
i = Number(i)+1;
//trace(button);
trace(menuItem);
redraw();
//gotoAndPlay('reload');
The problem lies with this line: button =
ACnav.createChild("View", buttonNum, {label: buttonName, icon:
"mainNav"});
If I quote the "buttonNum", it works, but jumbles the subnav
items together vs in the proper node. I need to have that declared
dynamically so that each parent node has a differentID and then
createchild goes under that. Anyway, I've tried alot of stuff over
the last few days and its getting frustrating.
Thanks for the help if anyone contributes!You shouldn't need to do a concat within the XQuery path parameter. Try the following:
bpws:getVariableData('inputVariable','payload','/ns1:Request/ns1:instance[bpws:getVariableData("j")]') -
Array loop - assigning variables
Hi All,
This is similar to a question I previously had, (which kglad so kindly helped me with) so please bare with me.
Here is the code:
nor = Array(a, b, c);
aVar = Array();
aVar[0] = 0;
aVar[1] = 0;
aVar[2] = 0;
for (i=0; i<nor.length; i++)
nor[i].onRelease = function() {
nor[i].ivar=i;
aVar[this.ivar] = 1;
trace(aVar[this.ivar]); // this outputs "1" //
for (i=0; i<nor.length; i++)
trace(aVar[i]); // this outputs "0, 0, 0" //
The problem is that when tracing the "aVar" array, one of them should be "1" (so I think)
Any Ideas?
Thank youahhhh... of course.
Thanks kglad. I really appreciate your help.
*I did revise the code slightly for future/others use.
nor = Array(a, b, c);
aVar = Array();
aVar[0] = 0;
aVar[1] = 0;
aVar[2] = 0;
for (i=0; i<nor.length; i++)
nor[i].ivar=i;
for (i=0; i<nor.length; i++)
nor[i].onRelease = function() {
aVar[this.ivar] = 1;
trace(aVar[this.ivar]); // this outputs "1" //
for (i=0; i<nor.length; i++)
trace(aVar[i]); // this outputs "0, 0, 0" // -
Spurious lint warning about assigned variable never used
I'm getting a lint warning "assigned value never used" (and another one "set but not used in function")
that seems to be spurious. Test case included below. Can someone either confirm that this is a bug or explain to me why
this warning is legitimate.
marten 229% cc -V
cc: Sun C 5.9 SunOS_sparc Patch 124867-01 2007/07/12
marten 230% uname -a
SunOS marten.isi.edu 5.10 Generic_127111-11 sun4u sparc SUNW,Sun-Blade-1000 Solaris
marten 231% cat test93.c
static long value;
long get_value (void) {
value++;
return value;
int main (int argc, char ** argv) {
long k, val;
value = 0;
k = 0;
while ((val = get_value()) != 47) {
k++;
return 0;
marten 249% lint -u -m -x -Ncheck=%all test93.c
argument unused in function
(9) argc in main
(9) argv in main
set but not used in function
(10) val in main
.c file has no corresponding .h file
/nfs/m_usr/dhollen/junk/test93.c
assigned value never used
val defined at test93.c(10) :: set at test93.c(14)The value of variable "val" is never actually used in function main.
You could delete its declaration and the assignment to it in the while-loop header without affecting the program.
Maybe you are thinking that the line
while ((val = get_value()) != 47)
uses the value of val. An assignment-expression has the type and value that is stored in the left operand, and is not an lvalue. The actual storage into the variable is a side effect that need not occur before the value of the assignment is used.
Reference: C99 Standard, section 6.5.16 "Assignment operators", paragraph 3.
In this example, the storage into val must occur before entering or skipping the loop body, but need not occur before evaluating the control expression. Variable val is therefore not used in the function.
We can go further and notice that since storing into val is not an "observable side effect", a compiler is free to omit allocating the variable or storing into it. -
Declaring variables in loops.
I am wondering about how the compiler deals with variables that are declared within the loop they appear in. Obviously if you have a:
while (loop == true); {
int var;
// do something that involeves var...The suggestion is that it allocates an int with every loop iteration?
If I declare my variables outside of the context in which I use them, then they persist throughout code that doesn't need them.
Surely the compiler is smarter than that?If I declare my variables outside of the contextin
which I use them, then they persist throughoutcode
that doesn't need them.The same goes for the variables declared in a loop.No it doesn't.
If I write this:
public class test
public static void main(String[] args)
for (int x = 0; x < 255; x++) {
int y = 12;
System.out.print(x + ", ");
System.out.println("\n" + y + x);
}The compiler throws two errors because the lower println statement has never heard of either x or y.
Just declare the variables in the scope block closest
to where they are needed. You're worrying about
things that need not concern you.Oh, it does concern me though. I want my programs to be as neat as clean as possible and I also want to learn to be the best programmer I can: I'm not here to just leap on a gravy train! : )
See, I do want to format my files in the manner you suggest, with variable declared in the scope in which they used, but how do I make adjustments to them later: I want to do this from the top of my file.
If anybody has the answer I'll give them four more Duke dollars! : ) -
Assigning variable value to another variable
I want to assign value from one variable with type DATETIME to another variable which has type STRING.
E.G: I need to convert this variable P_CreatedDate of type DATETIME to STRING variable CreatedDate.
Can someone help me in C# script. Im using SSIS script task.
Thanks in advance.
ZKI have a requirement where I need to convert following datetime variable value into the string vaiable format.
Here is the example:
7/23/2014 11:24 PM
I need above datetime to be converted into following string format
2014-01-01 03:15:01.470
yyyyMMdd HHmmssFFF
Thanks in advance.
ZK -
Why assign variable doesn't work
In my test in sqlplus:
variable testa number;
select max(length(name)) into :testa from myschema.tabletest;MAX(LENGTH(NAME))
6
print :testa;TESTA
----------You have to assign the value in pl/sql:
SQL> begin
2 select 5 into :testa from dual;
3 end;
4 /
Procedura PL/SQL completata correttamente.
SQL> print testa
TESTA
5Max
http://oracleitalia.wordpress.com -
SQL query returns value but EXEC SQL Task fails to assign variable
I have an execute SQL task to get an identifier from an Oracle database and assign it to a variable (the variable is used in subsequent query's where clauses to transfer the data to a SQL Server database). If I copy the query from the SSIS
package to the Oracle SQL Developer interface and run the query; I get the expected single identifier. If I run the execute SQL task I get the error "[Execute SQL Task] Error: An error occurred while assigning a value to variable "VariableName":
"Single Row result set is specified, but no rows were returned."
I can create phony/simpler versions of the query that will return the wrong identifier that will successfully assign the value to the variable, so the problem isn't with the variable itself or the way the result set is set up in the task (Result
Set/Single Row in the General panel; Result Name 0/VariableName in the Result Set panel).
How can two queries that return a single value produce different results with respect to the variable assignment process?Perhaps you can trap (profile) the query in Oracle to see what it gets submitted,
thing is this query
select id from table where ProcessedStatusColumn is null and DateColumn = (sub select to get most recent date in table where ProcessedStatusColumn is null)
is not guaranteed to return a value, you need a default returned in case the search failed e.g.
select
NVL(id, 0)
from table where ProcessedStatusColumn is null and DateColumn =
(sub select to get most recent date in table where
ProcessedStatusColumn is null)
Arthur My Blog -
Dynamically assigning variables based on condition in pl/sql
I have a script which takes 6 input parameters but based on input 1 parameters 2 to 6 will be assigned to different variables. but the condition is not being checked during variable assignment phase. even if table_update is 'P' para,mater 2 is getting assigned to tariff_group instead of offer_id. Is there any way to solve this problem so that variables get assigned based on condition.
table_update := '&1';
if(table_update = 'T')
THEN
tariff_group := '&2';
gf_version := &3;
tariff_table_name := '&4';
flag := '&5';
if(no_rows_tariff(tariff_table_name, gf_version, tariff_group))
THEN
if(flag = 'I')
THEN
tariff_column_name := column_tariff(tariff_table_name);
ELSIF(flag = 'R')
THEN
max_gf_tariff(tariff_table_name, gf_version, tariff_group);
ELSE
DBMS_OUTPUT.PUT_LINE('Please enter correct option for update I- Insert R-Rollback');
END IF;
END IF;
ELSIF(table_update = 'P')
THEN
offer_id := &2;
gf_version := &3;
promotion_table_name := '&4';
flag := '&5';Although you do not say what your problem is, I suspect that it si something like this:
SQL> !cat t.sql
DECLARE
table_update VARCHAR2(1);
tariff_group VARCHAR2(3);
gf_version NUMBER;
tariff_table_name VARCHAR2(5);
offer_id NUMBER;
promotion_table_name VARCHAR2(5);
flag VARCHAR2(1);
BEGIN
table_update := '&1';
IF table_update = 'T' THEN
tariff_group := '&2';
gf_version := &3;
tariff_table_name := '&4';
flag := '&5';
ELSIF table_update = 'P' THEN
offer_id := &2;
gf_version := &3;
promotion_table_name := '&4';
flag := '&5';
END IF;
DBMS_OUTPUT.Put_Line ('TableUpdate: '||table_update);
DBMS_OUTPUT.Put_Line ('TariffGroup: '||tariff_group);
DBMS_OUTPUT.Put_Line ('GfVersion: '||gf_version);
DBMS_OUTPUT.Put_Line ('Tarifftable: '||tariff_table_name);
DBMS_OUTPUT.Put_Line ('OfferID: '||offer_id);
DBMS_OUTPUT.Put_Line ('PromoTable: '||promotion_table_name);
END;
SQL> @t T TG1 1 TTN1 F
old 10: table_update := '&1';
new 10: table_update := 'T';
old 12: tariff_group := '&2';
new 12: tariff_group := 'TG1';
old 13: gf_version := &3;
new 13: gf_version := 1;
old 14: tariff_table_name := '&4';
new 14: tariff_table_name := 'TTN1';
old 15: flag := '&5';
new 15: flag := 'F';
old 17: offer_id := &2;
new 17: offer_id := TG1;
old 18: gf_version := &3;
new 18: gf_version := 1;
old 19: promotion_table_name := '&4';
new 19: promotion_table_name := 'TTN1';
old 20: flag := '&5';
new 20: flag := 'F';
offer_id := TG1;
ERROR at line 17:
ORA-06550: line 17, column 19:
PLS-00201: identifier 'TG1' must be declared
ORA-06550: line 17, column 7:
PL/SQL: Statement ignoredAs you can see, the initial assignments for compilation purposes are done prior to actually running the code, and the assignment to offer_id is failing.
If I change offer_id to a varchar and quote the &2, then the compilation assignemnt works, and the actual logic of the script works:
SQL> !cat t.sql
DECLARE
table_update VARCHAR2(1);
tariff_group VARCHAR2(3);
gf_version NUMBER;
tariff_table_name VARCHAR2(5);
offer_id VARCHAR2(3);
promotion_table_name VARCHAR2(5);
flag VARCHAR2(1);
BEGIN
table_update := '&1';
IF table_update = 'T' THEN
tariff_group := '&2';
gf_version := &3;
tariff_table_name := '&4';
flag := '&5';
ELSIF table_update = 'P' THEN
offer_id := '&2';
gf_version := &3;
promotion_table_name := '&4';
flag := '&5';
END IF;
DBMS_OUTPUT.Put_Line ('TableUpdate: '||table_update);
DBMS_OUTPUT.Put_Line ('TariffGroup: '||tariff_group);
DBMS_OUTPUT.Put_Line ('GfVersion: '||gf_version);
DBMS_OUTPUT.Put_Line ('Tarifftable: '||tariff_table_name);
DBMS_OUTPUT.Put_Line ('OfferID: '||offer_id);
DBMS_OUTPUT.Put_Line ('PromoTable: '||promotion_table_name);
END;
SQL> @t T TG1 1 TTN1 F
30 /
old 10: table_update := '&1';
new 10: table_update := 'T';
old 12: tariff_group := '&2';
new 12: tariff_group := 'TG1';
old 13: gf_version := &3;
new 13: gf_version := 1;
old 14: tariff_table_name := '&4';
new 14: tariff_table_name := 'TTN1';
old 15: flag := '&5';
new 15: flag := 'F';
old 17: offer_id := '&2';
new 17: offer_id := 'TG1';
old 18: gf_version := &3;
new 18: gf_version := 1;
old 19: promotion_table_name := '&4';
new 19: promotion_table_name := 'TTN1';
old 20: flag := '&5';
new 20: flag := 'F';
TableUpdate: T
TariffGroup: TG1
GfVersion: 1
Tarifftable: TTN1
OfferID:
PromoTable:John -
Creating variable via looped link
i am sending a url with a variable that is a modified series
of variables
sublnk=SECID=2*SECTSUBID=2*SECTSUB1ID=90
i replaced the "&" with "*" because it is a sub variable
in url string (i.e) sublnk=#sublnk#...? (please don't ask)
anyway when i get to the display page i need to split up and
make variables of SECID, SECTSUBID, SECTSUB1ID
I tried a list loop:
<cfloop index="catzzz" list="#sublnk#" delimiters="*">
<cfset #catzzz#>
</cfloop>
what i want is:
<cfset SECID=2>, etc.
does not work.... ????
tnx in advanceksmith,
your code worked great but i modified a little to take remove
some of the functions... see below.
one thing i don't understand is how the variable can be read
without making it dynamic with # sign as in
<cfset "#varName#" = varVal>
the variable "varVal" is dynamic.... it works but i don't get
it. please explain.
way thanks.... -
Assigning variable to the values from OUT.PRINTLN
Inside our Servlet doGet, we have the following,
out.println("<TR><TD></TD><TD><INPUT TYPE=\"text\" NAME=\"empphone\" VALUE=\"\" SIZE=\"40\" MAXLENGTH=\"50\" ></TD></TR>\n");
and we need to assign the value entered by the user to a variable.
I don't think the following is doing the trick?
String empphone1;
empphone1 = req.getHeader("empphone");
Thanks a lot
KAuse req.getParameter("empphone");
regards,
~K -
Assigning variable to a column value
Hi,
I am creating a report wherein I use a formula in one of the columns to forecast data. The values obtained in this column (as a result of application of the formula) is to be used to calculate the values for the second column.And the third column value is dependent on the second and so on....
If I use the column formula directly,then many lines of coding are involved...Is it possible to store the value of a column in a variable and hence forth use it for manipulation in subsequent columns?Helios,
I'm already having the same setup mentioned in the thread. And I'm doing exactly whats given there. But the issue seems to be something different. I'm assigning the value to the Global Variable in the Package through Apex. Here's the Package Code:
CREATE OR REPLACE PACKAGE Schema1.SPMS_SECURITY_PKG
AS
X_app_user_id NUMBER DEFAULT -1;
FUNCTION USER_ID RETURN NUMBER;
END SPMS_SECURITY_PKG;
CREATE OR REPLACE PACKAGE BODY Schema1.spms_security_pkg
AS
FUNCTION user_id
RETURN NUMBER
IS
BEGIN
-- RAISE_APPLICATION_ERROR(-20001,'USER ID'||'*'||X_app_user_id);
RETURN NVL (x_app_user_id, -1);
-- RETURN NVL (sys_context('USERENV', 'CURRENT_USER'), -1);
EXCEPTION
WHEN OTHERS
THEN
RETURN -1;
END;
And Here is the Trigger Code:
CREATE OR REPLACE TRIGGER Schema1."USER_DETAILS_TRIGGER"
BEFORE INSERT OR UPDATE
ON PMS_SICAL.SPMS_USER_DETAILS REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF INSERTING
THEN
:NEW.created_by := spms_security_pkg.user_id;
:NEW.created_date := SYSDATE;
:NEW.START_DATE := SYSDATE;
ELSIF UPDATING
THEN
:NEW.updated_by := SPMS_SECURITY_PKG.X_app_user_id;--spms_security_pkg.user_id;
:NEW.updated_date := SYSDATE;
END IF;
END;
But I always get -1 in the both in the table after the DMLs. -
What is the difference:
if I write
public class Test
static final String ImeDat="joga1.gif";
public static void main (String args[]) throws Exception
try {
System.out.print("Connecting to the Database. Please wait..");
//Load the Oracle JDBC Driver and register it.
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database:
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");....
I get NO ERROR message during compiling
BUT
public class Test
static final Connection conn;
static final String ImeDat="joga1.gif";
public static void main (String args[]) throws Exception
try {
System.out.print("Connecting to the Database. Please wait..");
//Load the Oracle JDBC Driver and register it.
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database:
conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");...
I always get
C:\xsu>javac Test.java
Test.java:41: Can't make a static reference to nonstatic variable conn in class
Test.
conn =
^
1 error
C:\xsu>javac Test.java
Test.java:17: Blank final variable 'conn' may not have been initialized. It must
be assigned a value in an initializer, or in every constructor.
public class Test
^
Test.java:41: Can't assign a second value to a blank final variable: conn
conn =
^
2 errors
nullIn the inspector for your "on" button, set the audio stream to whatever stream you want. for the "off" button set it to the other stream. dont forget to assign the action you want the button to take (ie - jump to main menu:play.) You can also do this with a script as well
-
Hi all,
I'm working on a JS-script to extract the text from text layers and I was wondering if it was possible to assign a Variable from within this script or through an Action.
Basically I want to auto-assign Text Replacement variables to all of the layers in an existing image. Is this possible?
Thanks in advance!I was able to solve this without using Variables, but I would still like to know if anyone has the answer.
Maybe you are looking for
-
If i pass the parameter like "select *from split('672','asdfg')" when i execute the function in sqlserver.. it will give the following o/p: RowID value 1 asdfg I want the same o/p in oracle using function.... below i gave the sample function in s
-
Hp pavalion g6 compatable with windows 8
is my pavalion g6 series compatable with windows 8 its a 64bit system
-
Strange graphical glitch in Select Columns
I've noticed a strange graphical glitch in the Select Columns region of an interactive report when using IE7 or 8. When a column is selected (other than the first or last) and sent to the Bottom or Top the change doesn't actually show until the mouse
-
Activating and Installation help BW statistic in BI 7.0
I have installed the business content for SAP Statistics, but don't have the BI_BW_Administration role authorization. I don't know if it is possible to still go ahead with the Statistics peice. I don't see the process chains created by following the
-
Is Adobe Edge Inspect compatible with Safari?
Currently, I am only able to get Chrome working through the Google Chrome Extension. If it is compatible with Safari, how would I configure it to work? Thank you.