Convert sql select statement to oracle
Hi All,
Can anyone help me converting this Sql select statement to oracle ....
-----------------------------------------Query--------------------------------------------------------------
select emp_master.emp_code ,
emp_master.dept_cd ,
attendance_master.daily_attn_code ,
attendance_master.linked_column ,
case when location.payroll_status <> 'N' and eDocsNetEmployeesLeave.StartDate < dateadd(mm, 1 , convert(smalldatetime, datename(yy ,location.next_pay_date) + '/'+ datename(mm ,location.next_pay_date)+ '/01'))
then
dateadd(mm, 1 , convert(smalldatetime, datename(yy ,location.next_pay_date) + '/'+ datename(mm ,location.next_pay_date)+ '/01'))
when eDocsNetEmployeesLeave.StartDate < convert(smalldatetime, datename(yy ,location.next_pay_date) + '/'+ datename(mm ,location.next_pay_date)+ '/01')
then convert(smalldatetime, datename(yy ,location.next_pay_date) + '/'+ datename(mm ,location.next_pay_date)+ '/01') else eDocsNetEmployeesLeaveDetails.StartDate
end ,
eDocsNetEmployeesLeaveDetails.NoOfDays,
case when eDocsNetEmployeesLeave.StartDate > location.next_pay_date
then convert(datetime , convert(varchar, dateadd(ss,-1, dateadd(mm, 1, convert(datetime , datename(yy,eDocsNetEmployeesLeave.StartDate)+ '/' + datename(mm,eDocsNetEmployeesLeave.StartDate)+ '/01') )),106) )
else
case when location.payroll_status <> 'N'
then dateadd(mm,1,location.next_pay_date)
else location.next_pay_date
end
end as PaymentDate ,
isnull(grade_master.leave_type,'C') ,
eDocsNetEmployeesLeave.StartDate ,
eDocsNetEmployeesLeaveDetails.LeaveType
from eDocsNetEmployeesLeave ,
eDocsNetEmployeesLeaveDetails ,
eDocsNetLeaveTypes ,
emp_master ,
grade_master ,
attendance_master ,
location
where eDocsNetEmployeesLeaveDetails.RequestID = eDocsNetEmployeesLeave.RequestID and
eDocsNetEmployeesLeave.EmployeeID = emp_master.emp_code and
eDocsNetEmployeesLeaveDetails.LeaveType = eDocsNetLeaveTypes.LeaveTypeID and
eDocsNetLeaveTypes.loc_cd = emp_master.loc_cd and
location.loc_cd = emp_master.loc_cd and
attendance_master.loc_cd = emp_master.loc_cd and
attendance_master.linked_column = eDocsNetLeaveTypes.LinkedAttendance and
grade_master.loc_cd = emp_master.loc_cd and
grade_master.grade_cd = emp_master.grade_cd and
eDocsNetEmployeesLeaveDetails.RequestID = @RequestID
order by eDocsNetEmployeesLeaveDetails.StartDate
Thanks in Advance
Smiley
Seems like you want to convert a SQL statement from the ??? dialect to the Oracle dialect. *(It would be useful to indicate the non-ANSI standard SQL you are starting with.)
Part of the problem is that you use several date related functions. Some are unnecessary in Oracle and some need to translated into Oracle functions as found in the Functions section (chapter 5) of the SQL Reference manual at http://www.oracle.com/pls/db102/homepage
Note that columns and expressions of type 'date' in ORacle always contain all of "yyyy mm dd hh mi ss" and you need to format and trauncate as necessary.
Also note that '09-JAN-31' is NOT an Oracle date, but rather a character representation of a date which must be converted to/from a date expression. You will often need to use the to_date() and to_char() functions with a format mask as the second parameter. This is also descreibed in the first 2 chapters of the SQL Reference manual.
Similar Messages
-
Convert SQL (Select Statement) to Procedure
Hi
I am using a select sql for retrieving the results - Below is a sample sql select query.
select TableC.DATEFIELD as QUERY_DATE,
TableB.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT,
sum(TableC.COLUMN4) as OPEN_INSTANCES
from TableA, TableB, TableC
where TableB.COLUMN1 = TableA.COLUMN2
and TableA.COLUMN2 = TableC.COLUMN2
and DATEFIELD <= to_date('2011-02-02' ,'YYYY-MM-DD')
and DATEFIELD >= to_date('2011-02-02' ,'YYYY-MM-DD')
and TableC.COLUMN4 <= (24 * 3600 )
and TableB.COLUMN1 like 'PROCESSID'
and TableC.COLUMN1 in ('OSRCITR')
group by TableC.DATEFIELD as QUERY_DATE,
TableA.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT
I believe if we use a Procedure, It would be much faster. Is there any way that we can convert the above select sql to a procedure. If yes, how can it be.
Thanks in Advance.
-SreekantHi,
Try this...
CREATE OR REPLACE PROCEDURE testing_sample (
ov_d_query_date OUT DATE,
ov_v_process OUT VARCHAR2,
ov_v_product OUT VARCHAR2,
ov_v_open_instances VARCHAR2
AS
BEGIN
BEGIN
SELECT tablec.datefield, tableb.column1, tablec.column1,
SUM (tablec.column4)
INTO ov_d_query_date, ov_v_process, ov_v_product,
ov_v_open_instances
FROM tablea, tableb, tablec
WHERE tableb.column1 = tablea.column2
AND tablea.column2 = tablec.column2
AND datefield <= TO_DATE ('2011-02-02', 'YYYY-MM-DD')
AND datefield >= TO_DATE ('2011-02-02', 'YYYY-MM-DD')
AND tablec.column4 <= (24 * 3600)
AND tableb.column1 LIKE 'PROCESSID'
AND tablec.column1 IN ('OSRCITR')
GROUP BY tablec.datefield, tablea.column1, tablec.column1;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN OTHERS
THEN
NULL;
END;
END;
Note: -In Exceptions if you wanna catch you can do.
Regards,
Simma..... -
How to convert simple SQL Select statements into Stored Procedures?
Hi,
How can I convert following SELECT statement into a Stored Procedure?
SELECT a.empno, b.deptno
FROM emp a, dept b
WHERE a.deptno=b.deptno;
Thanking in advance.
Wajidstored procedure is nothing but a named PL/SQL block
so you can do it like this see below example
SQL> create or replace procedure emp_details is
2 cursor c1 is SELECT a.empno, b.deptno
3 FROM scott.emp a, scott.dept b
4 WHERE a.deptno=b.deptno;
5 begin for c2 in c1
6 LOOP
7 dbms_output.put_line('name is '||c2.empno);
8 dbms_output.put_line('deptno is ' ||c2.deptno);
9 END LOOP;
10 END;
11 /
Procedure created.and to call it use like below
SQL> begin
2 emp_details;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on;
SQL> /
empno is 7839
deptno is 10
empno is 7698
deptno is 30
empno is 7782
deptno is 10
empno is 7566
deptno is 20
empno is 7654
deptno is 30
empno is 7499
deptno is 30
empno is 7844
deptno is 30
empno is 7900
deptno is 30
empno is 7521
deptno is 30
empno is 7902
deptno is 20
empno is 7369
deptno is 20
empno is 7788
deptno is 20
empno is 7876
deptno is 20
empno is 7934
deptno is 10Edited by: Qwerty on Sep 17, 2009 8:37 PM -
Jdev9i, JSP, Execute a Sql Select statement directly?
Hi,
I need the syntax to execute a Sql SELECT statement directly against Oracle database and access rows(returned by SELECT statemnet) in a JSP page or Servlet? Any sample code available for this.
Thanks.Hi Ali,
I didn't work a lot with jsp ,but on this url some help .
http://otn.oracle.com/docs/products/oracle8i/doc_library/817_doc/java.817/a83726/basics7.htm#1014578 -
Exporting SQL Select statement to spreadsheet
Hi all, basic question that I couldn't find the answer to while googling and searching the Oracle documentation.
What is the code for exporting the result of a SQL Select statement to a spreadsheet such as Excel or Calc? In Oracle SQL Developer I tried saving the output as an XML file but it did not come out properly and I tried using EXPORT at the end of the statement but it gave an error message and I am not sure if this is the right command or what the syntax is. Thanks.Here's an example of one way to create CSV files that most spreadsheet type applications are able to read...
As sys user:
CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
/As myuser:
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
,p_dir IN VARCHAR2
,p_header_file IN VARCHAR2
,p_data_file IN VARCHAR2 := NULL) IS
v_finaltxt VARCHAR2(4000);
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_fh UTL_FILE.FILE_TYPE;
v_samefile BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
d := DBMS_SQL.EXECUTE(c);
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
END CASE;
END LOOP;
-- This part outputs the HEADER
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
FOR j in 1..col_cnt
LOOP
v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
IF NOT v_samefile THEN
UTL_FILE.FCLOSE(v_fh);
END IF;
-- This part outputs the DATA
IF NOT v_samefile THEN
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
END IF;
LOOP
v_ret := DBMS_SQL.FETCH_ROWS(c);
EXIT WHEN v_ret = 0;
v_finaltxt := NULL;
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
ELSE
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
END CASE;
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
END LOOP;
UTL_FILE.FCLOSE(v_fh);
DBMS_SQL.CLOSE_CURSOR(c);
END;This allows for the header row and the data to be written to seperate files if required.
e.g.
SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
PL/SQL procedure successfully completed.Output.txt file contains:
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10The procedure allows for the header and data to go to seperate files if required. Just specifying the "header" filename will put the header and data in the one file.
Adapt to output different datatypes and styles are required.
There are other methods for specifically creating Excel workbooks with multiple sheets etc. and details can be found in the SQL and PL/SQL FAQ at the top of the forum:
{message:id=9360007} -
Javascript and sql select statement
I am working in html db in the 10 g environment. I'm trying to create an "auto suggestion function" that predicts values as the user enters characters into a text field. I've gotten it to work by hard coding the values in the function, ie: states. btw: there are several external javascript files at work here.
What I want is to create a dynamic link to the database to gather, say, all names of providers at the time the page is built to use in the auto suggest format. I want to put this into a javascript routine and I'm having problems finding out how to mix the sql select statement into the javascript function.
For the states example, I'm using:
function StateSuggestions() {
this.states = [
"Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut",..."Wyoming" ];
Can I substitute the hard coded data (states) with a sql select statement such
as:
select name from <dbtable> ???
How does this need to be "wrapped" or containerized?
Thank you. You have a great product in html db and your site is very useful. I appreciate everything you have done to assist us. Again, thank you.Hi,
You can use TRUNC with 2 arguments to get the first DATE in a month, year, quarter, week, hour, minute, ISO year, ...
SELECT TRUNC ( ADD_MONTHS ( SYSDATE
, 12
, 'MONTH'
) AS first_of_month
FROM dual
;The DATE returned will be in the same month, year, quearter, ... as the first argument.
\We convered the last day of the month in [your previous question|http://forums.oracle.com/forums/message.jspa?messageID=3942939#3942939].
At that time, I warded about using LAST_DAY as a cutoff point; TRUNC is a much better way.
For example, to find all appointment_dates in the current month next year:
SELECT *
FROM appointments
WHERE appointment_date >= TRUNC (ADD_MONTHS (SYSDATE, 12), 'MONTH')
AND appointment_date < TRUNC (ADD_MONTHS (SYSDATE, 13), 'MONTH')Note that
the first part of the WHERE clause calls for dates on or equal to the beginning of the 12th month in the future, but
the second part of the WHERE clause calls for dates before, not equal to , the beginning of the 13th month in the future. -
Parsing an OPEN SQL select statement
Hi all,
I want to parse an open SQL select statement to get the list of tables and the corresponding fields in the select statement.
Is there some ABAP-internal syntax from the SAP Database Interface that does this? or is the only option to do this is to manually break the statement?
Thanks.
GirishHi,
I doubt if there is something like that available to use. It think you will have to write a parser for yourself.
Regards -
Using column number inplace of column name in SQL Select statement
Is there a way to run sql select statements with column numbers in
place of column names?
Current SQL
select AddressId,Name,City from AddressIs this possible
select 1,2,5 from AddressThanks in Advanceuser10962462 wrote:
well, ok, it's not possible with SQL, but how about PL/SQL?As mentioned, using DBMS_SQL you can only really use positional notation... and you can also use those positions to get the other information such as what the column is called, what it's datatype is etc.
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2) IS
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_rowcount NUMBER := 0;
BEGIN
-- create a cursor
c := DBMS_SQL.OPEN_CURSOR;
-- parse the SQL statement into the cursor
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
-- execute the cursor
d := DBMS_SQL.EXECUTE(c);
-- Describe the columns returned by the SQL statement
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
-- Bind local return variables to the various columns based on their types
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000); -- Varchar2
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val); -- Number
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val); -- Date
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000); -- Any other type return as varchar2
END CASE;
END LOOP;
-- Display what columns are being returned...
DBMS_OUTPUT.PUT_LINE('-- Columns --');
FOR j in 1..col_cnt
LOOP
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' - '||case rec_tab(j).col_type when 1 then 'VARCHAR2'
when 2 then 'NUMBER'
when 12 then 'DATE'
else 'Other' end);
END LOOP;
DBMS_OUTPUT.PUT_LINE('-------------');
-- This part outputs the DATA
LOOP
-- Fetch a row of data through the cursor
v_ret := DBMS_SQL.FETCH_ROWS(c);
-- Exit when no more rows
EXIT WHEN v_ret = 0;
v_rowcount := v_rowcount + 1;
DBMS_OUTPUT.PUT_LINE('Row: '||v_rowcount);
DBMS_OUTPUT.PUT_LINE('--------------');
-- Fetch the value of each column from the row
FOR j in 1..col_cnt
LOOP
-- Fetch each column into the correct data type based on the description of the column
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_n_val);
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'));
ELSE
DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
DBMS_OUTPUT.PUT_LINE(rec_tab(j).col_name||' : '||v_v_val);
END CASE;
END LOOP;
DBMS_OUTPUT.PUT_LINE('--------------');
END LOOP;
-- Close the cursor now we have finished with it
DBMS_SQL.CLOSE_CURSOR(c);
END;
SQL> exec run_query('select empno, ename, deptno, sal from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
DEPTNO - NUMBER
SAL - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
DEPTNO : 10
SAL : 2450
Row: 2
EMPNO : 7839
ENAME : KING
DEPTNO : 10
SAL : 5000
Row: 3
EMPNO : 7934
ENAME : MILLER
DEPTNO : 10
SAL : 1300
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from emp where deptno = 10');
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
JOB - VARCHAR2
MGR - NUMBER
HIREDATE - DATE
SAL - NUMBER
COMM - NUMBER
DEPTNO - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
JOB : MANAGER
MGR : 7839
HIREDATE : 09/06/1981 00:00:00
SAL : 2450
COMM :
DEPTNO : 10
Row: 2
EMPNO : 7839
ENAME : KING
JOB : PRESIDENT
MGR :
HIREDATE : 17/11/1981 00:00:00
SAL : 5000
COMM :
DEPTNO : 10
Row: 3
EMPNO : 7934
ENAME : MILLER
JOB : CLERK
MGR : 7782
HIREDATE : 23/01/1982 00:00:00
SAL : 1300
COMM :
DEPTNO : 10
PL/SQL procedure successfully completed.
SQL> exec run_query('select * from dept where deptno = 10');
-- Columns --
DEPTNO - NUMBER
DNAME - VARCHAR2
LOC - VARCHAR2
Row: 1
DEPTNO : 10
DNAME : ACCOUNTING
LOC : NEW YORK
PL/SQL procedure successfully completed.
SQL> -
Can we convert a SQL (Select Statement) to Procedure.?
Hi
I am using a select sql for retrieving the results - Below is a sample sql select query.
select TableC.DATEFIELD as QUERY_DATE,
TableB.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT,
sum(TableC.COLUMN4) as OPEN_INSTANCES
from TableA, TableB, TableC
where TableB.COLUMN1 = TableA.COLUMN2
and TableA.COLUMN2 = TableC.COLUMN2
and DATEFIELD <= to_date('2011-02-02' ,'YYYY-MM-DD')
and DATEFIELD >= to_date('2011-02-02' ,'YYYY-MM-DD')
and TableC.COLUMN4 <= (24 * 3600 )
and TableB.COLUMN1 like 'PROCESSID'
and TableC.COLUMN1 in ('OSRCITR')
group by TableC.DATEFIELD as QUERY_DATE,
TableA.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT
I believe if we use a Procedure, It would be much faster. Is there any way that we can convert the above select sql to a procedure. If yes, how can it be.
Thanks in Advance.
-SreekantSreekant wrote:
select TableC.DATEFIELD as QUERY_DATE,
TableB.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT,
sum(TableC.COLUMN4) as OPEN_INSTANCES
from TableA, TableB, TableC
where TableB.COLUMN1 = TableA.COLUMN2
and TableA.COLUMN2 = TableC.COLUMN2
and DATEFIELD <= to_date('2011-02-02' ,'YYYY-MM-DD')
and DATEFIELD >= to_date('2011-02-02' ,'YYYY-MM-DD')
and TableC.COLUMN4 <= (24 * 3600 )
and TableB.COLUMN1 like 'PROCESSID'
and TableC.COLUMN1 in ('OSRCITR')
group by TableC.DATEFIELD as QUERY_DATE,
TableA.COLUMN1 PROCESS,
TableC.COLUMN1 PRODUCT
I believe if we use a Procedure, It would be much faster. Is there any way that we can convert the above select sql to a procedure. If yes, how can it be.Using the code tags would make the query easier to read :)
What version of Oracle are you on?
Under the right conditions deconstructing a huge query into smaller components sometimes can offer performance increases, but this is more true of older versions of Oracle than recent ones. Lately I get better results from tuning queries in place - as Aman pointed out you introduce context switching (moving between the SQL and PL/SQL engines to do work) which can also hurt performance.
Try tuning the query first. Get an execution plan. Things you can look for include
* make sure the driving table is the best one
* are the join columns properly indexed? Are existing indexes being suppressed due to the functions?
Is "and TableB.COLUMN1 like 'PROCESSID' " correct? without a wildcard LIKE should evalate to = -
Query regarding Oracle SQL select statement
Hi,
How does Oracle ensures a consistent snapshot with the select statement
without locking the table. My question is summarized with an example below:
1. At time T1, Oracle select statement is fired and is fetching the result.
2. At time T2, some DML operation is performed on the same table on which
the select was executed and a commit was fired.
3. At time T3, The Oracle select statement (Step 1) completes.
My question is whether the records of transaction at time T2 will be visible at time T3 or not?
If "not", then does it mean that Oracle retrieves the rows from the time of last commit.
I would also like to know if for the above mechanism, Oracle would make use of the rollback segemt to access the rows at a particular instant of time.
TIA
Regards,
RajThis is called Read Consistency in the oracle. Its all about SCN before starting the transaction.
Lets say
1. T1 executs SELECT statement on EMP table.
2. T2 made an update on the EMP and commited.
But, T1 only still sees only old image of the data not the new one. This is called read consistency.
You will be having two images in the buffer, one is consistent and changed image.
When the T1 give SELECT statement it notes the SCN of the transaction. Read oracle document about read consistency.
SJH. -
ORA-01841 Converting SQL Server DateTime to Oracle Date
I have Oracle 11gR2 x64 running on my Win7 Enterprise x64 personal system. I'm trying to convert the TSQL to create a database in SQL Server 2008 Express for my C# class to Oracle, and I've run into a "interesting" problem loading date data.
The SQL Server 2008 TSQL InvoiceDate column in the Invoices table has datatype DATETIME:
*[InvoiceDate] [datetime] NOT NULL,*
which Oracle does not support. The Oracle InvoiceDate column in the MMAB_Invoices table has datatype DATE:
InvoiceDate DATE NOT NULL,
The fun begins with the TSQL INSERT statements (for example):
INSERT [dbo].[Invoices] ([InvoiceID], [CustomerID], [InvoiceDate], [ProductTotal], [SalesTax], [Shipping], [InvoiceTotal]) VALUES (18, 20, CAST(0x00009CFD00000000 AS DateTime), 151.0000, 11.3300, 6.2500, 168.5800)
As you can see, whoever wrote the load script thought it would be totally awesome to convert hex (binary?) data to generate a date, instead of using data readable by a human being.
BTW, the date generated is 2010-01-13 00:00:00.000.
After Reading The Fabulous Manual and searching the OTN Discussion Fora, what I came up with is
INSERT INTO MMAB_Invoices (CustomerID, InvoiceDate, ProductTotal, SalesTax, Shipping, InvoiceTotal) VALUES (20, TO_DATE(UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('00009CFD00000000')), 'YYYY-MM-DD HH:MI:SS'), 151.0000, 11.3300, 6.2500, 168.5800);
which returned
Error starting at line 846 in command:
INSERT INTO MMAB_Invoices (CustomerID, InvoiceDate, ProductTotal, SalesTax, Shipping, InvoiceTotal) VALUES (20, TO_DATE(UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('00009CFD00000000')), 'YYYY-MM-DD HH:MI:SS'), 151.0000, 11.3300, 6.2500, 168.5800)
Error report:
SQL Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0
*01841. 00000 - "(full) year must be between -4713 and +9999, and not be 0"*
**Cause: Illegal year entered*
**Action: Input year in the specified range*
The PK InvoiceID is automatically generated by a BEFORE INSERT trigger that uses a sequence.
Suggestions?
Thanks.
P.S. Happy New Year!!!Your hex conversion is certainly not returning a date format:
sql> select UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('00009CFD00000000')) from dual;
UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('00009CFD00000000'))
┐²To convert the hex number to decimal, simply use to_number:
sql> select to_number('00009CFD00000000', 'xxxxxxxxxxxxxxxx') from dual;
TO_NUMBER('00009CFD00000000','XXXXXXXXXXXXXXXX')
1.7261E+14Now, before you can make any further calculations with that number, you need to know how it represents the date of 2010-01-13. Luckily, we've got Google nowadays. ;-)
Sql server representation seems to be an 8 byte field. The first 4 bytes stores the number of days since SQL Server's epoch (1st Jan 1900). The second 4 bytes stores the number of milliseconds after midnight.
sql> select to_number('00009CFD', 'xxxxxxxxxxxxxxxx') from dual;
TO_NUMBER('00009CFD','XXXXXXXXXXXXXXXX')
40189
sql> select to_date('1900-01-01','yyyy-mm-dd') + 40189 from dual;
TO_DATE('
13-JAN-10So, the conversion would be something like this:
sql> select to_date('1900-01-01','yyyy-mm-dd') + to_number(substr('00009CFD00000000',1,8), 'xxxxxxxx') + to_number(substr('00009CFD00000000',9,8), 'xxxxxxxx') / (24*60*60*1000) from dual;
TO_DATE('
13-JAN-10Note: Oracle DATE format specifies a datetime up to the second, whereas the sqlserver DATETIME format specifies a datetime up to the millisecond; if that precision is needed, use Oracle's TIMESTAMP instead.
Note: using an internal representation is risky, implementation details may change. In this case, it seems pretty unlikely that Microsoft will ever change the datetime internal representation, since that would break lots and lots of code.
Edited by: theoa on 2-jan-2012 9:14 -
Convert SQL SERVER DATETIME into ORACLE TIMESTAMP
I have to convert a column's datatype.
The source column's datatype is DATETIME which is in SQL Server to TIMESTAMP(datatype in target which is oracle)
Can anyone help me pleaseJust issue ALTER TABLE MODIFY:
SQL> create table tbl(c date)
2 /
Table created.
SQL> insert into tbl values(sysdate)
2 /
1 row created.
SQL> select to_char(c,'mm/dd/yyyy hh24:mi:ss') from tbl
2 /
TO_CHAR(C,'MM/DD/YY
12/26/2009 20:50:58
SQL> alter table tbl modify c timestamp
2 /
Table altered.
SQL> select c from tbl
2 /
C
26-DEC-09 08.50.58.000000 PM
SQL> SY. -
Retrieve floating point data with an sql select statement
Hi
I'm quite new to using sql but I have a system working where I can read and write strings to an access database.
Now I want to retrieve a a float, from a field where another field in the same post corresponds to a specified float, with a select statement.
When using strings I wrote
SELECT column_name FROM [table_name] WHERE column2_name='value'
in my query.
But instead of getting the desired value I get an error message telling me that I have a
"Data type mismatch in criteria expression".
I think I understand why but does anybody know what I should have written instead?Is the data type of column2_name String? If it's not, I think the single-quotes you have around 'value' will cause that error.
Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice -
Hi frenz, i m bharath a new bee to oracle,my problem is
the given table is
userid billno amount
1 101 1000
2 102 2000
3 103 3000
4 104 4000
output table should be:
userid billno amount total*
1 101 1000
2 102 2000 *3000*
3 103 3000 *6000*
4 104 4000 *10000*
I need a select query for the above output.
i.e,here i calculated total as sum(amount) for each row,i.e,3000 = 1000+2000,6000 = 1000+2000+3000,10000 = 1000+2000+3000+4000 by this way my calculation for total field
kindly send me a query.This is trivial with analytics:
SQL> select empno, sal, sum(sal) over (order by empno)
2 from emp
3 /
EMPNO SAL SUM(SAL)OVER(ORDERBYEMPNO)
7369 800 800
7499 1600 2400
7521 1250 3650
7566 2975 6625
7654 1250 7875
7698 2850 10725
7782 2450 13175
7788 3000 16175
7839 5000 21175
7844 1500 22675
7876 1100 23775
7900 950 24725
7902 3000 27725
7934 1300 29025
14 rows selected.
SQL> You might want to get your keyboard checked - the question mark key seems to be stuck.
Cheers, APC
blog: http://radiofreetooting.blogspot.com -
I use LV7.1.1 to read/update an Access Database, using DB Toolset.
When I use the Sql 'SELECT' query with LIKE operator, I do not get any results. (no error is returned).
The statement is:
SELECT Machine, System, AD, Query_Position, RefDesignator, Description
FROM AllLevel
WHERE ((Machine LIKE '*') AND (System LIKE '*') AND (AD ='2000CV20012') AND (Query_Position ='AD') AND (RefDesignator='u1'))
It seems that the LIKE '*' is not translated well by the 'Microsoft Jet 4.0 OLE DB Provider'. I must mention that the statement works just fine under Access, not using LV.
Is there any alternative format for LIKE '*' that do the job?
Regards,
Joseph MosheMaybe Access has different syntax, but for T-SQL, the proper syntax for LIKE is to use the % character to match any string of zero or more characters and the _ character to match a single characer. In fact, the documentation says that while * and ? are understood,
"The % and _ (underscore) wildcard characters should be used only
through the Jet OLE DB provider and ActiveX® Data Objects (ADO) code.
They will be treated as literal characters if they are used though the
Access SQL View user interface or Data Access Objects (DAO) code."
Maybe you are looking for
-
Need to call FM after the updation of the table
Hi Experts, In the user exit 'Exit_sapllmde_006' of l_idoc_input_wmmbxy am using function module WFMC_MESSAGE_SINGLE for triggering orders05 idoc. The function module l_idoc_input_wmmbxy updates the table EKBE. But this update takes place only a
-
Paid Invoices - Status showing as 'Open'
Hi Experts, Have you had a situation in SBO 05 where the an invoice has been paid but the status shows Open? This is a very weird problem I am having. The BP's recon does not pick up these 'Open' Invoices at all, and the BP's balance is correct! Thes
-
How to get detail technical information?
Dear All Kindly let me know from where i can get detail technical and business information for implementing MES at any manufacturing client. Thanks and Regards
-
Any Function Module or Program Exist ?
Hi Gurus, Can anyone please let me know is there any FM or any program for the below kind of requirement ? Let us say I have a Text ( change dynamically ) with 150 charecters length. However It is split into 3 parts of each 50. I would like to have o
-
Cannot download Flash Player v10
My computer says I don't have the latest version of Flash Player. When I try to download it from the adobe web site, there is a delay, the yellow band with the message to click on the yellow band doesn't come. Then I'm kicked over to a screen in in