How to return varied no.of columns from Stored Procedure?
Hi All,
I have situation where I need to return with columns which will keep changing. At any given point of time I will be returning 10 columns. But there are some conditions where I need to add multiples of 4 no.of columns.
Ex: Always 10 columns.
If condition1
return 10 + 4 columns
if condition2
return 10 + 4 + 4 columns
How can I handle this?
Can any one help me here.
Thanks
Vikram
Change the stored procedure to a single column set return type like returning nulls if column is not applicable.
Alternate, split the sp to single column set sp-s.
You can get the returned column set by SELECT TOP(0) * INTO spxColumns FROM OPENQUERY..... method:
http://sqlusa.com/bestpractices2005/selectfromsproc/
http://www.sqlusa.com/bestpractices/select-into/
Note, however, the OPENQUERY method is not completely clean.
Kalman Toth Database & OLAP Architect
Free T-SQL Scripts
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012
Similar Messages
-
How to return a VARRAY datatype in a stored procedure
I have declared a type using
create type list1 as VARRAY(1000) of number(5);
I want to return a list of values from a stored procedure. I created a stored procedure using
create procedure getlist(mylist OUT list1) is
begin
end;
The type and procedure creation was successful. But when I call this stored procedure from a PL/SQL module I get the following error.
ORA-06531: Reference to uninitialized collection
The PL/SQl module is
declare
s1 list1(1,2);
begin
getlist(s1);
end;
I want the procedure to return around 1000 values. Any solutions.
Mohan
null<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by MOHAN [email protected]:
I have declared a type using
create type list1 as VARRAY(1000) of number(5);
I want to return a list of values from a stored procedure. I created a stored procedure using
create procedure getlist(mylist OUT list1) is
begin
end;
The type and procedure creation was successful. But when I call this stored procedure from a PL/SQL module I get the following error.
ORA-06531: Reference to uninitialized collection
The PL/SQl module is
declare
s1 list1(1,2);
begin
getlist(s1);
end;
I want the procedure to return around 1000 values. Any solutions.
Mohan<HR></BLOCKQUOTE>
I am also having the same problem ..plzz help me someone
null -
How to find if certain record exists from stored procedure
Hello
I am not an expert in this and am trying simple thing. I want to find if certain record exists in a table and if so set some boolean variable.
create or replace procedure findit(param)
AS
return_group boolean;
BEGIN
myflag := false;
--here goes my question
-- say I have SELECT WHATEVER FROM TABLE WHERE BLA = param
--if it returns at least one record set myflag to true;
END;Any idea?THanks Satyaki_De,
However, when I put simple SELECT statement in the body of my procedure it does not compile. Actually I should have said that before. So here is my code and where it breaks:
create or replace procedure close_subjects(study_id varchar2)
AS
return_group boolean;
BEGIN
dbms_output.enable(1000000);
FOR current_group IN(
SELECT DISTINCT group_id from groups WHERE study_id=study_id
) LOOP
FOR current_subject IN(
SELECT individual_id from groups WHERE group_id=current_group.group_id AND study_id=study_id
) LOOP
return_group := true;
SELECT INDIVIDUAL_ID FROM ASSIGN WHERE DATE_TIME_ASSIGNED = ( SELECT MAX(DATE_TIME_ASSIGNED) FROM ASSIGN WHERE INDIVIDUAL_ID = current_subject.individual_id ) AND ASSIGN_STATUS_ID = 'A';
IF SQL%RowCount = 0 THEN
return_group := false;
dbms_output.put_line(current_subject.individual_id);
END IF;
END LOOP;
END LOOP;
END;
/If I comment out SELECT statement and leave dbms_output for testing purposes it works well. As I said I am faaaar from expert and seems to me that I cannot have SELECT in procedure BODY? -
How to return a record's column head given a record of a table.
Hi:
Do you know how to return a record's column head given a record of a table.
For example, in a table with 3 columns "ID", "Education", "Age".
Give you the record "Bachelor", how can you use a SQL to return the correspondent column head "Education"?
Thanks for the big help.Have a look at http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSetMetaData.html.
import java.sql.*;
import java.util.*;
ResultSet rs = stmt.executeQuery("SELECT * FROM testTable");
ResultSetMetaData rsmd = rs.getMetaData();
// loop through rsmd.getColumnCount() {
String columnName = getColumnName(i);
// run a query SELECT * FROM testTable WHERE columnName='Bachelor'
// if rs !=null return columnName and break out of the loop
} -
How can I remove the Location column from the search results in a .chm file?
How can I remove the Location column from the search results in a .chm file?
I generated the file in Robohelp HTML 9.
As far as I know it's used when you've combined more than one source into the final help file. It is possible (though I don't remember doing it) that I did that once a couple of years ago but now I'm only using one source - the project that's generating the .chm.
Can anyone tell me how I can remove it?
Thanks
TomIn Project Setup look at the Window properties. I think you will find Advanced Search is ticked.
See www.grainge.org for RoboHelp and Authoring tips
@petergrainge -
Return to more than columns from LOV
I need return to more than columns from LOV. All columns belong to one form region
Please helpl me
MaryHi Mary,
Do you mean you need to return more than 3? If so then in the link area change the Target to URL and do something like the following:
f?p=&APP_ID.:7:&SESSION.::&DEBUG.::P7_UNIQUE_ROWID,P7_OET_TBL_NM:#UNIQUE_ROWID#,#OET_TBL_NM#Items on left separated by commas, values on right separated by commas and a colon (:) in the middle...make sure the items & values are placed in the same order.
Mike -
How can I edit the SamAccountName column from the list or be able to edit it?
Hello,
In my view for a SharePoint list, I see three columns for SamAccountName. This list is for listing staff as a directory, so there are phone numbers, names, emails addresses, division, department and such. The SamAccountName
column for some people has incorrect username or is blank. When I edit a record in the list, the SamAccountName column is not available to edit.
How can I edit the SamAccountName column from the list or be able to edit it?
I do have user profiles working and importing profiles from AD, but the list still shows empty or incorrect SamAccountName for some records.
PaulIf the user does not remember the encryption password there is no way to use the backup.
The password cannot be removed or reset.
Delete the backup, restore as new. Sync the desired content to the device. -
Hi Experts,
I have on stored procedure which returns mote than one resultset i want that to store in two different temp table how can achieve this in SQL server.
following is the stored procedure and table that i need to create.
create procedure GetData as begin select * from Empselect * from Deptend
create table #tmp1 (Ddeptid int, deptname varchar(500),Location varchar(100))
Insert into #tmp1 (Ddeptid , deptname ,Location )
exec GetData
create table #tmp (empid int , ename varchar(500),DeptId int , salary int)
Insert into #tmp (empId,ename,deptId,salary)
exec GetData
Niraj SevalkarYou cant get two resultsets out of SP like this. The workaround is to merge and bring the resultsets.
For this number of columns as well as corresponding datatypes have to be compatible. Also you will need one additional column which indicates resultset value. Then use this as filter to get your desired resultset out
create procedure GetData as
begin
select 'resultset1' as Cat,*,.. N columns from Emp
union all
select 'resultset2' as Cat,*,.. N columns from Dept
end
create table #tmp1 (Ddeptid int, deptname varchar(500),Location varchar(100))
Insert into #tmp1 (Ddeptid , deptname ,Location )
Select column1,column2,column3
from OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
Integrated Security=SSPI','Execute yourdb..GetData')
WHERE Cat = 'resultset1'
create table #tmp (empid int , ename varchar(500),DeptId int , salary int)
Insert into #tmp (empId,ename,deptId,salary)
Select column1,column2,column3, column4
from OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
Integrated Security=SSPI','Execute yourdb..GetData')
WHERE Cat = 'resultset2'
also see
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx
Another method is to populate table with relevant resultset within procedure itself and then select from the table directly outside.
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
How to create a LOV based on a stored procedure returning a cursor
Hello,
I've tried to search the forum, but did not find much. We are facing a problem of large LOVs and creating large TMP files on the app server. Our whole application is drived by store procedures. LOVs are built manually by fetching data from cursors returned from stored procedures. That creates the issue when whole LOV needs to be stored in the memory and thus the TMP files.
Is there anyway how to create LOV based on a procedure returning cursor ?
Thank you,
RadovanHello,
As of now we populate the record group by looping through the ref cursor and adding rows into it. Is there a better way? That forces the whole record group to be stored in a memory on the app server.
Thank you,
Radovan -
URGENT : Return Bulk data from Stored Procedure
Hi,
Tell me, how do I return a bulk of data which
does not exist in the data base
but is concluded while the Stored Procedure is executed
from the Stored procedure
to the C++ program.
For Example:
Table ABC
Field1 Field2 Field3
A 1 3
B 1 5
C 2 10
Table DEF
Field1 Field2 Field3
D 10 24
E 3 16
F 8 19
SP_TESTING
Depending on the values in both the tables
for some range of conditions,
a conclusion X is derived for each range value of the
condition range.
Now I need to return this bulk of data X with the
condition they belong to
back to the C++ code calling it....
NOTE : A stored procedure is requited as there is a lot
of processing
required before we conclude the result X for each value
in the condition range.
If I execute this code from C++ instead of Stored
procedure
it is very slow and speed is a prime requirement of my
system.
Also i'm not using any MFC class to access database.
I'm using ConnectionPtr, RecordsetPtr and _CommandPtr
from msado15.dll for database access...
One solution to this could be use of Temp tables.
As this process is used by a lot of different stored
procedures having a common
temp table to all will need something like 50 NUMERIC
fields, 50 VARCHAR fields
and so on, which doesn't seem like a very good solution
to this problem.
Sounds like something I would have done while in school,
implement a dumb solution.
So, please suggest me a solution as to how do I return
bulk data in the form
of recordsets from stored procedure.
Regards
ShrutiUse Out parameter mode
SQL> CREATE OR REPLACE procedure a1 (x OUT NUMBER, y OUT NUMBER) AS
2 BEGIN
3 x:= 1;
4 y:= 2;
5 END;
6 .
SQL> /
Procedure created.
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
2 a NUMBER :=3;
3 b NUMBER :=4;
4 BEGIN
5 a1 (a,b);
6 DBMS_OUTPUT.PUT_LINE( 'a = ' || a );
7 dbms_output.put_line( 'b = ' || b );
8 END;
9 .
SQL> /
a = 1
b = 2
PL/SQL procedure successfully completed.By default parameters are copied to the OUT parameter mode .
COPY hint in PLSQL don’t send a pointer to calling program unit but NOCOPY
does.
Khurram -
Ora-06550 returning data from Stored Procedure and Entity Data Model
Hi.
I'm creating an application that uses a WCF Service to return data. I also created a proyect with the EDMX design and mapped most of my DBModel to a classes context. I have added some of the procedures as well. One of them receive some parameters and return a Sys_RefCursor, that is populated according to the parameters.
I have declared the "<add >" tags in the Web.Config and imported the function of the Procedure. When I call the Asyncronous function I get different exceptions:
1. If I call the function, with all of the parameters i get:
Oracle.DataAccess.Client.OracleException: ORA-06550: línea 1, columna 8:
PLS-00306: número o tipos de argumentos erróneos al llamar a
'SP_HECHOSJURITER'
ORA-06550: línea 1, columna 8:
(wrong number or types of arguments in call to 'SP_HECHOSJURITER')
2. If i just set 1 parameter in the SP, returning the same type of data, I get:
Error al recibir la respuesta HTTP a
http://localhost/Procalculo.CGFM.SIGOC.DatosServices/ServiceDatos.svc.
(failed to receive http response. error 12152)
3. If I don't set any parameters in the procedure, it works fine, and return correct data.
It exclusively happen with one entity.
Any clue?
I appreciate any help.When you return result sets from stored procedures to Entity Framework, you are very likely using implicit result sets. Implicit result sets don't need to be declared as a parameter in code, only in the <add> tags to define the metadata in the .NET config file.
For example, in the EF Oracle By Example, you'll see that the stored procedure in the function import has three parameters, but only two are declared in the code. The third one was defined in the config file.
http://download.oracle.com/oll/obe/EntityFrameworkOBE/EntityFrameworkOBE.htm -
Returning SQL cursor from Stored Procedure
Hi,
I have a query regarding returning sql cursor from stored procedure to java in oracle 11g.
I want to query some data ex: my query returns A, B, C. Also, now I want to query some other data ex: D, E, F. Now I want to return this data as an sql cursor as a single row . Example: A,B,C,D,E,F. Is it possible to return/create a cursor in stored procedure?
assume both query returns equal number of rows.. however both are not related to each other..RP wrote:
Hi,
I have a query regarding returning sql cursor from stored procedure to java in oracle 11g.
I want to query some data ex: my query returns A, B, C. Also, now I want to query some other data ex: D, E, F. Now I want to return this data as an sql cursor as a single row . Example: A,B,C,D,E,F. Is it possible to return/create a cursor in stored procedure?
assume both query returns equal number of rows.. however both are not related to each other..It sounds like what you need is a ref cursor.
First thing to remember though is that cursors do not hold any data (see: {thread:id=886365})
In it's simplest form you would be creating a procedure along these lines...
SQL> create or replace procedure get_data(p_sql in varchar2, p_rc out sys_refcursor) is
2 begin
3 open p_rc for p_sql;
4 end;
5 /
Procedure created.
SQL> var rc refcursor;
SQL> exec get_data('select empno, ename, deptno from emp', :rc);
PL/SQL procedure successfully completed.
SQL> print rc;
EMPNO ENAME DEPTNO
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
14 rows selected.
SQL> exec get_data('select deptno, dname from dept', :rc);
PL/SQL procedure successfully completed.
SQL> print rc
DEPTNO DNAME
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
50 IT SUPPORTWhich takes an SQL statement (as you said that both your queries were unrelated), and returns a ref cursor, and then your Java code would fetch the data using that cursor.
Now, as for getting your rows to columns and combining two queries that do that... something along these lines...
SQL> select * from x;
C
A
B
C
SQL> select * from y;
C
D
E
F
SQL> ed
Wrote file afiedt.buf
1 select x.col1, x.col2, x.col3
2 ,y.col1 as col4
3 ,y.col2 as col5
4 ,y.col3 as col6
5 from (
6 select max(decode(rn,1,col1)) as col1
7 ,max(decode(rn,2,col1)) as col2
8 ,max(decode(rn,3,col1)) as col3
9 from (select col1, rownum rn from (select * from x order by col1))
10 ) x
11 cross join
12 (
13 select max(decode(rn,1,col1)) as col1
14 ,max(decode(rn,2,col1)) as col2
15 ,max(decode(rn,3,col1)) as col3
16 from (select col1, rownum rn from (select * from y order by col1))
17* ) y
SQL> /
C C C C C C
A B C D E F... will do what you ask. For further information about turning rows to columns read the FAQ: {message:id=9360005} -
Returning object from stored procedures
I am having a lot of trouble finding an example to demonstate how I return a Java/DB Object from a Java Stored Procedure. All the examples I have been able to find return one of the native types (VARCHAR, NUMBER, etc). As an example...
public static DataPoint getPoint() {
return new DataPoint(
new Timestamp(System.currentTimeMillis()),
new BigDecimal(50.0));
Assuming DataPoint is a Java class mapped to an Oracle Data Type created via the following statement.
create or replace type data_point as object (
p_date date,
p_data number
After loading and publishing the procedure I get no (zero, 0) rows returned when calling. I should be getting one row returned.
Thanks,
MikeMike,
You said:
Assuming DataPoint is a Java class mapped to an Oracle
Data TypeI think your assumption is incorrect.
In any case, isn't this what "TopLink" is used for?
(I don't know, since I don't use "TopLink".)
Have you tried the TopLink/JPA forum?
Good Luck,
Avi. -
How to generate a report from stored procedure
I would like to generate a report from stored procedure.
I used to work on sql server. this can be done as easy as put a select statement at the end of stored procedure.
The resule can be displayed on the development IDE, like sql developer or consume by Java JDBC client.
is there equivalent way to do this in Oracle stored procedure?Hi,
What type of report you are looking..for.. ??
As you said that "I used to work on sql server. this can be done as easy as put a select statement at the end of stored procedure. "
When you execute it will return the result set and you will display directly on the FrontEnd.. Is my Understanding is correct Up to here.
See, In oracle you have call some custom stored procedures as you did in SQL Sever, but you have return the Results Sets, with help of Out put paramter, Either Cursors or Varrays..
or Else you can generate the Html reports based on your requirement, HTML can be used in the stored procedures of Oracle which will generate for your, you need to code it.
I could not able get the relevant link for your reference.
I will get back to you on this.
- Pavan Kumar N -
Returning rowcount and resultset from stored procedure
Hello,
In SQL Server you can return multiple resultsets from stored procedure by simple SELECT statement. But in Oracle, you have to use SYS_REFCURSOR.
Sample code:
CREATE OR REPLACE PROCEDURE sp_resultset_test IS
t_recordset OUT SYS_REFCURSOR
BEGIN
OPEN t_recordset FOR
SELECT * FROM EMPLOYEE;
END sp_resultset_test;
Is there any other way in Oracle 10 or 11 ?
Thank You.What is the requirement? Oracle is far more flexible than SQL-Server... with numerous features that do not exist in SQL-Server. (Fact)
One of the biggest mistakes you can make is to treat Oracle like SQL-Server... trying to match feature A1 in SQL-Server to feature B3 in Oracle.
Does not work that way.. rather then stick to SQL-Server as SQL-Server does SQL-Server specific features far better than Oracle.
So instead of trying to map what a T-SQL stored proc to do to an Oracle ref cursor (even to the extent of using that very silly sp_ prefix to name the proc), tell us the problem and requirements... That way we can tell you what Oracle features and options are best used to solve that problem - instead of competing in some unknown feature comparison event with SQL-Server.
Maybe you are looking for
-
Hi Would anyone know if I can I move my iTunes library onto Time Capsule and share it across my devices (similar to a NAS drive)? This would give me some hard drive space on my iMac and more importantly mean my iTunes library is available all the tim
-
EDirectory question about OES2SP3 to OES11SP1 upgrade
My apologies if this is not the correct place to ask this question but I decided to ask here becuase my question involved OES. We are going to perform an offline upgrade to OES 11 SP1 of an OES2SP3 server that hasn't been patched in one year. Novell
-
Photoshop and lightroom previously downloaded...now gone
I have a windows operating system and have used both of these to edit last week...now they are gone and they won't re-download...what happened?
-
Can i use lightroom 4 with windows xp
help with lightroom 4
-
Hi I have a strange Use-Case for Lightroom. Sometimes i want to use my Lightroom on my Main-Computer via a VNC-Connection. This worked fine until i upgraded to Lightroom 2.0. After this upgrade i can't see some Windows opened by Lightroom. The Main A