| Author |
Topic |
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 14:40:31
|
HiI have inherited some legacy code and reading the code it looks like its more of copy paste something likeif some_conditionbeginselect col1, col2from table t1inner join t2on t1.col1=t2.col2else if some_other_conditionselect col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5elseif some_other_conditionselect col1, col2from table t1inner join t2on t1.col1=t2.col2where col2=5.. is there way i can refactor this code and no copy paste the same sql 10 times but write it once and keep on appending the conditions for each of the case ?does my query make senseregardsHrishy |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-20 : 14:44:52
|
| [code]select col1, col2from table t1inner join t2on t1.col1=t2.col2where (col1=5 and condition2)or (col2=5 and condition3)or condition1[/code] |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 15:58:10
|
HiThanks for your reply.But the condition is something that may not be matched with the join something likeif day='Monday'beginselect col1, col2from table t1inner join t2on t1.col1=t2.col2else if day='Tuesday'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5elseif day='Wednesday'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col2=5 not the if conditions may not be present in the table regardshrishy |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-20 : 16:07:19
|
| Do you have tried Visa's solution?In the WHERE-Clause you can use conditions no matter when they're not present in the table.For example:select col1 from table1 where 1=1 and 'yak' = 'yak'WebfredPlanning replaces chance by mistake |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 16:11:39
|
HiI am sorry i am not sure to use this condition in a where clauseselect col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5and day='Monday' --I think sql wouldnt compile here regardshrishy |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-10-20 : 16:17:32
|
| If you are talking about current day you are correct. But you can use DATEPART(dw, GETDATE()) Will return a numeric value for the Day of the Week that you can use.from BOLThe weekday (dw) datepart returns a number that corresponds to the day of the week, for example: Sunday = 1, Saturday = 7. The number produced by the weekday datepart depends on the value set by SET DATEFIRST, which sets the first day of the week."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-20 : 16:19:28
|
do you have a column called 'day' ? Or are you trying to check if the current day is Monday if so. Instead of and day='Monday' try and datename(dw, getdate())='Monday' |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 16:20:47
|
| HiThanks again for looking into this.I used day as just an example it can be anythingif Country='USA'beginselect col1, col2from table t1inner join t2on t1.col1=t2.col2else if Country='Canada'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5elseif Country='Sweden'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col2=5.....[/code]regardsHrishy |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 16:21:32
|
| HiNo i dont have a column called dayregardsHrishy |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-10-20 : 16:26:02
|
| Then try posting your actual code so we can see what you are trying to do and maybe we can come up with a way to do it."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-20 : 16:27:02
|
quote: Originally posted by hrishy HiI am sorry i am not sure to use this condition in a where clauseselect col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5and day='Monday' --I think sql wouldnt compile here regardshrishy
Hello hrishy,coming from here:quote: if some_conditionbeginselect col1, col2from table t1inner join t2on t1.col1=t2.col2else ...
There was no reason to think "some_condition" looks like anything that is a problem for sql because what you have posted was SQL-Code wasn't it?Planning replaces chance by mistake |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-20 : 16:34:16
|
[code]select col1, col2from table t1inner join t2on t1.col1=t2.col2where where1 = case when datename(dw, getdate()) = 'Monday' then 1 else case when datename(dw, getdate())= 'Tuesday' and col1 = 5 then 1 else case when datename(dw, getdate())= 'Wednesday' and col2 = 5 then 1 else 0 end end end[/code]quote: Originally posted by hrishy HiThanks for your reply.But the condition is something that may not be matched with the join something like[code]if day='Monday'beginselect col1, col2from table t1inner join t2on t1.col1=t2.col2else if day='Tuesday'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col1=5elseif day='Wednesday'select col1, col2from table t1inner join t2on t1.col1=t2.col2where col2=5[/code]not the if conditions may not be present in the table regardshrishy
|
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-20 : 16:38:48
|
quote: Originally posted by jhocutt Then try posting your actual code so we can see what you are trying to do and maybe we can come up with a way to do it."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
agree, we are getting paid $50/hr to solve your problems you know... |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-20 : 16:43:30
|
| Oh!Then I have to economize...Planning replaces chance by mistake |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 17:16:19
|
| HiOhh no please i appreciate your help very much.Its just that when i get paid at the end of the month i need to give my salarys worth to my company thats why i wanted to refactor the code.I will post the code tomorrow after i simplify and obfuscate the coderegardsHrishy |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-10-20 : 17:23:34
|
| If you can simplify it then you dont need us.Is there something in this code that can not be posted?Like hard coded SSNs or something?"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-20 : 17:26:46
|
| HiWell if i simplify it and obfuscate the names then it will be easier for you guys to help me i think.I am not in office now so dont have acesses to source.regardsHrishy |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-22 : 04:24:44
|
HiHere is a very simple t-sql that needs to be refactored.If [@empid]='ALL'--Select all employeesselect empidfrom employeeselse--if only one particular employee needs to be selectedselect empidfrom employeeswhere empid=@empid There are other variants of this likeIf [@empid]='ALL'--Select all employeesselect empid,ename,bankaccountfrom employeeselse--if only one particular employee needs to be selectedselect empid,ename,dob,ssnfrom employeeswhere empid=@empid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-22 : 04:31:32
|
it can be written simply like thisselect empid,ename,dob,ssnfrom employeeswhere empid=@empid or @empid='ALL' |
 |
|
|
hrishy
Starting Member
47 Posts |
Posted - 2008-10-23 : 04:01:06
|
HiVishak thanks for your reply Is there a way to write something like thisselect empid,ename,dob,ssnfrom employeeswhere empid=CASE WHEN @empid='ALL' THEN '%' ELSE @empid But i dont know what to write in the part where there THEN '%'regardsHrishy |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-23 : 04:14:03
|
| why use %? what was problem with soln i gave? |
 |
|
|
Next Page
|