How to Create a procedure
I would like to convert the below query to procedure that takes to parameter.Instead of hardcoding the value for the date, i would like to pass it as a parameter from my asp.net application.Can someone take a look at it and help me altering this sql scrip to procedure that takes two date vlues.
AND (P.DATEPUR >= TO_DATE('2002-01-01','YYYY-MM-DD')
AND P.DATEPUR <= TO_DATE('2005-12-31','YYYY-MM-DD'))
SELECT DISTINCT V.VENDOR, R.ADDRNUM, V.VNAMEL, R.AADDR1, R.ACITY, R.ASTATE,
R.AZIPCODE, R.APHONE, MAX(P.DATEPUR)
FROM VENDOR V,VENDADDR R,PLANHOLD P
WHERE V.VENDOR = R.VENDOR
AND P.VENDOR = R.VENDOR
AND (P.DATEPUR >= TO_DATE('2002-01-01','YYYY-MM-DD')
AND P.DATEPUR <= TO_DATE('2005-12-31','YYYY-MM-DD'))
AND V.VOBSOLET = 'N'
GROUP BY V.VENDOR, R.ADDRNUM, V.VNAMEL, R.AADDR1, R.ACITY, R.ASTATE,R.AZIPCODE, R.APHONE
ORDER BY V.VENDOR;
here is an example:
CREATE OR REPLACE PROCEDURE myProcedure(pFromDate Date,
pToDate Date) Is
CURSOR c1 IS
SELECT DISTINCT V.VENDOR, R.ADDRNUM, V.VNAMEL, R.AADDR1, R.ACITY, R.ASTATE,
R.AZIPCODE, R.APHONE, MAX(P.DATEPUR)
FROM VENDOR V,VENDADDR R,PLANHOLD P
WHERE V.VENDOR = R.VENDOR
AND P.VENDOR = R.VENDOR
AND (P.DATEPUR >= TO_DATE(pFromDate,'YYYY-MM-DD')
AND P.DATEPUR <= TO_DATE(pToDate, 'YYYY-MM-DD'))
AND V.VOBSOLET = 'N'
GROUP BY V.VENDOR, R.ADDRNUM, V.VNAMEL, R.AADDR1, R.ACITY, R.ASTATE,R.AZIPCODE, R.APHONE
ORDER BY V.VENDOR;
BEGIN
FOR c1_rec in c1 LOOP
END;
END;
/
Similar Messages
-
How to create a procedure in oracle to write the data into file
Hi All,
I am just wondered on how to create a procedure which will do following tasks:
1. Concat the field names
2. Union all the particular fields
3. Convert the date field into IST
4. Prepare the statement
5. write the data into a file
Basically what I am trying to achieve is to convert one mysql proc to oracle. MySQL Proc is as follows:
DELIMITER $$
USE `jioworld`$$
DROP PROCEDURE IF EXISTS `usersReport`$$
CREATE DEFINER=`root`@`%` PROCEDURE `usersReport`(IN pathFile VARCHAR(255),IN startDate TIMESTAMP,IN endDate TIMESTAMP )
BEGIN
SET @a= CONCAT("(SELECT 'User ID','Account ID','Gender','Birthdate','Account Registered On') UNION ALL (SELECT IFNULL(a.riluid,''),IFNULL(a.rilaccountid,''),IFNULL(a.gender,''),IFNULL(a.birthdate,''),IFNULL(CONVERT_TZ(a.creationDate,'+0:00','+5:30'),'') INTO OUTFILE '",pathFile,"' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\n' FROM account_ a where a.creationDate>='",startDate,"' and a.creationdate <='",endDate,"')");
PREPARE stmt FROM @a;
EXECUTE stmt;
DEALLOCATE PREPARE stmt ;
END$$
DELIMITER ;
Regards,
Vishal G1. Concat the field names
Double Pipe (||) is the concatenation operator in Oracle. There is also a function CONCAT for this purpose
2. Union all the particular fields
Not sure what do you mean by UNION ALL particular fields? UNION ALL is a set operation applied on two different result sets that have the same projection.
3. Convert the date field into IST
SQL> select systimestamp "Default Time"
2 , systimestamp at time zone 'Asia/Calcutta' "IST Time"
3 from dual;
Default Time IST Time
05-05-15 03:14:52.346099 AM -04:00 05-05-15 12:44:52.346099 PM ASIA/CALCUTTA
4. Prepare the statement
What do you mean by prepare the statement?
5. write the data into a file
You can use the API UTL_FILE to write to a file. -
How to create a procedure to send a mail if the Database is down?
Hi,
I have created the below procedure to send a mail if the count is less than 1300. It scheduled daily @ 15 30 hrs. Its fine.
CREATE OR REPLACE procedure SCOTT.hrsmail
is
v_count number;
begin
Select count(*) into v_count from emp;
if v_count < 1300
then
UTL_MAIL.send(sender => '[email protected]',
recipients => '[email protected]',
cc => '[email protected]',
bcc => '[email protected]',
subject => 'Testing the UTL_MAIL Package',
message => 'If you get this, UTL_MAIL package
else
null; --what you want to do here
end if ;
end;
Sometime the Database is down, so the job is not running.
How to create a procedure to send a mail if the database is down?
Pls help me. Its highly appreciated.
Thanks
Niharnihar wrote:
How to create a procedure to send a mail if the database is down?And what if the database is up, but the network down? Or the database up and mail server down? Or mail server undergoing maintenance?
There are loads of "+What if's+" - and in that respect, playing "+What if database is down..+" in this case does not make any sense. You do not use the database to monitor its own up/down status. You do not rely just on SMTP as notification protocol that the database is down.
The correct approach would be using something like SNMP as the monitoring protocol. A monitoring system that can process SNMP and perform some basic root cause analysis (e.g. network to the database server down, database server status unknown). And this system supporting notification methods like SMTP, SMS and so on. -
How to create a procedure function with a return value of ref cursor?
Can anybody provide a sample about how to create a procedure function with a return value of REF CURSOR?
I heard if I can create a function to return a ref cursor, I can use VB to read its recordset.
Thanks a lot.http://osi.oracle.com/~tkyte/ResultSets/index.html
-
How to create stored procedures and use those in Crystal reports
Hi All,
Can anyone explain me how to create a stored procedure and use that stored procedure in Crystal reports. As I have few doubts in this process, It would be great if you can explain me with a small stored proc example.
Thanks in advance.Hello,
There is a document explainining how to use Oracle stored proc with Crystal reports.
Check at:
https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/1084c536-711e-2b10-e48a-924a60745253
It is for older version but it should work and you can refer to it to get general idea how to start with it. It has a sample oracle stored proc and how to connect it from Crystal Reports. -
HOW TO CREATE STORED PROCEDURE IN DATA INTEGRATOR
Hi to every one,
Can any one help in giving me a solution for creating stored procedure in Data Integrator.
I m new to this fieldHi nath,
Firstly are you using MYSQL or Oracle as the database,if its oracle database then follow this
http://obiee101.blogspot.com/2008/01/obiee-using-oracle-stored-procedure-to.html
http://oraclebizint.wordpress.com/2008/02/20/oracle-bi-ee-101332-executing-stored-proceduresfunctions-before-reports-before-report-triggers-and-global-temporary-tables/
EXEC [DATABASE_NAME].[SCHEMA_NAME].[PROCEDURE_NAME][DATABASE_NAME] --> is the database name your creating your procedure
[SCHEMA_NAME]-->is the user with which your creating
[PROCEDURE_NAME] --> the name given to procedure
You dont know how to get those run this SQL in TOAD and see select sys_context('userenv','db_name'), sys_context('userenv','session_user') from dual
(OR) open you connection pool properties window in RPD,you will get the DB name and the user name as the schema name
hope answered your question.
CHeers,
KK -
How to create stored procedure directely in RPD
Hi Gurus,
I am trying to create stored procedure directly in physical table in rpd level in OBIEE.for this one i have gone through below website.
http://obiee101.blogspot.com/2011/01/obiee-using-mssql-stored-procedure-as.html
in that blog they mentioned to write some query which is...........
EXEC [DATABASE_NAME].[SCHEMA_NAME].[PROCEDURE_NAME]
in above query i am not getting what is DATABASE_NAME,SCHEMA_NAME,PROCEDURE_NAME
can any one elobrate this oneHi nath,
Firstly are you using MYSQL or Oracle as the database,if its oracle database then follow this
http://obiee101.blogspot.com/2008/01/obiee-using-oracle-stored-procedure-to.html
http://oraclebizint.wordpress.com/2008/02/20/oracle-bi-ee-101332-executing-stored-proceduresfunctions-before-reports-before-report-triggers-and-global-temporary-tables/
EXEC [DATABASE_NAME].[SCHEMA_NAME].[PROCEDURE_NAME][DATABASE_NAME] --> is the database name your creating your procedure
[SCHEMA_NAME]-->is the user with which your creating
[PROCEDURE_NAME] --> the name given to procedure
You dont know how to get those run this SQL in TOAD and see select sys_context('userenv','db_name'), sys_context('userenv','session_user') from dual
(OR) open you connection pool properties window in RPD,you will get the DB name and the user name as the schema name
hope answered your question.
CHeers,
KK -
How to create several procedures from several script files?
Hello,
I have several procedures for SAP HANA. Every procedure is stored in a script file. I can only use SAP HANA Studio. How can I easily run all script files to create all procedures on a schema? Can somebody help me?
Best regards,
Y.HuHi Fernando,
Thank you very much for you explanation.
My scripts contain native sql statements for creation stored procedures “CREATE PROCEDURE … AS BEGIN … END”. They are not objects for or from Content/package. The procedures should be created direct in a project schema.
The option with hdbsql command line is not possible because I may not use hdbsql (unfortunately not allowed for me).
The option all scripts into a big file is a possible option for me. The big file has only 1600 lines. But there is a strange problem with the text “FOR” in script (please see the thread http://scn.sap.com/thread/3728741 ). Unfortunately SAP HANA Studio cannot run my big script.
I hope there is a solution or workaround for this problem.
Best regards,
Y.Hu -
How to create approval procedure when saved as draft?
How can I create an approval procedure where the document is saved as draft?
Specifically, returns saved as draft where document total is greater than $3,000 need approval.
ThanksHi,
Approval Procedures cant be used in draft mode. Approval Procedure itself acts as document in draft. It wont do inventory postings & financial postings as well. Just create approval procedure with your requirement & try.
I am looking forward your reply,
Regards,
Maheswaran. -
How to create stored procedures automatically when creating new company
I need to create a method in our add-on Business One application that will create stored procedures at the time we install the add-on (when the new company is created). Where would I store the SQL scripts, and what SAP Business One commands do I need to use to build the stored procedures into the new company database? Can someone give me an example of how to do this? We are coding in C#, but an example in visual basic would also be helpful if no one out there in forum land uses C#
Thanks,
Nancy Walk
[email protected]Hi Nancy!
Of course are there people here that use C#
To solve your problem, create a routine in your add-on that checks at startup if the stored procedures are present. If they are not, create them. You can use the DI-API, or just add them directly to your database.
<i>Note: SAP does not like it when you play around directly in the SAP databases, and does not supply support for databases that are changed manually. You'd better use the DI-API...</i>
Hope it helps...
Grtz, Rowdy -
How to create a procedure using program unit
Hai All
Ii have created a procedure like this
PROCEDURE Duty_calc
IS
procedure w_Time
is
begin
update dail_att set wtime= (select lpad((to_number(to_char(outtime,'hh24mi')-to_char(intime,'hh24mi'))),4,0) from dail_att where attend_date=f_date);
end w_time;
begin
if wtime >0830 then
update dail_att set etime= (select lpad(wtime-0830) from dail_att where attend_date=f_date);
else
null;
end if;
duty_calc;
end duty_calc;
And i have declare in the program unit as Duty_calc and while i am executing my procedure i had an error
is it must i need to declare a my inner procedure in my program unit
Regards
Srikkanth.Mthe same question you asked 2 month ago and same mistake you are doing
- Procedure name can only be one.
- Count proper BEGIN ENDs
PROCEDURE Duty_calc
IS
BEGIN
begin
update dail_att set wtime= (select lpad((to_number(to_char(outtime,'hh24mi')-to_char(intime,'hh24mi'))),4,0) from dail_att where attend_date=f_date);
end;
begin
if wtime >0830 then
update dail_att set etime= (select lpad(wtime-0830) from dail_att where attend_date=f_date);
else
null;
end if;
end;
END; -
How to create a procedure to change randomly passwords
Hello,
I am trying to create a procedure to change randomly passwords for all users in a database. I need this after the database cloning. I have too many users to alter in a manual way...
Is there any option to create a procedure which will fetch all users in a database and alter them by a random password?
I was not able to find any clue.
Could you help me?
ThanksWelcome to the forum.
change randomly passwords for all users in a database.All users? Including SYS/SYSTEM? I hope not...
But you could use DBMS_RANDOM.STRING and ALL_USERS and dynamic SQL.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_random.htm#sthref4675
http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2114.htm#REFRN20302
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/dynamic.htm#LNPLS01101
(easy to find when you do a quick search from http://www.oracle.com/pls/db102/homepage or http://www.oracle.com/pls/db112/homepage)
DBMS_RANDOM.STRING can give you a random password easily:
select dbms_random.string('x', 10)
from dual
connect by level <= 10;Zo you could do something like:
begin
for rec in ( select t.username
, dbms_random.string('x', 10) new_pass
from all_users t
-- where t.username not in (...)
-- or
-- where t.username in (...)
loop
--execute immediate ' alter user '||rec.username||' identified by '||rec.new_pass;
dbms_output.put_line ('New password for user '||rec.username||' = '||rec.new_pass);
end loop;
end;
/You'll have to fill in the where-clause yourself/
I also commented out the dynamic alter user statement, since I'm not sure if you really want to reset the pwd's for all users.
Also, instead of using dbms_output.put_line to check the new passwords, you could insert them in a table or spool the output to a file. -
How to Create a Procedure/Function to Return more than one value
How I can write a function/Procedure to which one value is passed and it will return nine values. How I can use these values
Syed,
I would use PL_SQL table versus a VARRAY for this purpose as you will have an advantage of joining PL_SQL table if you want to in your SQL statements in the procedure.
1. At the SQL prompt, create a type using,
create or replace type myTable as table of VARCHAR2(100);
2. Pass the table to your procedure as IN OUT parameter,
Create or replace procedure
myProc(pvar1 IN Number, passingArray IN OUT myTable) AS
Begin
--Fill the array with your logic
for i in 1..9
loop
passingArray.extend;
passingArray(passingArray.count) := 'what ever';
end loop;
End;
3. From your Main prog, you call Myproc
--declare a variable for type first
passingArray myTable := myTable();
begin
myProc(10, passingArray());
--At this point, You would be able to Join the PL_SQL table
--which gives you the power of using SQL with PL_SQL tables
end; -- end of main program
4. All done!I have not shown how to use PL_SQL tables in SELECT statements, as that is not the subject here.
At the end of the story, I would say, if you know the number of arguments that you are going to pass to a procedure, Simply use "that many" IN OUT parameters to finish your task(9 in your case). Although the proc call looks large with this, it is much simpler. The above approach is VERY helpful if YOU DO NOT KNOW THE NUMBER OF ARGUMENTS that you are sending AND receiving From a procedure.
Thx,
SriDHAR -
How to create a procedure to output REF CURSOR with any WHERE clause?
I have an requirement like this: I have huge query which need to reuse in my code more than 10 times. This SQL has about 50 lines. Thing is for those 10 odd times sometimes the WHERE clause changes (columns are the same). So I cannot create a view since SQL is not static.
I thought of writing a procedure with a WHERE_CLAUSE input para. I output a sys refcursor by adding the where clause. But I can't do it since you cannot add a where clause like that.
i.e.
PROCEDURE dynamyic_query (p_where_clause IN VARCHAR2, p_out_query OUT SYS_REFCURSOR ) IS
BEGIN
OPEN p_out_query FOR SELECT ......... FROM table WHERE || ' ' || p_where_clause;
END;The above gives error.
How to handle a situation like this???? Any help would be greatly appreciated.I tried this method:
I created a table tab_test which has these records:
TNAME TABTYPE CLUSTERID
ABS_V4_P_ERROR_MESSAGES TABLE
ABS_V4_P_ORG_PARAM TABLE
ABS_V4_P_PARAMETER TABLE
ABS_V4_P_SYS_PARAM TABLE
ACCINTERFACE_PARAMETERS TABLE
ACCOUNTS TABLE
ACCOUNT_EXTRACT_PERIODS TABLE
ACCOUNT_EXTRACT_PERIODS# TABLE
ACCOUNT_EXTRACT_PERIODS_1 TABLE Now I create this proc:
PROCEDURE FORMS_TEXT_DYN_SQL_TEST(p_where_cluase IN VARCHAR2, p_out_cursor OUT SYS_REFCURSOR) IS
v_stmt VARCHAR2(1000);
BEGIN
v_stmt := 'SELECT tname FROM tab_test WHERE tname LIKE ''%ABS_V4%'' AND tabtype = :x';
OPEN p_out_cursor FOR v_stmt using p_where_cluase;
END;I create this code block and run it:
declare
v_tname varchar2(200);
out_cursor sys_refcursor;
begin
forms_text_dyn_sql_test('TABLE', out_cursor );
LOOP
fetch out_cursor INTO v_tname;
exit when out_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_tname);
END LOOP;
end;
/I get correct output:
ABS_V4_P_ERROR_MESSAGES
ABS_V4_P_ORG_PARAM
ABS_V4_P_PARAMETER
ABS_V4_P_SYS_PARAMHowever, when I change the proc like this:
PROCEDURE FORMS_TEXT_DYN_SQL_TEST(p_where_cluase IN VARCHAR2, p_out_cursor OUT SYS_REFCURSOR) IS
v_stmt VARCHAR2(1000);
BEGIN
v_stmt := 'SELECT tname FROM tab_test WHERE tname LIKE ''%ABS_V4%'' AND :y';
OPEN p_out_cursor FOR v_stmt using p_where_cluase;
END;And run this code block:
declare
v_tname varchar2(200);
out_cursor sys_refcursor;
begin
forms_text_dyn_sql_test(' 1 = 1 ', out_cursor );
LOOP
fetch out_cursor INTO v_tname;
exit when out_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_tname);
END LOOP;
end;
/I get error:
[1]: (Error): ORA-00920: invalid relational operator ORA-06512: at "ABS.FORMS_TEXT_DYN_SQL_TEST", line 6 ORA-06512: at line 5Looks like you can only put column_name = :z, column_name = :y type values. You cannot it seems replace it with any WHERE CLAUSE???? -
How to create stored procedure to drop and create table
Version: Oracle 10g
I am trying to create a stored procedure that will drop and create a table based on a select statement. I can create the table but I can't drop it.
CREATE OR REPLACE procedure EC_LOAD is
begin
INSERT INTO Sales_table
(FSCL_WK,
DIV,
ACCT_TYPE)
Select
FSCL_WK,
DIV,
ACCT_TYPE
from
sales_revenue;
end ecload;
I need to drop Sales_table before inserting the values. How do i do this?Or with a drop:
SQL> desc emp2
ERROR:
ORA-04043: object emp2 does not exist
SQL>
SQL> declare
2 cnt int := 0;
3 begin
4 select max (1)
5 into cnt
6 from user_tables
7 where table_name = 'EMP2';
8
9 if cnt = 1
10 then
11 execute immediate 'drop table emp2';
12 end if;
13
14 execute immediate 'create table emp2 as select * from emp';
15 end;
16 /
PL/SQL procedure successfully completed.
SQL>
SQL> desc emp2
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> -
How to create Store Procedure for export result select in .txt or .csv
Hello,
I'm using MSSQL Server.
How to export results query in .txt or .csv?
But
I can not use bcp or SQLCMD.1) linked server
insert into txtsrv...table1#txt(CustomerNumber,CustomerName,EntryDate)
select orderid, shipname, orderdate
from Northwind..orders
Set up the linked server
Connecting a directory of text files as a linked server is easy. Here’s how:
In Windows, create a new directory for the linked server.
For this article, that directory will be E:\txtsvr.
Add that directory as a linked server with sp_addlinkedserver:
EXEC sp_addlinkedserver txtsrv, 'Jet 4.0',
'Microsoft.Jet.OLEDB.4.0', 'e:\txtsrv', NULL, 'Text'
That’s all that’s required to add the linked server txtsrv
as a connection to the text files in E:\txtsvr, but there are
no tables in txtsvr yet. Also missing is a required file, schema.ini,
which must contain a description of each table’s format.
You can learn more about schema.ini from the Microsoft MSDN Library,
but for now, just use Notepad or your favorite text editor
to create the file e:\txtsrv\schema.ini as shown in Listing 1.
In addition, create an empty text file in e:\txtsrv named table1.txt.
The file e:\txtsrv\table1.txt is now an empty table in the linked server txtsrv.
We can see that if we list information about all the tables in txtserv with
the system stored procedure sp_tables_ex:
EXEC sp_addlinkedsrvlogin txtsrv, FALSE, Admin, NULL
EXEC sp_tables_ex txtsrv
Text files are tables
We can’t issue a CREATE TABLE statement against this linked server,
but we have created a table easily enough. We can, however,
run INSERT and SELECT queries, if we use the four-part naming
convention <server>…<table> (that’s three dots):
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence
Maybe you are looking for
-
Can't find BonjourPS.msi file
When I try to install Bonjour in Windows to set up a printer, I get a message that says can't find this file. Anybody help? Thank you very much.
-
Oracle BPM 10.3 File Save Dialog Box
Hi, I am using Oracle BPM 10.3. Can anybody tell me that how to get a File Save Dialog box in the method. On the method call i want to call Save Dialog box which will save the file on the local computer. Thanks Edited by: user9293762 on Sep 2, 2010 9
-
ABAP/4 processor: POSTING_ILLEGAL_STATEMENT in WSM3
hi all, i am getting dump while doing listing in WSM3 stating 'Update was terminated '. the detailed message is as follows: Transaction.. WSM3 Update key... 4BCEF56BE790016000000000C0A80054 Generated.... 22.04.2010, 08:12:55 Completed.... 22.
-
Iphoto won't recognize iphone?
so i plug in my iphone 4s to iphoto so i can upload pictures, but instead of recognizing it as "my iphone" (that is the name of it) it comes up at "apple iphone" none of the photos show up there are just a bunch of squares with dotted lines around th
-
hi all, as per our business scenerio, here we want to calculate VAT including Frieght amount. sample calculation: Basic 200.00 ED 32.00 Cess 0.96 232.96 Frt 5.00 237.96 Vat 9.52 Gross Amt 247.48 how we