result of a dynamic select statement in variable
I have a function that generates a dynamic sql statement. I would then take the result of this statement and insert it into a variable. I tried thisexecute immediate strSQL USING OUT intCounter;
but that gives me an error on this line of code. SQL is a count (*) select and works very well. It's just how to get it in the variable which is tripping me.Thank you
Eva
good error ;)
Delete the final semicolon.
Of
..... = '||p_SiteID||');';
do
..... = '||p_SiteID||')';
Tags: Database
Similar Questions
-
How can I dynamically select the shared Variable API programming data type?
I am trying to create a configuration of open connections of variable shared using the programming API. It seems to me that the cleanest way to do would be to put one "open and check" routine in a loop, then call it for each variable in the library.
The question that I am running is that I have different types of data in my library (to help a server Modbus i/o and data types 'boolean' and 'single' in my library.) How can I dynamically select the data type of the shared variable API?
See the attached snipit.
Thank you
What I ended up doing was doing a Subvi to open, read, write, and close each data type, I use the packaged in 4 polymorphic SubVIs (polymorphic Open, read, etc...)
Now all I have to do I drop in the polymorphic Subvi and it switches automatically to the appropriate data type
-
Is it possible to create a dynamic query who will be given a list of column names, table name and where clause? He must then collect in a something like a pl/sql table where I can then count the total number of records and treat them as needed?
Published by: user10868996 on June 16, 2009 10:43Hello
Maybe it helps,
SQL> Declare 2 v_String Varchar2(4000); 3 v_Empno dbms_utility.uncl_array; 4 v_Nro Number; 5 v_cur Sys_Refcursor; 6 v_name Varchar2(30); 7 Begin 8 SELECT Empno Bulk Collect 9 INTO v_Empno 10 FROM Emp 11 WHERE deptno IN (20, 30); 12 13 dbms_utility.table_to_comma( v_Empno 14 , v_Nro 15 , v_String ); 16 17 dbms_output.put_line(v_String); 18 19 Open v_cur For 20 SELECT ename 21 FROM EMP 22 WHERE instr(',' || v_String || ',' , ',' || empno || ',') > 0; 23 Loop 24 Fetch v_cur 25 INTO v_name; 26 Exit When v_cur%Notfound; 27 dbms_output.put_line(v_name); 28 End Loop; 29 End; 30 /
Kind regards
Christian Balz -
Helps with the syntax of dynamic sql statements
Hi all
How can I pass the value of the result of my dynamic select statement to a Ref Cursor?
I try the following statement but does not work, I get this error:
ORA-00932: inconsistent data types: expected - was - ORA-06512: at
Any ideas?Procedure getItems(v_first In number, v_second In Number, arg_Cursor IN OUT CUSTOM_REF_CURSOR) is sqlString varchar(3000); Begin sqlString := 'select* from bla where o.arg_1 = :1 and arg_2 = :2'; execute immediate sqlString into arg_Cursor using v_first, v_second; End getItems;
Thank you
JohnnyPROCEDURE getitems (v_first IN NUMBER, v_second IN NUMBER, arg_cursor IN OUT sys_refcursor) IS sqlstring VARCHAR (3000); BEGIN sqlstring := 'select* from bla where o.arg_1 = :1 and arg_2 = :2'; OPEN arg_cursor FOR sqlstring USING v_first, v_second; END getitems;
-
How to add an imaginary line in the result of the value of a SELECT statement.
Hello all-
I have requirment to add an imaginary line in the result set of a SELECT statement.
For example lets say it is a pay table having the following columns:
Number of Payment_id
status varchar2 (10)
number amount
date of payment_date
so here's the data: -.
Payment_id status amount payment_date
applied 1 100 12/07/2008
2 Reversed 200 01 / 06/2009
Applied 3 300 01 / 07/2009
Here is my SQL
Select * form payment where payment_date > = 01/01/2009
The output will be
2 Reversed 200 01 / 06/2009
Applied 3 300 01 / 07/2009
My desired output is less than
2 Reversed 200 01 / 06/2009
Applied 3 300 01 / 07/2009
reversed-200 2, 01 / 06/2009-(Dummy Row)
Third line here is the imaginary line that I add when the status is "reversed".
I would be very grateful for any help on this...
Thank you
GerardCartesion join against a shadow table is a useful method to create a fictitious line:
with my_tab as (select 1 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all select 1 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all select 1 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all select 2 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all select 2 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/05/2009', 'mm/dd/yyyy') payment_date from dual union all select 2 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all select 2 cust_id, 4 Payment_id, 'Reversed' Status, -400 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all select 2 cust_id, 5 Payment_id, 'Applied' Status, 500 Amount, to_date('01/07/2009', 'mm/dd/yyyy') payment_date from dual), --- end of mimicking your table dummy as (select 'Reversed' col1, 1 rn from dual union all select 'Reversed' col1, 2 rn from dual) select mt.cust_id, mt.payment_id, mt.status, decode(dummy.rn, 2, -1*mt.amount, mt.amount) amount, mt.payment_date from my_tab mt, dummy where mt.status = dummy.col1 (+) order by mt.cust_id, mt.payment_id, dummy.rn nulls first; CUST_ID PAYMENT_ID STATUS AMOUNT PAYMENT_DATE 1 1 Applied 100 07/12/2008 1 2 Reversed 200 06/01/2009 1 2 Reversed -200 06/01/2009 1 3 Applied 300 06/01/2009 2 1 Applied 100 07/12/2008 2 2 Reversed 200 05/01/2009 2 2 Reversed -200 05/01/2009 2 3 Applied 300 06/01/2009 2 4 Reversed -400 06/01/2009 2 4 Reversed 400 06/01/2009 2 5 Applied 500 07/01/2009
Published by: Boneist on January 7, 2009 23:10
NB. You may need to mess around with the order if that is not in the order you want. You did not mention what the rules were for all orders expected, so I composed my own * {;-)Also, I added a (cust_id) identifier to differentiate different types of payments, as is usually the case. Remove unless it is not applicable to your case.
-
Hello community, I have a problem when creating a function. The purpose of this function is to check the table of weather gave yesterday or not. We must check this on different tables on different sachems. We are creating a function with input variables.
CREATE OR REPLACE FUNCTION IN_SCHEMA.IS_YDAYDATA_TO_TABLE
(
in_schema IN VARCHAR2,
in_tablename IN VARCHAR2,
in_datefield IN VARCHAR2,
)
RETURNS INTEGER
AS
-Declaring variables
v_is_true INTEGER.
BEGIN
SELECT
CASE
WHEN MAX (in_datefield) = TRUNC(SYSDATE-1)
THEN 1
ON THE OTHER
0
END
IN
v_is_true
Of
in_schema.in_tablename
;
RETURN v_is_true;
END;
/
When creating, I got error: [error] ORA-00942 (44:19): PL/SQL: ORA-00942: table or view does not exist
How to use the values of the input variables on the table select statement names?
Hello
Here's a way you can use dynamic SQL statements for this task:
CREATE OR REPLACE FUNCTION IS_YDAYDATA_TO_TABLE
(
in_schema IN VARCHAR2,
in_tablename IN VARCHAR2,
in_datefield IN VARCHAR2,
in_first_date DATE DEFAULT SYSDATE - 1,.
in_last_date DATE by DEFAULT NULL
)
RETURNS INTEGER
AS
-IS_YDAYDATA_TO_TABLE returns 1 if in_schema.in_tablename.in_datefield
-contains all the dates in the in_first_date of the range through included in_last_date
- and it returns 0 if there is no such lines.
-If in_last_date is omitted, the search only the data on in_first_date.
-If in_first_date is omitted, it defaults to yesterday.
-Time parts of the in_first_date and in_last_date are ignored.
-Declaring variables
sql_txt VARCHAR2 (1000);
v_is_true INTEGER.
BEGIN
sql_txt: = 'SELECT COUNT (*).
|| 'FROM ' | in_schema | '.' || in_tablename
|| 'WHERE ' | in_datefield | ' > =: d1'
|| «AND» | in_datefield | '< > >
|| 'AND ROWNUM = 1';
dbms_output.put_line (sql_txt |) '= sql_txt in IS_YDAYDATA_TO_TABLE"); -For debugging
Sql_txt EXECUTE IMMEDIATE
IN v_is_true
With the HELP of TRUNC (in_first_date) - d1
TRUNC (NVL (in_last_date
in_first_date
)
) + 1 -- d2
;
RETURN v_is_true;
END is_ydaydata_to_table;
/
DISPLAY ERRORS
If you must use dynamic SQL statements, put all the SQL statement in a single string variable, such as sql_txt in the example above. In this way, you can easily see exactly what will be executed. Comment out the call to dbms_output under test is completed.
Try to write functions that will address not only the question that you have now, but similar questions that you may have in the future. For example, now that interest you only to the verification of the data of yesterday, but later, you might want to check another day or range of days. The above function combines the convenience of a function simple (looks like yesterday data if you don't tell him otherwise) with the power of a more complex function (you can use the same function to check any day or range of days).
-
How to use the variable instead of the table name in the select statement of procedure
I have a procedure with a select statement with dynamic from clause. How can I use instead of the name of the table variable.
create or replace procedure scc_chemical_analysis
is
v_table_name varchar2 (100);
Start
declare
cursor c1 is select * from v_table_name; -This is the variable name. How can I use it in the select statement.
.
.
.
.
end;Hello
something like that.
1 declare 2 vSQLString VARCHAR2(32000); 3 pTableName varchar2(1000):='EMP'; 4 BEGIN 5 vSQLSTRING := 6 'SELECT ''x'' FROM ' || DBMS_ASSERT.sql_object_name(pTableName) || ' WHERE rownum<=1 '; 7 EXECUTE IMMEDIATE vSQLSTRING; 8* END; SQL> / PL/SQL procedure successfully completed.
-
using variable in the select statement (php)
I have trouble using a variable in a select statement.
The following query to manually (using a static date) works very well:
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > ' 2006-06-01' ';"
However, if I use the following:
$dateVar = date('Y-m-d');
@mysql_select_db ($database_mw, $mw);
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > $dateVar";
All records are returned, which means that the $dateVar variable is not recognized. I compared the values of $dateVar <? PHP echo $datetime;? > against the real value of my date field <? PHP echo $row_Recordset1 ["classDate"];? > in my results to the table and it seems that the values are indeed accurate regarding a date 2006-06-04 for example.
My date field 'classDate' is of type 'date' in the mysql database. I use Dreamweaver MX 2004 with Mac OSX 10.3.9.
I'm sure it's just a syntax problem, at least, I hope it is.
Any help is greatly appreciated.
It worked:
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > = CURRENT_DATE;
Thank you
MW -
Assign the values of the two variables in a select statement
I'm trying to set two variables in a select statement in my definition of the procedure. I get the error identifier not valid "ATTRIBUTE" running the following code.
Can I not connect two different values to two different variables in the same select?create or replace procedure tbl_auth ( Email in varchar2, UserLevel out number) as v_Email varchar2(100) := lower(Email); v_login_chk number := 0; begin select UserLevel into p_UserLevel, v_login_chk into v_login_chk from tbl_User where eMail=v_Email exception when no_data_found then UserLevel := 0; end tbl_auth;
Thank you
Published by: jerry8989 on August 22, 2011 07:06
Published by: jerry8989 on August 22, 2011 07:06change as follows:
select UserLevel, v_login_chk into p_UserLevel, v_login_chk
IN ONE ALONE is enough.
BTW, don't you have "login Check" column in the "tbl_user"? If you do, your selection should be like this:
... select UserLevel, loginChk into /* check the column name in your table */ p_UserLevel, v_login_chk from tbl_User ...
-
Set the Variable in the select statement
Hello
Can we define a variable in the select statement.
Example:
Select firstname, lastname
employees;
in this simple select statement we can define x as number?select fname,lname from employees where rownum <= 30;
-
Set a variable to the result of an instruction select
I create a list view of the numbers that I have in the system. I'm doing a table with only a list of numbers from 1 to whatever number max is in view. IIf 89 324 is so the last number in the system, so I want a table with 1 thru 89324. Then I can use the NOT EXIST to determine what numbers are miissing in the system. so, if 1 to 200 are not used, but by 89 324 201 is used, then I want the output to 1,2,3... 200.
The code to make the display works and the code to create the table is good, the loop works when I use a number defined for example 100. I just have problems affecting a variable in the number max.
Here's what I have (which of course does not)
Published by: james3302 on August 11, 2010 15:59CREATE OR REPLACE PROCEDURE test IS n number := 1; maxNum number; BEGIN execute immediate 'create view check_numbers as select num from table1 union select num from table2 union select num from table3'; maxNum := EXECUTE IMMEDIATE 'select max(num) from check_numbers'; --I also tried taking out the EXECUTE IMMEDATE here and it still did not work WHILE n <= maxIpid LOOP insert into numList values(n); n := n + 1; END LOOP; --Here I will put in the code to compare the two and display the numbers that are in numList but not in Check_numbers END; /
James,
The solution to your immediate problem to get the result of the immediate execution in a variable is to use
EXECUTE IMMEDIATE 'select max(num) from check_numbers' into maxNum;
You can also do without running to the immediate assistance
select max(num) into maxNum from check_numbers;
Of course, you should definitely consider using one of the other solutions already provided.
Depending on what you ultimately trying to reach, another option to consider is to get the list of discrepancies between the numbers to help
Method of Tabibitosan of Aketi ({: identifier of the thread = 1005478}).with t as ( select 1 as n from dual union all select 2 as n from dual union all select 7 as n from dual union all select 8 as n from dual union all select 11 as n from dual ) select * from ( select grp, n1, n2, 1+lag(n2,1,0) over(order by n1) as gap_n1, n1-1 as gap_n2 from ( select grp, min(n) as n1, max(n) as n2 from ( select n, n - row_number() over(order by n) as grp from t ) group by grp ) ) where grp>0 order by grp ; GRP N1 N2 GAP_N1 GAP_N2 ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- 4 7 8 3 6 6 11 11 9 10
Kind regards
BobPublished by: BobLilly on August 11, 2010 15:00
-
How to insert the value of the variable using select statement - function Oracle
Hello
I have a function that inserts the record based on a condition
INSERT IN the case
(
Case_ID,
Case_Status,
Closure_Code,
Closure_Date
)(
SELECT newCaseID,
caseStatus,
Closure_Code,
Closure_Date,
CASE
WHERE Case_ID = caseID
);
Now, I want the new value casestatus instead of value caseStatus select statement. I have a m_caseStatus variable and I want to use the value of this variable in the select statement above.
How can I use this.
Thank youINSERT INTO Case ( Case_ID, Case_Status, Closure_Code, Closure_Date )( SELECT newCaseID, m_caseStatus, <-- here place your variable Closure_Code, Closure_Date, FROM Case WHERE Case_ID = caseID );
Nicolas.
-
How to loop through the results of a select statement in a process flow?
Hello
I use Warehouse Builder 10 g R2.
I'm trying to implement a loop in a process stream that runs through the results of a select statement.
In pl/sql, it would be implemented as a for loop by using a slider, but I simply doesn't work how to do this using the loops in the process flow.
Someone at - it ideas?
Thank you
LiffeyLiffey,
Look at this thread
For or while loop a loop in the process flowKind regards
Oleg -
Pivot will not get result in the select statement
This is the version of oracle 11g R2 on windows details 7 OS
CREATE TABLE OTBOOKINGDETAILS
(EMPNO VARCHAR2 (10 BYTE),
DATE OF OTBOOKEDDATE,
NUMBER (5.0) DAY OF THE WEEK.
DAYCEILINGHRS VARCHAR2 (10 BYTE));
Here is the data
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 MARCH 2013', 1,' 09:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 APRIL 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 MAY 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 JUNE 2013', 4' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 JULY 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 AUGUST 2013', 6,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 SEPTEMBER 2013', 7,' 00:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 MARCH 2013', 1,' 09:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 APRIL 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 MAY 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 JUNE 2013', 4,' 02:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 JULY 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 AUGUST 2013', 6,' 04:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 SEPTEMBER 2013', 7' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 OCTOBER 2013', 1,' 00:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 NOVEMBER 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 DECEMBER 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 13 MARCH 2013', 4,' 04:45 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 14 MARCH 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 15 MARCH 2013', 6,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 16 MARCH 2013', 7' 01:00 ');
Here, I want to Dayceiling hours below the output using pivot
EMPNO OTBOOKEDDATE DAY1_HRS DAY2_HRS DAY3_HRS DAY4_HRS DAY5_HRS DAY6_HRS DAY7_HRS
101 03/03/2013 09:00 01:00 01:00 01:00 01:00 01:00 00:00
102 03/03/2013 09:00 01:00 01:00 02:00 01:00 04:00 01:00
101 03/10/2013 00:00 01:00 01:00 04:45 01:00 01:00 01:00ChakravarthyDBA wrote:
I need to include the pivot query in the select statementCan' are you use PIVOT here since you do not know from the start what values OTBOOKEDDATE. So use:
select empno, otbookeddate - weekday + 1 otbookeddate, max( case weekday when 1 then dayceilinghrs end ) day1_hrs, max( case weekday when 2 then dayceilinghrs end ) day2_hrs, max( case weekday when 3 then dayceilinghrs end ) day3_hrs, max( case weekday when 4 then dayceilinghrs end ) day4_hrs, max( case weekday when 5 then dayceilinghrs end ) day5_hrs, max( case weekday when 6 then dayceilinghrs end ) day6_hrs, max( case weekday when 7 then dayceilinghrs end ) day7_hrs from otbookingdetails group by empno, otbookeddate - weekday + 1 order by otbookeddate, empno / EMPNO OTBOOKEDDA DAY1_HRS DAY2_HRS DAY3_HRS DAY4_HRS DAY5_HRS DAY6_HRS DAY7_HRS ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 101 03-03-2013 09:00 01:00 01:00 01:00 01:00 01:00 00:00 102 03-03-2013 09:00 01:00 01:00 02:00 01:00 04:00 01:00 101 10-03-2013 00:00 01:00 01:00 04:45 01:00 01:00 01:00 SQL>
SY.
-
How to get a select statement to pass a variable as the OUT parameter in PROC
I have the underside of the nested select statement that returns the name of the missing table (data_table_name).
How to be an effective way to modify the select statement, so that the returned 'data_table_name' is passed as OUTPUT in a procedure parameter
Any help is greatly appreciated.select 'Missing Table Name : ' || data_table_name from ( select upper(data_table_name) data_table_name from TABLE1 where data_table_name in ('ABC',EFG','XYZ') MINUS ( select upper(fus_data_table_name) from TABLE2 where fus_data_table_name in ('EFG',') group by upper(fus_data_table_name) union select upper(fus_data_table_name) from TABLE2 where fus_data_table_name = 'LMN' group by upper(fus_data_table_name) ));
SCREATE OR REPLACE PROCEDURE myproc (tname OUT NOCOPY VARCHAR2) AUTHID CURRENT_USER IS BEGIN SELECT something INTO tname FROM whatever; EXCEPTION WHEN OTHERS THEN tname :=
; END myproc; /
Maybe you are looking for
-
I can't get my itunes gift I have sent to me by e-mail. Any clue?
I received an email with my gift, but when I click "use now" he send me to http://www.apple.com/itunes/download/ but I have itunes on my pc. I also received my billing that prooves that it worked. Any clue?
-
Mail question: remove all the e-mail folder
Hello. It is as much a matter of Gmail as a matter of Mail, but I tried to find an answer online with no luck. In Mail, is it possible to disable the all mail folder that generates Gmail? The problem with this file, is it seems to keep emails after I
-
When I try to compare it to similar .seq file using diff sequence I get an error: Index out of range Error code:-17301 Is it because the sequence files are not quite similar? The sequences are similar, but there are different menus and the sequence o
-
Display of RDP - Surface vs iPad
I am considering buying your Surface for my law firm - which uses a complete environment of Ms. One of the applications we use, Profiles(a SQL-based, case management software) Client are available on the race with a RDP session. We have tried to use
-
Pavilion A6123IN desktop - missing Windows Serial Key
Dear Sir. We have been in HP computers desktop and laptops in our office. We have a HP Pavilion A6123IN desktop computer that shipped with the version of Windows Vista Edition family of base. His hard drive broke down, and we found that the Microsoft