Coding Blues
April 27, 2007
I have been doing designer(software design) work for the last few weeks and one of my job is to make those SQL queries. One of the things that I wanted to revise was the stored procedure to update the main table. The data will come from the temporary table. If the data is not present in the main table, insert it. Otherwise, make an update. It was implemented using cursors(yuck!). So I created the SQL queries which looked like:
update main set …..
from temp
where temp.pk = main.pk;
insert into main(…)
select …
from temp left join main on main.pk=temp.pk
where main.pk is null
When I showed it to the developer he said that update-from will not work in Oracle. WTF!?! MS SQL Server has it. PostgreSQL has it. I don’t know why Oracle can’t have this powerful SQL extension. One of the few things that I like with MS SQL’s T-SQL is that its easy to use. Even MS SQL Server 7 has a join keyword which makes your SQL easier to read. In Oracle at that time you need to use the + operator. I was thinking that since Oracle is the bigger company they should have a better SQL language.
I did revise the earlier query but its a bit uglier.
——————————————————————-
One error that I keep on seeing is the improper use of random number generators. If for example you have ten choices and you need to randomly choose between them you get a number from the random number generator then from that number determine your choice. You should have something like the code below:
int num = random.nextInt(10); //gives a number from 0 to 9
if(num == 0) {
//do something
} else if(num == 1) {
//do something
} ….
But no. What I usually found in other people’s code was:
if(random.nextInt(10) == 0) {
//do something
} else if(random.nextInt(10) == 1) {
//do something
}
——————————————————————-
One of the WTF questions that I heard a long time ago: “What is the basis of your randomization?”