Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sk.faheemuddin
Starting Member
2 Posts |
Posted - 2012-05-12 : 01:31:14
|
Hi,I am having a query containing three unions.But i want to compulsory execute 1st queryrest two query should be optional based on the users interest using parameters because 2nd and 3rd query are time consuming.But even though data retrieval point of view it is working very fine but time taken is samethere is no changefor example/*Parameter Area*/ declare @StartDate as datetime set @StartDate ='09-MAY-12' declare @EndDate as datetime set @EndDate ='09-MAY-12'declare @With_MonthYear as nvarchar(1)set @With_MonthYear ='N'/*Main Query Area*/ select * from a where a.date betwn @startdate and @enddate;union all --Bselect * from a where a.date betwn @startdate and @enddate and 'Y'=@With_MonthYear; union all --Cselect * from a where a.date betwn @startdate and @enddate and 'Y'=@With_MonthYear;i.eee the 2nd n 3rd should only work when user selects 'Y'but it execute i guess and take same amount of time approx..more thn 2minsPlease advice |
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-12 : 06:15:54
|
[code] Try this/*Parameter Area*/ declare @StartDate as datetime set @StartDate ='09-MAY-12' declare @EndDate as datetime set @EndDate ='09-MAY-12'declare @With_MonthYear as nvarchar(1)set @With_MonthYear ='N'/*Main Query Area*/ if(@With_MonthYear ='Y')beginselect * from a where a.date betwn @startdate and @enddate;union all --Bselect * from a where a.date betwn @startdate and @enddateand 'Y'=@With_MonthYear; union all --Cselect * from a where a.date betwn @startdate and @enddateand 'Y'=@With_MonthYear;else select * from a where a.date betwn @startdate and @enddate;END[/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-12 : 20:08:10
|
quote: Originally posted by sk.faheemuddin Hi,I am having a query containing three unions.But i want to compulsory execute 1st queryrest two query should be optional based on the users interest using parameters because 2nd and 3rd query are time consuming.But even though data retrieval point of view it is working very fine but time taken is samethere is no changefor example/*Parameter Area*/ declare @StartDate as datetime set @StartDate ='09-MAY-12' declare @EndDate as datetime set @EndDate ='09-MAY-12'declare @With_MonthYear as nvarchar(1)set @With_MonthYear ='N'/*Main Query Area*/ select * from a where a.date betwn @startdate and @enddate;union all --Bselect * from a where a.date betwn @startdate and @enddate and 'Y'=@With_MonthYear; union all --Cselect * from a where a.date betwn @startdate and @enddate and 'Y'=@With_MonthYear;i.eee the 2nd n 3rd should only work when user selects 'Y'but it execute i guess and take same amount of time approx..more thn 2minsPlease advice
all queries look same in above. then why are calling it again and again? i cant understand what you're trying to achieve. Are you trying to replicate the data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2012-05-13 : 01:50:26
|
You could split the two options into 2 stored procedures. Make the decision at the application levelJack Vamvas--------------------http://www.sqlserver-dba.com |
 |
|
sk.faheemuddin
Starting Member
2 Posts |
Posted - 2012-05-13 : 05:25:45
|
Hi @Vijays3,Thanks, It's Done. |
 |
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-13 : 07:45:24
|
[code]Welcome[/code]Vijay is here to learn something from you guys. |
 |
|
|
|
|