| Author |
Topic |
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 03:51:10
|
| Hi again. Another problem though...Can I make a query in if else statement? For exampleint querynum;if(querynum==1)<execute query1>else if(querynum==2)<execute query2>else <execute query3>There is no problem with passing parameters(getquerynum) from my front end application to sql function. I can manage to do it. hehe.the query looks like this:DECLARE @querynum numberSET @querynum = @getquerynumwhat i want to do is,if(querynum==1) Select * from hremployeeselse if(querynum==2) Select * from hrapplicantselse Select * from pspersonaldatais it possible by having multiple queries in conditional statement?if so, how to do it in sql language?Thanks-Ron- |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 04:00:32
|
use single equal = instead of double equal == sign for comparison"is it possible by having multiple queries in conditional statement?"yes. enclose the query in BEGIN ... END block KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 04:12:11
|
| This is whta i did.DECLARE @querynum intSET @querynum = 2if(@querynum=1) BEGIN Select * from hremployees ENDelse if(@querynum=2) BEGIN Select * from hrapplicants ENDelse BEGIN Select * from pspersonaldata ENDOh thanks KH. It works well.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 04:23:17
|
What are you trying to achieve here ? A common stored procedure for querying data from various table ? KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-07 : 04:37:30
|
| DECLARE @querynum intSET @querynum = 2if @querynum = 1 Select * from hremployeesif @querynum = 2 Select * from hrapplicantsif @querynum not in (1, 2) Select * from pspersonaldataPeter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 04:44:20
|
| no. same table as well. but the queries have a distinct parameters. for example.DECLARE @querynum intSET @querynum = 2if(@querynum=1) BEGIN Select * from hremployees where empdcno = 12 ENDelse if(@querynum=2) BEGIN Select * from hremployees inner join CSVTable.... ENDelse BEGIN Select * from hremployees ENDit looks like this. My intention for this is to prevent of creating another report design. It must be 1 report only for hremployees. However, if you create multiple reports for hremployeesis,hmmmmm... its not logically correct. that's on my own opinion.Thanks again.-Ron- |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 22:02:47
|
| what is wrong with my query? did i proper use the if statement?DECLARE @querynum intSET @querynum = 2DECLARE @religion varchar (100)SET @religion = 'Born again, adventist'select *from(select a.HospName,a.HospLogo,a.HospTelephone,a.HospWebsite,a.HospStreetBldg1,a.HospAddress as Addressfrom appsysconfiggeneral as a) across join(if(@querynum = 1)BEGINselect D.fullname, P.religion, E.empno from pspersonaldata as Pinner join hremployees as E on P.dcno = E.empdcnoinner join psdatacenter as D on D.dcno = E.empdcnoinner join CSVTable(@religion) as R on P.religion = R.stringvalEND ) bERROR:Msg 156, Level 15, State 1, Line 13Incorrect syntax near the keyword 'if'.Msg 102, Level 15, State 1, Line 20Incorrect syntax near ')'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 22:21:20
|
You can't have the IF condition inside a query. What are you trying to achieve here ? KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 22:29:46
|
| this is what i want to achieve.DECLARE @religion varchar (100), @querynum intSET @religion = 'Born again, adventist'SET @querynum = 2select *from(select a.HospName,a.HospLogo,a.HospTelephone,a.HospWebsite,a.HospStreetBldg1,a.HospAddress as Addressfrom appsysconfiggeneral as a) across join(if(@querynum = 1)BEGINselect D.fullname, P.religion, E.empno from pspersonaldata as Pinner join hremployees as E on P.dcno = E.empdcnoinner join psdatacenter as D on D.dcno = E.empdcnoinner join CSVTable(@religion) as R on P.religion = R.stringvalEND elsebeginselect D.fullname, P.religion, E.empno from pspersonaldata as Pinner join hremployees as E on P.dcno = E.empdcnoinner join psdatacenter as D on D.dcno = E.empdcnoinner join CSVTable(@religion) as R on P.religion = R.stringvalend) bBut it pretty works fine here.DECLARE @religion varchar (100), @querynum intSET @religion = @empreligionSET @querynum = @getquerynumif(@querynum =1) beginselect *from(select a.HospName,a.HospLogo,a.HospTelephone,a.HospWebsite,a.HospStreetBldg1,a.HospAddress as Addressfrom appsysconfiggeneral as a) across join(select D.fullname, P.religion, E.empno from pspersonaldata as Pinner join hremployees as E on P.dcno = E.empdcnoinner join psdatacenter as D on D.dcno = E.empdcnoinner join CSVTable(@religion) as R on P.religion = R.stringval) bendelsebeginselect *from(select a.HospName,a.HospLogo,a.HospTelephone,a.HospWebsite,a.HospStreetBldg1,a.HospAddress as Addressfrom appsysconfiggeneral as a) across join(select D.fullname, P.religion, E.empno from pspersonaldata as Pinner join hremployees as E on P.dcno = E.empdcnoinner join psdatacenter as D on D.dcno = E.empdcno) bend |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 01:35:56
|
| Your CSV variable should not be divided by both comma and space!Peter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-08 : 01:41:40
|
| "Your CSV variable should not be divided by both comma and space!"Sorry I can't get your point. What do you mean by that?Thanks.-Ron- |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 03:46:43
|
| If you split the value "Born again, adventist", you get two records in the split function like this"Born again"" adventist" -- Please notice the space in front of adventistPeter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 03:47:03
|
| And there are no records matching that one!Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 03:53:13
|
Actually the Doctor's CSVTable has kindly remove the spacesselect stringval = '[' + stringval + ']'from CSVTable(' Born again , adventist ')/*stringval -------------[Born again][adventist]*/from CSVTable codequote:
insert into @tvalues (CASE WHEN isnumeric(@c)=1 THEN @c else Null END,rtrim(ltrim(@c)),
KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-08 : 05:01:17
|
| Even with space or no, still works perfectly... :) :) :)Thanks to KH.-Ron- |
 |
|
|
|