SQL Server - Stored procedures executed one after the other give different results.

Asked By Aryan Bhatt on 23-Nov-13 10:11 AM

Hi,

I have a batch of 10 stored procedures that I am executing one after the other.
Each of these procedures accepts 3 parameters, the startdate the enddate and a flag which determines whether a table
is to be repopulated or not. If the value passed to the flag is 'Y' the table is repopulated whereas if its 'N' its not.
All my stored procedures query data from this table.

Now, if I execute this batch of stored procedures on my Development Server they give the expected results.
However, on deploying this batch to the Production server, they gave totally different results.
i.e.  the number of rows did not match for the stored procedure 1 and 2.
Also, the procedures 3-7 did return give any rows whereas they do return the correct data on the development server.

The only ctach seems to be I pass the flag as 'Y' i.e. I repopulate the table when the first stored procedure runs whereas for all the others the flag is passed as 'N'.
e.g. exec [dbo].[spMonthlyInvChecks1] '2025-09-10', '2025-09-30', 'Y'
exec [dbo].[spMonthlyInvChecks2] '2025-09-10', '2025-09-30', 'N'
exec [dbo].[spMonthlyInvChecks3] '2025-09-10', '2025-09-30', 'N'

I have pasted the stored procedure code below :

CREATE


procedure [dbo].[spMonthlyInvoiceChecks1]

@startDate

datetime,

@endDate

datetime,

@repopulateTable

char (1) = 'N'

as


BEGIN

IF


(@repopulateTable = 'Y')

BEGIN

truncate table MonthlyChecks

insert into MonthlyChecks

select * from vChecksView

END

BEGIN


select col1, col2 etc..
from MonthlyChecks join table2

where conditions...
order by...

  End
End
Go

Any help would be much appreciated.


Aryan.


 



Robbe Morris replied to Aryan Bhatt on 24-Nov-13 10:54 AM
What is the purpose of loading the entire dataset from a View into another table?  Each time you truncate and reload the table, you fragment its indexes.  You don't post the code from the 3 different stored procedures.  So, no one can tell you why your code doesn't work.