Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Thursday, April 14, 2016

Error When Using Distinct - ORA-01791: not a SELECTed expression

The Problem
I had a query that was working, but when I added a DISTINCT to it started throwing an error.  The error I was receiving was ORA-01791: not a SELECTed expression.  It didn't give me which field it didn't like, so I was confused.


The Solution
What I found was that once DISTINCT was in play all the fields in my ORDER BY had to be in the SELECT.  All of the ORDER BY fields had to be fields and not calculations.

Friday, March 18, 2016

How to insert an ampersand into an Oracle field


I was trying to insert a value into a varchar2 field.  The value contained an ampersand.  Something like "A&B Home Remodeling".  It kept giving me an error like &B was a variable.


What worked for me was storing it like this:
'A'||'&'||'B'


It like that much better.  I also saw suggestions to chr(38).  I did not try that, so I am not sure if it works, but I am guessing it would be very similar to what I did.  Something like:
'A'||chr(38)||'B'

Tuesday, November 10, 2015

Oracle Scheduled Jobs

TO CREATE A JOB:

Begin
 Dbms_scheduler.Create_job (
    Job_name => 'RUN_JOB_TO_RUN',
    Job_type => 'PLSQL_BLOCK',
    Job_action => 'BEGIN JOB_TO_RUN(); END;',
    Start_date => To_timestamp_tz('10-NOV-2015 13:00:00','DD-MM-YYYY HH24:MI:SS'),
    Repeat_interval => 'FREQ = DAILY; BYHOUR=13',
    comments => 'Some job to run.',
    Enabled => True);
End;


TO UPDATE A JOB:

begin
Dbms_scheduler.Set_attribute
(  name => 'RUN_JOB_TO_RUN'
,   Attribute =>'REPEAT_INTERVAL'
,   value =>'FREQ=DAILY; BYHOUR=2');
End;



Thursday, March 13, 2014

How to do Select Top in Oracle

In SQL Server I often do a SELECT TOP 10 FROM [TableName].

To get the bottom ten I found that I could ORDER BY DESC.

In Oracle things are a bit different. In Oracle they have a ROWNUM system field. To get the first ten records you just have to get the ROWNUMs that are 10 and below like the following:

SELECT * FROM [TableName] WHERE ROWNUM >= 11