23. October 2009 12:44

Generate n rows from dual

I needed to generate exactly n rows and then this is very handy.

select * from (
select level lvl
from dual
connect by level <= N
)

Found at: http://awads.net/wp/2005/07/01/generating-n-rows-from-dual/


Tags:  SQL  Oracle 
Comments
21. October 2009 16:27

ODBC Hell

It is so easy: I have a testing environement where an OCI8-Driver is installed. Therefore I can use the OCI-Function if PHP to Bind Variables, get return values and everything is just fine.

Then on the production I needed to switch to ODBC, and thought: Easy just write this stuff for ODBC and you’re done.

Unfortunately the driver does simply not support return values of functions. The problem is, that there are updates in my function, so I can’t solve the problem using:

select function_name(param1,param2,param3,...) from dual

After giving up my return value, I wanted to run the function like that (in TOAD this is no problem):

begin :retval := function_name(?,?,?,?); end;

Afterwards you are giving an array of parameter to the PHP function odbc_execute, and this should work alright.

I tried hundrets of possibilities to run my function. Finally I gave up, made a procdure (which has no return value and vor sure ODBC has no support for OUT or INOUT-Vars). I resigned to have a return value (which should only be the number of updated rows).

I tried: begin procedure_name(?,?,?,?); end;

There was no reaction. My last try:

call procedure_name(param1,param2,param3,..);

This woked! So I need to use “call” and not using the parameter-array of ODBC but giving it directly into the call. I’m happy to get it working, but this costs me one day.

Is there any really working ODBC-Driver for PHP?

I gave up.

PS: Yes I know, there is a solution for my return value. In my procedure I could write the value into a temporary table. But I rather want to use a good driver (like the OCI8) than using such a temporary solution. I’m open to new ways of doing this.


Tags:  Oracle  PHP  PL/SQL  SQL  Programming 
Comments
12. December 2006 00:00

SQL Start-Script generator

Sometimes it’s so simple:

import glob
files = glob.glob('*.*');
f=open('start_script.sql', 'w')
f.write('PROMPT This is an automatic generated start script\n');
f.write('PROMPT\n');
f.write('SPOOL start_script.LOG\n\n');
for filename in files:
    if filename == 'start_script.sql':
        continue
    f.write('PROMPT Starting script ' + filename + '\n');
    f.write('@@' + filename + '\n');
f.write('\nPROMPT\n');
f.write('PROMPT Finished.\n');
f.write('SPOOL OFF\n');
f.close();
exit();

Explanation:

First get a file list of the current directory (glob.glob(‘.’), this won’t return you directories since they don’t have an extension, if your files doesn’t have an extension, either: your fault). Then open a file called start_script.sql which will held our start-script (pretty self-explanatory). Then we generate a “@@” line for each file (this means if you have non-SQL-files in that directory you have to clean-up your script or edit it that it only takes .sql files (tip: glob.glob(‘*.sql’)). The start-script itself is excluded but the python script will be included (can easily be made excluded as well). After the loop the file gets closed and that’s it.


Tags:  Python  SQL  Programming 
Comments
7. November 2006 00:00

Delete duplicate records with Orcle

Don’t ask me why I needed to to that, but I had a lot (about 14 Mio.) duplicate records in a table.

DELETE FROM our_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM our_table
GROUP BY column1, column2, column3... ;

helped me out. (thanks to www.devx.com)

Next time I write my queries to filter those out before inserting them…


Tags:  Oracle  SQL 
Comments
18. October 2006 00:00

Need to generate a GUID with Oracle?

I just searched the web for this simple task, and it was difficult to find.

Maybe anyone working a bit with Oracle knows this, but it helped me, maybe I can help someone out there too, so here it is:

select sys_guid() from dual

This generates a 32 character GUID (fits best in a RAW(16) field).


Tags:  Oracle  SQL 
Comments