| Author |
Topic |
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-11 : 21:20:13
|
| Hi all. what is wrong with my query?DECLARE @empDcno varchar(20), @secnum intset @empDcno = '205'set @secnum = 1select l.* , e.paytypefrom hrempleaveledger as linner join hremployees as e on l.empdcno = e.empdcnoinner join hrsetbenefitsleave as b on l.type = b.typeinner join mscsections as s on e.sectioncode = s.sectioncodeinner join mscdivisions as d on e.divisioncode = d.divisioncodeinner join mscbranches as br on e.branchcode = br.branchcodeinner join CSVTable(@empDcno) as R on e.empdcno = R.stringvalwhere e.sectioncode =case @secnumwhen 1 then '1001'when 2 then '1002'else ''enddid i use the case/when statement properly? if not, how could i query this in correct format?Edit .:. this is my changes. still not working... Edit .:. Ok got it...thanks. @secnum instead.Thanks.-Ron- |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 21:38:43
|
[code]WHERE e.sectioncode =case secnumwhen 1 then where e.sectioncode = '1001'when 2 then where e.sectioncode = '1002'end[/code] KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 21:40:40
|
Why don't you refer to the Books OnLine for the correct syntax ?quote: SyntaxSimple CASE function:CASE input_expression WHEN when_expression THEN result_expression [ ...n ] [ ELSE else_result_expression ] END
KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-11 : 22:49:04
|
| Hi again. I tried all my best to come up with the correct output, but all my trials failed. I would like to ask help again. thanks.DECLARE @empDcno varchar(20), @secnum int, @allsections varchar(max)set @empDcno = '205,14'set @secnum = 4set @allsections = '1001, 1002, 1003'select l.* , e.paytype, e.sectioncodefrom hrempleaveledger as linner join hremployees as e on l.empdcno = e.empdcnoinner join hrsetbenefitsleave as b on l.type = b.typeinner join mscsections as s on e.sectioncode = s.sectioncodeinner join mscdivisions as d on e.divisioncode = d.divisioncodeinner join mscbranches as br on e.branchcode = br.branchcodeinner join CSVTable(@empDcno) as R on e.empdcno = R.stringvalwhere e.sectioncode =case @secnumwhen 1 then '1001'when 2 then '1002'when 3 then '1003'else endIn Else (If secnum == 4) i want to use it the CSVTable, wherein where e.sectioncode = @allsections. Thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 23:06:17
|
you mean when @secnum = 4, you want a where condition like this ?e.sectioncode in ('1001', '1002', '1003') KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-11 : 23:15:43
|
| yes, with the use of CSVTable if possible.thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 23:22:53
|
Then why not standardize and use CSVTable instead of case when end with @secnumselect l.* , e.paytype, e.sectioncodefrom hrempleaveledger as linner join hremployees as e on l.empdcno = e.empdcnoinner join hrsetbenefitsleave as b on l.type = b.typeinner join mscsections as s on e.sectioncode = s.sectioncodeinner join mscdivisions as d on e.divisioncode = d.divisioncodeinner join mscbranches as br on e.branchcode = br.branchcodeinner join CSVTable(@empDcno) as R on e.empdcno = R.stringvalinner join CSVTable(@allsections) as sc on e.sectioncode = sc.stringval assign '1001' to @allsections when @secnum is 1assign '1002' to @allsections when @secnum is 2 etc and assign '1001,1002,1003' to @allsections when @secnum is 4 KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-11 : 23:35:09
|
Damn. im dumbass.lol. I made my query so complicated.I thought i can't inner join 2 CSVTable. Oh well i got it....my front end passes set of sections to the variable @getallsections.DECLARE @empDcno varchar(20), @secnum int, @allsections varchar(max)set @empDcno = '205,14'set @secnum = 1set @allsections = @getallsectionsselect l.* , e.paytype, e.sectioncodefrom hrempleaveledger as linner join hremployees as e on l.empdcno = e.empdcnoinner join hrsetbenefitsleave as b on l.type = b.typeinner join mscsections as s on e.sectioncode = s.sectioncodeinner join mscdivisions as d on e.divisioncode = d.divisioncodeinner join mscbranches as br on e.branchcode = br.branchcodeinner join CSVTable(@empDcno) as R on e.empdcno = R.stringvalinner join CSVTable(@allsections) as sc on e.sectioncode = sc.stringvalThanks for the guidance KH.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 23:36:59
|
then you can remove the @secnum from your SP. KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-14 : 21:42:59
|
| Hi.Is it possible to make a CASE/WHEN function in JOINS?This is my query:DECLARE @count1 int, @empDcno varchar(10)set @empDcno = '25,100'set @count1 = 0select l.* , e.paytype, e.sectioncode, e.empno,dc.fullname,e.hrswork,earnedDays = lc.earned, availedDays = lc.availed,earnedHours = (lc.earned * e.hrswork), availedHours = (lc.availed * e.hrswork),balHours = (lc.earned * e.hrswork) - (lc.availed * e.hrswork),balDays = (lc.earned - lc.availed)from hrempleaveledger as linner join hremployees as e on l.empdcno = e.empdcnoinner join psdatacenter as dc on dc.dcno = l.empdcnoinner join hrempleavecredits as lc on lc.empdcno = e.empdcnoinner join hrsetbenefitsleave as ben on l.type = ben.typeinner join mscsections as s on e.sectioncode = s.sectioncodeinner join mscdivisions as d on e.divisioncode = d.divisioncodeinner join mscbranches as br on e.branchcode = br.branchcodeinner join mscwarehouse as w on e.whcode = w.whcodecase count1 when 1 then inner join CSVTable(@empDcno) as R on e.empdcno = R.stringvalelse 0endthis is the error:Msg 156, Level 15, State 1, Line 33Incorrect syntax near the keyword 'case'.Thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-14 : 21:46:10
|
No. You can't KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-14 : 21:51:17
|
| thanks khtan. if not possible, can you give me suggestion or any suggestions to make a DYNAMIC JOIN... thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-14 : 21:56:16
|
you can consider using IF .. ELSE block KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-14 : 22:00:02
|
| You said before that it is not also possible to use IF..ELSE in a JOINS... if im not mistaken... I think its not possible coz as what you have said before you can't either use IF..ELSE inside a query. thanks-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-14 : 22:07:09
|
not in joins. IF @count1 = 0begin <query here with join to CSVTable(@empDcno)>endelsebegin <query here without joining to CSVTable(@empDcno)>end KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-14 : 22:13:27
|
| That is what i did before. I just want to simplify my query. If there is no other alternative, i think i should stick with that. Thanks.-Ron- |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-19 : 05:19:59
|
| Hi all. I have difficulty on the CASE function. DECLARE @isResigned bitSET @isResigned = @getisResignedselect e.* from hremployees inner join psdatacenter d on e.empdcno = d.dcnowhere e.dateresign is null AND e.dateresign is case @isResigned when 1 then not nullelse nullendThis is the query i want to get up but it has error. @isResigned may have a value of 0 or 1. If 0, it will display all employees that still working in the company. If 1, display all employees working in the company AND those who resigned. thanks.-Ron- |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-19 : 05:43:54
|
| [code]DECLARE @isResigned bitSET @isResigned = @getisResignedselect e.*from hremployees inner join psdatacenter as d on e.empdcno = d.dcnowhere CASE WHEN @IsResigned = 1 AND e.DateResigned IS NOT NULL THEN 1 WHEN e.DateResigned IS NULL THEN 1 ELSE 0 END = 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-19 : 05:52:29
|
| o thanks Peso. **Bow down**-Ron- |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-19 : 06:02:42
|
Also try thisselect e.*from hremployees inner join psdatacenter as d on e.empdcno = d.dcnowhere e.DateResigned IS NULLunion allselect e.*from hremployees inner join psdatacenter as d on e.empdcno = d.dcnowhere @IsResigned = 1 and e.DateResigned IS not NULL Peter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-19 : 06:11:30
|
| *scratching head* I can't understand your first query, but great it works well. in your 2nd query i understand it but i refer to use the first 1.-Ron- |
 |
|
|
Next Page
|