SQLTeam.com | Weblogs | Forums

Hello, can you help me improve my sql code to be better?

SELECT distinct *
from pod p
inner join klas k on (p.gooo = k.gooo)
inner join adr a on (p.wooo = a.wooo)
inner join obow o on (p.sooo = o.sooo) inner join urz us on (o.para = us.para)
WHERE 1=1
and ((:good = 1
and p.name = :name
and p.surname = :surname
and p.name2 = :name2
and a.miejscowosc = :Miejscowosc
and a.street = :street)
OR (:good = 0
AND p.name2 like :name2
and p.name like :name
and a.vilege like :vilege
and a.street like :street))
AND a.change = :change
AND a.house = :house

How can I change the code to be better?

You'll need to define "better". What are you trying to improve?

Also, SQLTeam is a Microsoft SQL Server site, the code you posted does not match its syntax.

DB2 maybe?

oracle

just to give you an idea

SELECT 
    distinct *
from 
(select * from pod where and p.name = :name and p.surname = :surname and p.name2 = :name2) p
(select * from pod  where p.name2 like :name2 and p.name like :name ) q
(select * from adr where and a.miejscowosc = :Miejscowosc and a.street = :street and :good = 1 and a.change = :change and a.house = :house ) a
( select * from adr where and a.miejscowosc = :Miejscowosc and a.street = :street and :good = 1 and a.change = :change and a.house = :house ) b 
	   inner join obow o on (p.sooo = o.sooo) 
	   inner join urz us on (o.para = us.para)
      inner join klas k on (p.gooo = k.gooo)