Hi Guys,
I am using SQL Server 2012. Today I noticed that Output of Stored Procedure is coming in a order by default. But when same query I run separate, output is not in order.
The reason I ask this question because SP is taking hell of time to execute due to sorting (I hope) while query returning within a second.
See the below query and SP. Below SP returning values which is sorted based on ID but If I run same query outside result is not sorted.
DECLARE
@P_FROM_DATEASDATETIME,
@P_TO_DATEASDATETIME
SET@P_FROM_DATE='2013-12-20'
SET@P_TO_DATE='2013-12-21'
SELECT
BT.ID
,MIN(BDT.DATE)asDATE
FROMDBO.EMP_TABLEBT
LEFTJOINDBO.EMP_DETAILSREFon REF.ID =BT.ID
LEFTJOINDBO.EMP_SUMMARYBDTon BDT.EMP_ID=REF.EMP_ID
WHEREBT.CREATEDDATE>=@P_FROM_DATE ANDBDT.MODIFIEDDATE>= @P_FROM_DATE
GROUPBY
BT.ID
--SP
GO
ALTERProcedure[DBO].[SPR_EMP]
(
@P_FROM_DATEASDATETIME,
@P_TO_DATEASDATETIME
)--WITH RECOMPILE
AS
SET@P_TO_DATE= CASEWHENCONVERT(VARCHAR,@P_TO_DATE,108)='00:00:00'THEN@P_TO_DATE+' '+'11:59:59 PM'
ELSE@P_TO_DATE END
-------------------------------------------------------------------------------------------------------------
SELECT
BT.ID
,MIN(BDT.DATE)asDATE
FROMDBO.EMP_TABLEBT
LEFTJOINDBO.EMP_DETAILSREFon REF.ID =BT.ID
LEFTJOINDBO.EMP_SUMMARYBDTon BDT.EMP_ID=REF.EMP_ID
WHEREBT.CREATEDDATE>=@P_FROM_DATE ANDBDT.MODIFIEDDATE>= @P_FROM_DATE
GROUPBY
BT.ID
The reason I ask this question because SP is taking hell of time to execute due to sorting (I hope) while query returning within a second.
Thanks Shiven:) If Answer is Helpful, Please Vote