Solved

Different results from stored procedure: Query Analyzer vs. ADO

Posted on 2004-03-29
12
411 Views
Last Modified: 2008-03-17
I have a stored procedure that is creating a temporary table then providing a select query for output based off of the temporary table.  A weird thing happened in Access when I tried to call the SP by a pass-through query: the procedure took a long time, then returned blank rows and zeroes where there should have been data.

I then opened the Query Analyzer and ran the SP manually.  It returned the information as it was intended to do.  Funny.  I went back to Access and now the pass-through query was working normally.  Arrgh!  All that day I could not get the SP to break.

Next morning I tried to run the pass-through query in Access, and it puked on my shoes yet again.  I decided not to "fix" it by running the SP in the Query Analyzer, but to instead experiment.  I ran the same pass-through on 3 different machines including my computer with no success.  I then blamed Access, my default whipping-boy program.  Onward to VB and ADO!

Surely pulling in the SP through ADO and creating a simple CSV file (for use in Excel) would be the simplest thing to do, right?  Same problem!!!  Yet everytime I run it from the Query Analyzer, it works and fixes the pass-through and ADO connections.

I've rebooted the server, which is MS SQL Server 2000 running on Windows 2000 Server SP3.  Thanks in advance!

dexweaver

0
Comment
Question by:dexweaver
12 Comments
 
LVL 3

Expert Comment

by:debi_mela
ID: 10707433
Local Temporary table is valid only for that session..  When you run using query analyser it considers as a single session.

it could be possible that your access connects to sp in different sessions?, just a suggestion though..
0
 
LVL 34

Expert Comment

by:arbert
ID: 10707456
Post the sp.....

Make sure that you have

SET NOCOUNT ON


as one of the first statement in the proc--if you don't "informational" messages get returned to the front-end programs messing them up..........
0
 
LVL 1

Author Comment

by:dexweaver
ID: 10707661
arbert:

I haven't run the SP in Query Analyzer at all today, so it is still "broken".  I tried to run this with SET NOCOUNT ON and it did the same thing.  

One thing I failed to mention is that this thing runs in under twenty seconds in Query Analyzer, but bombs out after 3-5 minutes any other way.

Here is the SP:

*************************

CREATE procedure
 spBankReconciliation
  @StartFundDate datetime,
  @EndFundDate datetime

as

if isnull(object_id(N'[tempdb].[dbo].[#CardTotals]'),0) <> 0
 drop table [#CardTotals]

select
 SFB.[FundDate],
 SFB.[RegisterDate],
 SFB.[SettlementFileBatchID],
 SFB.[TerminalNumber],
 ST.[StoreID],  
 sum((case
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [MC],
 sum((case
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [VI],
 sum((case
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DB],
 sum((case
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [VY],
 sum((case
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DC],
 sum((case
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DS],
 sum((case
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [AE],
 sum((case
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [WX],
 sum((case
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [MM],
 sum((case
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [SV]
into
 #CardTotals
from
 SettlementFile SF join
 SettlementFileDivision SFDV on SF.SettlementFileID = SFDV.SettlementFileID join
 SettlementFileMerchant SFM on SFDV.SettlementFileDivisionID = SFM.SettlementFileDivisionID join
 SettlementFileBatch SFB on SFM.SettlementFileMerchantID = SFB.SettlementFileMerchantID join
 SettlementFileDetail SFDT ON SFB.SettlementFileBatchID = SFDT.SettlementFileBatchID join
 Store ST on SFM.PaymentechMerchantNumber = ST.PaymentechMerchantNumber
where
 SFB.FundDate between @StartFundDate and @EndFundDate and
 SFDT.RecordType in ('1','4') and
 SFDT.TransactionVoidFlag not in ('R','V') and
 SFB.RegisterDate is not null
group by
 SFB.FundDate,
 SFB.RegisterDate,
 SFB.SettlementFileBatchID,
 SFB.TerminalNumber,
 ST.StoreID

select
 StoreID,
 FundDate,
 count(SettlementFileBatchID) as AllBatchCount,
 sum(MC + VI + DB + VY + DC + DS + AE + WX + MM + SV) as AllBatchTotal,
 sum(MC + VI + DB + VY + DC) as AllPaymentech,
 sum(DS) AS AllDS,

 sum(AE) AS AllAE,
 sum(WX) AS AllWX,
 sum(MM) AS AllMM,
 sum(SV) AS AllSV,
 sum(case
  when FundDate = RegisterDate then 0
  else 1 end) as PreEODBatchCount,
 sum(case
  when FundDate = RegisterDate then 0
  else MC + VI + DB + VY + DS + AE + WX + MM + SV end) as PreEODBatchTotal,
 sum(case
  when FundDate = RegisterDate then 0
  else MC + VI + DB + VY + DC end) as PreEODPaymentech,
 sum(case
  when FundDate = RegisterDate then 0
  else DS end) as PreEODDS,
 sum(case
  when FundDate = RegisterDate then 0
  else AE end) as PreEODAE,
 sum(case
  when FundDate = RegisterDate then 0
  else WX end) as PreEODWX,
 sum(case
  when FundDate = RegisterDate then 0
  else MM end) as PreEODMM,
 sum(case
  when FundDate = RegisterDate then 0
  else SV end) as PreEODSV
from
 #CardTotals
group by
 StoreID,
 FundDate
order by
 StoreID,
 FundDate

drop table
 #CardTotals
GO

*************************

0
 
LVL 34

Expert Comment

by:arbert
ID: 10707886
I would start up SQL Server profiler and capture the SQL Statements that are sent when you run this from Access....I'm guessing maybe there are some problems with parms or something.

You should still include the SET NOCOUNT ON at the top of your proc.
0
 
LVL 3

Expert Comment

by:ctcampbell
ID: 10710197
From the SQL BOL: "The user executing a SELECT statement with the INTO clause must have CREATE TABLE permission in the destination database".  I think by default, everyone has rights to create temp tables, but make sure the user that is coming in through ADO isn't being denied access for any reason.  Since you are using SQL Server 2000, I would highly recommend canning your temp table altogether and using a table variable.  You have to declare the table variable first (no SELECT ... INTO), but you're supposed to do that anyway :).


DECLARE @CardTotals TABLE (...)

INSERT INTO @CardTotals (...)
SELECT ...
FROM SettlementFile SF
JOIN ...etc
0
 
LVL 1

Author Comment

by:dexweaver
ID: 10714564
arbert:

I ran the profiler and found nothing of interest.  Whether I run it during execution of the Access pass-through or the VB with ADO, I get only normal looking steps.  It did show that the temp table creation took just 15 seconds shy of 5 minutes, so the problem lies there.

The NOCOUNT statement has been added to the procedure.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 34

Expert Comment

by:arbert
ID: 10714634
"It did show that the temp table creation took just 15 seconds shy of 5 minutes, so the problem lies there."

Did you look at the parms that got passed--did they look correct?  I'm still betting that there is a parm problem if you see the temp table being created, but it takes longer to build....
0
 
LVL 1

Author Comment

by:dexweaver
ID: 10714777
arbert:

The 2 parameters look correct.  I pass a begin and end date using single quotes:

spBankReconciliation '03/21/2004','03/27/2004'

That is exactly how I call it, and exactly as it showed up in the profiler.  In fact, I copied and pasted that line from the profiler.
0
 
LVL 1

Author Comment

by:dexweaver
ID: 10725460
ctcampbell:

The permissions all look normal.  I converted the SP to use a table variable.  It did the same thing!!!  The SP was changed to the following:

***********************

CREATE procedure
 spBankReconciliation
  @StartFundDate datetime,
  @EndFundDate datetime

as

declare @CardTotals table(
  [FundDate] [datetime] null ,
  [RegisterDate] [datetime] null ,
  [SettlementFileBatchID] [int] not null ,
  [TerminalNumber] [char] (3)  null,
  [StoreID] [nvarchar] (4) not null,
  [MC] [money] null,
  [VI] [money] null,
  [DB] [money] null,
  [VY] [money] null,
  [DC] [money] null,
  [DS] [money] null,
  [AE] [money] null,
  [WX] [money] null,
  [MM] [money] null,
  [SV] [money] null)

insert into
 @CardTotals
select
 SFB.[FundDate],
 SFB.[RegisterDate],
 SFB.[SettlementFileBatchID],
 SFB.[TerminalNumber],
 ST.[StoreID],  
 sum((case
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'MC' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [MC],
 sum((case
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'VI' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [VI],
 sum((case
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DB' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DB],
 sum((case
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'VY' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [VY],
 sum((case
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DC' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DC],
 sum((case
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'DS' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [DS],
 sum((case
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'AE' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [AE],
 sum((case
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'WX' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [WX],
 sum((case
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'MM' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [MM],
 sum((case
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '06' then -1
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '01' then 1
  when SFDT.CardType = 'PS' and SFDT.TransactionCode = '03' then 1
  else 0 end) * SFDT.TransactionAmount) as [SV]
from
 SettlementFile SF join
 SettlementFileDivision SFDV on SF.SettlementFileID = SFDV.SettlementFileID join
 SettlementFileMerchant SFM on SFDV.SettlementFileDivisionID = SFM.SettlementFileDivisionID join
 SettlementFileBatch SFB on SFM.SettlementFileMerchantID = SFB.SettlementFileMerchantID join
 SettlementFileDetail SFDT ON SFB.SettlementFileBatchID = SFDT.SettlementFileBatchID join
 Store ST on SFM.PaymentechMerchantNumber = ST.PaymentechMerchantNumber
where
 (SFB.FundDate between @StartFundDate and @EndFundDate) and
 SFDT.RecordType in ('1','4') and
 SFDT.TransactionVoidFlag not in ('R','V') and
 SFB.RegisterDate is not null
group by
 SFB.FundDate,
 SFB.RegisterDate,
 SFB.SettlementFileBatchID,
 SFB.TerminalNumber,
 ST.StoreID

select
 StoreID,
 FundDate,
 count(SettlementFileBatchID) as AllBatchCount,
 sum(MC + VI + DB + VY + DC + DS + AE + WX + MM + SV) as AllBatchTotal,
 sum(MC + VI + DB + VY + DC) as AllPaymentech,
 sum(DS) AS AllDS,
 sum(AE) AS AllAE,
 sum(WX) AS AllWX,
 sum(MM) AS AllMM,
 sum(SV) AS AllSV,
 sum(case
  when FundDate = RegisterDate then 0
  else 1 end) as PreEODBatchCount,
 sum(case
  when FundDate = RegisterDate then 0
  else MC + VI + DB + VY + DS + AE + WX + MM + SV end) as PreEODBatchTotal,
 sum(case
  when FundDate = RegisterDate then 0
  else MC + VI + DB + VY + DC end) as PreEODPaymentech,
 sum(case
  when FundDate = RegisterDate then 0
  else DS end) as PreEODDS,
 sum(case
  when FundDate = RegisterDate then 0
  else AE end) as PreEODAE,
 sum(case
  when FundDate = RegisterDate then 0
  else WX end) as PreEODWX,
 sum(case
  when FundDate = RegisterDate then 0
  else MM end) as PreEODMM,
 sum(case
  when FundDate = RegisterDate then 0
  else SV end) as PreEODSV
from
 @CardTotals
group by
 StoreID,
 FundDate
order by
 StoreID,
 FundDate

GO

***********************

0
 
LVL 1

Author Comment

by:dexweaver
ID: 10725501
I just recently deleted and re-created the SP.  It fixed it for the first time, but then it reverted to its normal decrepit state on a second run.

I also was able to get it to puke from the Query Analyzer window once!  This doesn't help with the trouble-shooting.

Another thing I notice is that the Profiler shows a recompile every other time this thing is run.  Hope that tidbit can help.

Tonight I am going to update the server to SQL Server Service Pack 3a.  It currently has no service packs applied.  I'm running out of ideas.  Help!

0
 
LVL 34

Accepted Solution

by:
arbert earned 500 total points
ID: 10727257
Ya, definately put the service pack on!  If for not other reason, protection against Slammer....The Recompile you see in profiler is normal....
0
 
LVL 1

Author Comment

by:dexweaver
ID: 10758481
The service pack made the problem go away.  Even though I fixed the problem myself, I appreciate the input arbert.

dexweaver
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now