Behaviour of the brackets in a Select clause
Dear Readers,
I can't understand why I get 2 different results on running with a Bracket I get NULL and without a bracket I get the declared variable value which is Noname
Below is Query 1:
Declare @testvar char(20)
Set @testvar = 'noname'
Select @testvar= pub_name
FROM publishers
WHERE pub_id= '999'
Select @testvar
Out put of this query is 'Noname'
BUT when I type the same query in the following manner I get Null-------Please note that the only difference between this query below is I used brackets and Select in the Select@testvar statement
Declare @testvar char(20)
Set @testvar = 'noname'
Select @testvar=(Select pub_name
FROM publishers
WHERE pub_id= '999')
Select @testvar
Thank-you
Yes, this is plain-table valued query:
Select au_id
From Authors
WHERE 1=0
But here
Select(Select au_id
FROM authors
WHERE 1=0)
you have a query on the form
SELECT (scalar value)
The scalar value in this case is a subquery, a scalar subquery. A scalar subquery always returns a single row. If the set returned by the query is empty, the scalar return value is NULL. If the set includes more than one value, this is an error.
A different way to describe this is that the context is scalar, and this is why the subquery is scalar. All values in a SELECT list must be scalar with regards to the row returned. If you say
SELECT expr FROM tbl
And there are five rows in table, the query will return five rows. If expr is a nested query, this cannot lead to the query returning 3 or 33 rows. The query will return five rows, period.
There is also:
Select au_id
FROM (Select au_id
FROM authors
WHERE 1=0) AS k
Here the context is tabular, and in this example you will get no rows back. With a diffrent WHERE clause you can get many rows back.
Erland Sommarskog, SQL Server MVP, [email protected]
Similar Messages
-
EJB QL-What does the Object keyword in select clause do ?
I always wondered when do you need to use the Select object( ) keyword ? I read this:
[http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBQL5.html#73030]
but didn't find it very clear.
Cheers,
Michalsee
@ejbgen:entity Annotation
go to
http://edocs.bea.com/workshop/docs81/doc/en/core/index.html
see
max-beans-in-cache
Set it to 1
See also
@ejbgen:finder Annotation
- group-name
- max-elements
set it to 1 or to desired
optional:
Add to a group the desired cmp fields, if u don't want to display all the columns.
After the exec. of the findMethod u should have 1 Object, one EJB inst., in your Collection, and furthermore only the desired DB table columns -
Need to know the column names in my dynamic select clause
Dear All,
Please go through the following code. While executing the following code i am getting an error saying that dbms_sql.describe_columns overflow, col_name_len=35. Use describe_columns2.
Please guide me how to proceed further. Or please help me, how can i get the column names when i issue a dynamic select clause.
DECLARE
CUR INTEGER;
COL_CNT INTEGER ;
A INTEGER;
SEL_CLAUSE VARCHAR2(2000);
DESC_T DBMS_SQL.DESC_TAB;
REC DBMS_SQL.DESC_REC;
b number;
BEGIN
SEL_CLAUSE := 'SELECT 1,2,DECODE(1,1,''ONE'',2,''TWO'',3,''THREE'') FROM DUAL';
--'SELECT ROWID,PARA_SUB_CODE,DECODE('||''''||'ENG'||''''||','||''''||'ENG'||''''||',PARA_NAME,PARA_BL_NAME),NULL,NULL FROM PCOM_APP_PARAMETER';
--'SELECT 1,2,DECODE(1,1,''ONE'',2,''TWO'') FROM DUAL';
DBMS_OUTPUT.PUT_LINE( SEL_CLAUSE );
CUR := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(CUR,SEL_CLAUSE,DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS(CUR,COL_CNT,DESC_T);
B := desc_t.first;
FOR J IN 1..COL_CNT
LOOP
DBMS_OUTPUT.PUT_LINE('J := '||J || ' COL CNT ' || COL_CNT);
END LOOP;
BEGIN
A := DBMS_SQL.EXECUTE(CUR);
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
END;
Regards,
BalajiIs there any way can i have it directly??It does not work with static SQL either. Dynamic SQL is no different.
SQL> select 1 x from dual where x = 1 ;
select 1 x from dual where x = 1
ERROR at line 1:
ORA-00904: "X": invalid identifier
SQL>As suggested already, you will need to use alias if you want your select expression to be referred in the where clause.
SQL> BEGIN
2 FOR rec IN (SELECT *
3 FROM (SELECT 1,
4 2,
5 DECODE(1, 1, 'ONE', 2, 'TWO', 3, 'THREE') "DECODE(1, 1, 'ONE', 2, 'TWO', "
6 FROM DUAL)
7 WHERE "DECODE(1, 1, 'ONE', 2, 'TWO', " = 'ONE')
8 LOOP
9 NULL;
10 END LOOP;
11 END;
12 /
PL/SQL procedure successfully completed.
SQL>Message was edited by:
Kamal Kishore -
Merge can't accept a variable in the select clause?
oracle 10.2
I have a stored procedure.
I have a variable, vseq, which I set a sequence variable. With all other sql statements I can do
insert into table a
select vseq
from dual;
Apparently a merge can't handle variables in the select clause. I get an error. I can get it to work when I hard code a value.
this is ridiculous...merge can't handle variables in the select clauseCare to prove?
sql> DECLARE
2 v_first_name varchar2(20) := 'BOSS';
3 BEGIN
4 MERGE INTO sun_employees se
5 USING (SELECT * FROM employees WHERE department_id = 20) e
6 ON (e.employee_id = se.employee_id)
7 WHEN MATCHED THEN
8 UPDATE SET salary = e.salary
9 WHEN NOT MATCHED THEN
10 INSERT(employee_id, first_name, last_name, department_id)
11 VALUES (e.employee_id, v_first_name, e.last_name, e.department_id);
12 END;
13 /
PL/SQL procedure successfully completed.
sql> select first_name from sun_employees;
FIRST_NAME
BOSS
BOSS -
Using @Prompt in the SELECT clause (?)
Post Author: faltinowski
CA Forum: Semantic Layer and Data Connectivity
Product: Business Objects
Version: 6.5 SP3
Patches Applied: MHF11 and CHF48
Operating System(s): Windows
Database(s): Oracle
Error Messages: "Parse failed: Exception: DBD, ORA-00903 invalid table name State N/A"
Hi! I'm bewildered -- we have an object that parses but when I try to reproduce this object, it does not.
We have a universe that's been in production for several years using an object developed by another designer who's no longer with the company. This object is a dimension, datatype is character, and there's no LOV associated. The SELECT statement in this object is
decode(@Prompt('Select Snapshot Month','A','Object Definitions\CY Month Snapshot',MONO,CONSTRAINED),'00-Previous Month',to_number(to_char(add_months(sysdate,-1),'MM')),'01-Current Month',to_number(to_char(add_months(sysdate,0),'MM')),'01-January','1','02-February','2','03-March','3','04-April','4','05-May','5','06-June','6','07-July','7','08-August','8','09-September','9','10-October','10','11-November','11','12-December','12')
This object parses. The client uses the object in the select clause to capture the "month of interest" for the report. So the report may be for the entire year's data which is graphed to show trends, but there's a table below the graph which is filtered to show just the month of interest. Typically they use the value "00-Previous Month" so they can schedule the report and it will always show the last month's data.
Problem
The original object parses.
If I copy the object within the same universe, it parses.
If I copy the code into a new object in the same universe, it doesn't parse
If I copy the code into a new object in a different universe, it doesn't parse
If I copy the object to a different universe, then edit the LOV reference, it doesn't parse
If I create any new object having @Prompt in the SELECT statement, it doesn't parse.
If another designer tries - they get the same thing.
What am I missing? Obviously someone was able to create this successfully.
On the brighter side
The object I created in a new universe (which doesn't parse in the universe) seems to work fine in the report.Seems that, the prompt syntax is correct.
But the condition is not correct.
You are taking the prompt value and not doing anything. That could be one issue for this error.
I believe that, you just want to capture the prompt value use it in report level and do not want to apply as a filter.
So, use the condition as follows.
@Prompt('Select Grouping','A',{'A','B','C'},mono,constrained) = @Prompt('Select Grouping','A',{'A','B','C'},mono,constrained)
Hope this helps! -
Using a select clause in the column formula in an Analysis
Hi all,
Is there a function or syntax that I can use to simulate a "SELECT" clause in the column formula of an Analysis? What I am trying to achieve is displaying a measure from a specific fact record into the current record. For example, if the current record displays "Region","position", and "salary", I would like to add an additional column called "compared to" in which I can display the "salary" measure from another specific fact record whose attributes I know.
Is there a function/statement that I can use to achieve this?
ThanksHi,
U can't do it in Edit formula column..it's possible below one
Add SQL filter in that column then follow blow steps
Convert this filter to SQL
i.e : add more option to sql here u can write SQL query in that column
Thanks
Deva -
Problems with query with more than 20 values in the select clause
I have a region based on a function returning a SQL query. It needs to have more than 20 values in the select clause. When I run the page I get a no data found error in the region. I managed to reproduce this behavior with just the following as the select returned by the function:
select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
from dual
I am running the 1.3.9.00.15 release of Marvel on 9.2.0.2 of the db on Solaris.Hello Raju,
I will email you the connection settings when I return to the office.
One thing I should have mentioned: The sql string is returned from a package in the db, so the query region text I originally posted isn't quite correct.
it is something like:
declare
begin
return my_pkg.my_fnc;
end;
the stored package is nothing more than:
package my_pks is
funtion my_fnc(i_test_param in varchar2) is
begin
return 'select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21 from dual';
end;
end;
Sorry for the inaccurate info in the first post, but I am away from the server in question right now. -
In a SQL statement, the SELECT clause is used to
In a SQL statement, the SELECT clause is used to select
(a) columns
(b) rows
(c) tables
(d) none of the above
can any one help ImmediatelyIs used to select rows of specified column...
SELECT column_name(s) FROM table_name -
Dividing the SELECT clause to understand
Hi All,
I am given a code which has very complicated SELECT clause which I am not able to break in understandable form.
Its like this:
for i in (select 'select ROWIDTOCHAR(rowid) rid, studyid, '''||db_link||''' db_link, rowid || '':'' || studyid || '':'' || '''||db_link||''' as text from consolidated.cp_queue_tbl'||decode(db_link,null,null,'@'|| db_link|| ' where process_start_time is null') stat from (select distinct db_link from cp_study_metadata_tbl))
I am not able to understand which are actual column and which are aliases among this. Neither I can understand where oen query ends and other begins.
Please help
Aashish S.Are you sure that the parenthesis that stands immediatly before "stat" is well placed?
You're adding the where clause only if selecting through database link, seems strange...
Maybe it should be
FOR i IN
(SELECT 'select ROWIDTOCHAR(rowid) rid, studyid, '''||
db_link||''' db_link, rowid || '':'' || studyid || '':'' || '''||
db_link||''' as text from consolidated.cp_queue_tbl'||
DECODE(db_link,null,null,'@'||db_link)||' where process_start_time is null' stat
FROM (SELECT DISTINCT db_link
FROM cp_study_metadata_tbl)
)Max
[My Italian Oracle blog|http://oracleitalia.wordpress.com/2009/12/29/estrarre-i-dati-in-formato-xml-da-sql/] -
Can a subquery in the select clause return more than one field?
Something like this:
select ename,
(select dname, loc from dept where e.deptno = deptno) as (dname,loc)
from emp eA simple way to find out is to test it. In my tests below, the original query produces an error that says it didn't find FROM where it expected. Eliminating "as (dname,loc)" produces an error about too many values. Putting only one value in the subquery in the select clause works, whether it is dname or loc. Concatenating the two columns as dname || ' ' || loc to produce one value in the subquery in the select clause works. Using two separate subqueries, one for dname and one for loc works. And, lastly, a cursor statement with both values works, although the output is a little hard to read. This may be different in newer versions. I am using Oracle 8.1.7. It may be different in 9i. I was unable to locate any documentation on the cursor statement or cursor operator which I have also heard it called. I only knew to try it because I have seen it used. I looked up the SELECT syntax in the 9i SQL Reference and there was no mention of cursor in the select clause. Can anyone provide a link to some documentation on this? I vaguely recall reading something that said that, other than outputting from SQL*Plus as below, it wasn't yet compatible with anything else, like you can't use it in PL/SQL, but I can't remember where I read it.
SQL> -- 2 values in subquery in select clause:
SQL> select ename,
2 (select dname, loc from dept where e.deptno = deptno) as (dname,loc)
3 from emp e
4 /
(select dname, loc from dept where e.deptno = deptno) as (dname,loc)
ERROR at line 2:
ORA-00923: FROM keyword not found where expected
SQL> select ename,
2 (select dname, loc from dept where e.deptno = deptno)
3 from emp e
4 /
(select dname, loc from dept where e.deptno = deptno)
ERROR at line 2:
ORA-00913: too many values
SQL> -- 1 value in subquery in select clause:
SQL> select ename,
2 (select dname from dept where e.deptno = deptno)
3 from emp e
4 /
ENAME (SELECTDNAMEFR
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
14 rows selected.
SQL> select ename,
2 (select loc from dept where e.deptno = deptno)
3 from emp e
4 /
ENAME (SELECTLOCFRO
SMITH DALLAS
ALLEN CHICAGO
WARD CHICAGO
JONES DALLAS
MARTIN CHICAGO
BLAKE CHICAGO
CLARK NEW YORK
SCOTT DALLAS
KING NEW YORK
TURNER CHICAGO
ADAMS DALLAS
JAMES CHICAGO
FORD DALLAS
MILLER NEW YORK
14 rows selected.
SQL> select ename,
2 (select dname || ' ' || loc from dept where e.deptno = deptno)
3 from emp e
4 /
ENAME (SELECTDNAME||''||LOCFROMDEP
SMITH RESEARCH DALLAS
ALLEN SALES CHICAGO
WARD SALES CHICAGO
JONES RESEARCH DALLAS
MARTIN SALES CHICAGO
BLAKE SALES CHICAGO
CLARK ACCOUNTING NEW YORK
SCOTT RESEARCH DALLAS
KING ACCOUNTING NEW YORK
TURNER SALES CHICAGO
ADAMS RESEARCH DALLAS
JAMES SALES CHICAGO
FORD RESEARCH DALLAS
MILLER ACCOUNTING NEW YORK
14 rows selected.
SQL> select ename,
2 (select dname from dept where e.deptno = deptno),
3 (select loc from dept where e.deptno = deptno)
4 from emp e
5 /
ENAME (SELECTDNAMEFR (SELECTLOCFRO
SMITH RESEARCH DALLAS
ALLEN SALES CHICAGO
WARD SALES CHICAGO
JONES RESEARCH DALLAS
MARTIN SALES CHICAGO
BLAKE SALES CHICAGO
CLARK ACCOUNTING NEW YORK
SCOTT RESEARCH DALLAS
KING ACCOUNTING NEW YORK
TURNER SALES CHICAGO
ADAMS RESEARCH DALLAS
JAMES SALES CHICAGO
FORD RESEARCH DALLAS
MILLER ACCOUNTING NEW YORK
14 rows selected.
SQL> -- cursor statement:
SQL> select ename,
2 cursor (select dname, loc from dept where e.deptno = deptno)
3 from emp e
4 /
ENAME CURSOR(SELECTDNAME,L
SMITH CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
RESEARCH DALLAS
1 row selected.
ALLEN CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
WARD CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
JONES CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
RESEARCH DALLAS
1 row selected.
MARTIN CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
BLAKE CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
CLARK CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
ACCOUNTING NEW YORK
1 row selected.
SCOTT CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
RESEARCH DALLAS
1 row selected.
KING CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
ACCOUNTING NEW YORK
1 row selected.
TURNER CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
ADAMS CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
RESEARCH DALLAS
1 row selected.
JAMES CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
SALES CHICAGO
1 row selected.
FORD CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
RESEARCH DALLAS
1 row selected.
MILLER CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
DNAME LOC
ACCOUNTING NEW YORK
1 row selected.
14 rows selected. -
How to overwrite an inner query in the select clause
Hello,
I have a queryof this form:
SELECT t1.ID,
t1.column2,
(SELECT SUM (t2.column2)
FROM table2 t2
WHERE t2 = t.ID
AND t2.column1 IN (SELECT ..... FROM table3 t3 WHERE t3.column1 = t1.column3 ........)
FROM table1 t1At the stage where I select from table3 I do not see the columns of table1. In other words:
t3.column1 = t1.column3will NOT work.
The version of the database is 10.2.0.4.
How could I rewrite this piece of code in order to surround the problem?
I cannot join table3 with table1 in the outermost query, because the structure of this database is such that it would not work for me.
I know that most of you would say, change the DB structure, but anyway, any ideas? :)
Besides, is the same restriction for inner queries applicable in Oracle 11g2?
Thanksuser13080027 wrote:
Hi
I mean I receive the error ORA-00904-"t1.column3" invalid identifier.Something does not add up here. You said earlier
I cannot join table3 with table1 in the outermost query, because the structure of this database is such that it would not work for me.Why can't you rewrite your query something like follows:
SELECT t1.ID,
t1.column2,
(SELECT SUM (t2.column2)
FROM table2 t2
WHERE t2 = t.ID
AND t2.column1 = t3.column3
FROM table1 t1, table3 t3
WHERE t3.column1 = t1.column3p.s. I am not saying this is the exact query you need but then you don't provide any details requested. ;) -
Getting Exception in the select clause - Very Urgent
Hi,
I have given the below statement but it is showing the exception.
SELECT *
FROM (lv_tab_name)
INTO CORRESPONDING FIELDS OF TABLE <fs_1>.
If I execute the above select statement it is showing the below exception.
Exception CX_SY_NO_HANDLER triggered.
An exception with the type CX_SY_OPEN_SQL_DB occurred.
It is working very well if I mention the few field names.
Please let me know the possible reasons.
Thanks,
SuvinThanks much for your quick response.
I have tried with what you mentioned but it is not working.
Please find my code below.
*Declarations
DATA: ddfields TYPE STANDARD TABLE OF ddfield.
DATA: ls_ddfields TYPE ddfield.
DATA: lt TYPE lvc_t_fcat.
DATA: ls TYPE lvc_s_fcat.
DATA: lv_tab_name TYPE dd02l-tabname.
Data References
DATA: lt_data TYPE REF TO data.
DATA: new_line TYPE REF TO data.
Field Symbols
FIELD-SYMBOLS: <fs_1> TYPE ANY TABLE.
FIELD-SYMBOLS: <fs_data> TYPE REF TO data.
FIELD-SYMBOLS: <fs_2> TYPE ANY,
<l_field> TYPE ANY.
Get the fields of the database table, here i am passing the table name
CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'
EXPORTING
KEYFIELDS = 'X'
NULLABLE = 'X'
tabname = lv_tab_name
TABLES
ddfields = ddfields.
LOOP AT ddfields INTO ls_ddfields.
ls-fieldname = ls_ddfields-fieldname.
APPEND ls TO lt.
ENDLOOP.
*Assigning Field-Symbol to our dynamic internal table
ASSIGN lt_data TO <fs_data>.
Calling the method CREATE_DYNAMIC_TABLE
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc EQ 0.
ASSIGN <fs_data>->* TO <fs_1>.
Create a work area for the dynamic table.
CREATE DATA new_line LIKE LINE OF <fs_1>.
A field-symbol to access that work area
ASSIGN new_line->* TO <fs_2>.
ENDIF.
Selecting the Data from the respective table.
SELECT *
FROM (lv_tab_name)
INTO CORRESPONDING FIELDS OF TABLE <fs_1>.
Please advise..it is very urgent.
Thanks,
Suvin -
Decimal Separator in SELECT Clause
Hi
I have the following decimal format parameters:
SQL> select value
2 from v$nls_parameters
3 where parameter = 'NLS_NUMERIC_CHARACTERS';
VALUE
If I show a number with decimal I get a comma as the decimal separator
SQL> select 10/100 from dual;
10/100
,1
But if I use a decimal separator in the SELECT clause I get:
SQL> select 100 * 1,1 from dual;
100*1 1
100 1
It doesn't work. But using a period as the decimal separator works:
SQL> select 1.1 * 100 from dual;
1.1*100
110
Maybe this is something I've never had to deal with before but I thought that the numeric format applied to the sql results and also the numbers that you used in the sql clauses.
Regards,
Néstor BoscánHi,Néstor,
user594312 wrote:
... I thought that the numeric format applied to the sql results and also the numbers that you used in the sql clauses.No; it applies to results, and it can affect implicit conversions, but it doesn't apply to SQL code.
The period (or dot, '.') is always the decimal separator in numeric literals. There is no way to change that.
Think how confusing it would be if it did apply to SQL code! For example:
WHERE num_col IN (1,2)Are we comparing num_col to 1 value or 2 values? Whichever it is, what if we wanted to do the opposite?
If you really wanted to use comma as the decimal separator, you could have to use strings, not numbers, and that could be a lot less efficient.
For example:
SELECT 100 * TO_NUMBER ('1,1') -- This assumes your NLS settings are correct
FROM dual;Of course, efficiency won't be an issue when you're selecting 1 row from dual. -
To lock the output of a SELECT query
Hi,
I want to lock the output of a SELECT query to some transaction.
I have wriiten it as follows , the output of the query is a single row;
SELECT empname
FROM employees
where empid = 100
FOR UPDATE;
Specifying only FOR UPDATE is proper or do i need to specify some more clause ??
Thanks.bscalzo wrote:
You can also specify the columns to lock if you like:
select x, y, z from aaa where x = 1 for update of x, y;
But the locking is still currently only done at the row level - but who knows what the future might offer. May not hurt to have the columns named for thay day when the syntax and engine sync up :)It might be worth to amend that specifying the columns is already particularly relevant if you select from multiple tables/views in order to specify which rows of which table/view to lock.
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
How to pass values to select clause in PL/SQL procedure
Am relatively new to PL/SQL programming and ran into the following issue...
Table
EMP_MASTER
ID VARCHAR2(10);
FIRSTNAME VARCHAR2(20);
DATA FOR EMP_MASTER
'1','SCOTT'
'2','TIGER'
I ran the following SQL Query
SELECT COUNT(*) FROM EMP_MASTER WHERE FIRSTNAME IN ('SCOTT','TIGER');
This select Query is working fine and we get the count = 2 as
expected. Now I want a procedure for the same fn()
CREATE OR REPLACE PROCEDURE TEST_EMP_MASTER(NAMELIST IN VARCHAR2)
IS
CNT NUMBER := 0;
BEGIN
SELECT COUNT(*) INTO CNT FROM EMP_MASTER WHERE FIRSTNAME IN (NAMELIST);
DBMS_OUTPUT.PUT_LINE('Output-->NAMELIST:'||NAMELIST||':cnt:'||cnt);
END;
Now when I test the procedure by passing just one value its working
fine. But when I want to pass multiple values, it doesnt work!
set serveroutput on;
i.e exec TEST_EMP_MASTER('SCOTT'); Works and the output is
Output--->NAMELIST:SCOTT:cnt:1
but don't get the expected output for exec TEST_EMP_MASTER('SCOTT,TIGER');
I understand that the IN modifier in the WHERE clause expects the
values as 'Value1','value2'....I tried different combination by the
passing the values with quotes '''value1'',''value2'''.....but no
successSelect e.*
From EMP_MASTER e;
ID FIRSTNAME
1 1 SCOTT
2 2 TIGER
3 3 CAT
4 4 MOUSE
SQL> create or replace procedure count_emp_master (p_namelist VARCHAR2)
2 AS
3 v_namelist VARCHAR2 (1000) := p_namelist;
4 v_name Varchar2(100);
5 v_count Number := 0;
6 p_count_emp Number := 0;
7 BEGIN
8 v_namelist := ',' || v_namelist || ',';
9
10 FOR cur IN 1 .. LENGTH (v_namelist) - LENGTH (REPLACE (v_namelist, ',', '')) - 1
11 LOOP
12 v_name := (SUBSTR (v_namelist,
13 INSTR (v_namelist, ',', 1, cur) + 1,
14 INSTR (v_namelist, ',', 1, cur + 1)
15 - INSTR (v_namelist, ',', 1, cur) - 1
16 ));
17 Select Count(1)
18 Into v_count
19 From emp_master
20 Where FIRSTNAME = v_name;
21
22 p_count_emp := p_count_emp + v_count;;
23
24 END LOOP;
25
26 dbms_output.put_line ('namelist --> '||p_namelist ||'p_count_emp -->'||p_count_emp);
27 END count_emp_master;
28 /
Procedure created
SQL> exec count_EMP_MASTER('SCOTT');
namelist --> SCOTTp_count_emp -->1
PL/SQL procedure successfully completed
SQL> exec count_EMP_MASTER('SCOTT,TIGER');
namelist --> SCOTT,TIGERp_count_emp -->2
PL/SQL procedure successfully completed
SQL> exec count_EMP_MASTER('SCOTT,TIGER,CAT');
namelist --> SCOTT,TIGER,CATp_count_emp -->3
PL/SQL procedure successfully completed
SQL> exec count_EMP_MASTER('SCOTT,TIGER,CAT,MOUSE');
namelist --> SCOTT,TIGER,CAT,MOUSEp_count_emp -->4
PL/SQL procedure successfully completed
SQL> Message was edited by:
Nicloei W
Maybe you are looking for
-
Questions on Lync Server Management Pack 2013
Hello All, We are using SCOM to monitor Lync Server Is it possible to create "distributed applications"? Which services are critical for Lync Server to function? Is it possible to monitor the network connection between servers?
-
How to capture the value in buffer
Hi, In VA01, when I change name of ship to party. Want to compare changed with name in ztable. But when I change the name it will be in buffer.So how to do this? Is there any function module to caputure the value in buffer. Please suggest me. Regard
-
Secondary DNS failing to redirect clients when Primary DNS goes down
I have a single domain with two Windows 2008 servers, DC1 (physical) and DC2 (virtual). Both servers run DNS and are GC servers, and the entire domain is on the same subnet (192.168.0.x). All clients on the network are configured to use DC1 as prim
-
Canceled service before end of contract and billing cycle. Being charged an extra month!
Hi. I have been a loyal Verizon wireless customer for a long while, but have had such a bad experience in the past year (where I have paid bills as high as $700.00) that I decided to cancel my services and move to another network. My billing cycle ru
-
I have been having a lot of trouble with Safari Version 7.0.2 (9537.74.9) since upgrading to 10.9.2. One such problem is with pull-down menus in websites. (This is as distinguished from the pull-down menus of Safari itself.) Many websites have