显示标签为“stored”的博文。显示所有博文
显示标签为“stored”的博文。显示所有博文

2012年3月29日星期四

Flow Control in stored procedures

A newbie question..

If within a stored procedure there are a list of Stored Procedure calls.

Create Procedure spTest AS

EXEC spMyActionQuery1
EXEC spMyActionQuery2
EXEC spMyActionQuery3
EXEC spMyActionQuery4

GO

Assuming each sub stored procedure executes an action query, does SQL Server ALWAYS wait for the current procedure to finish execution before proceding with the next stored procedure? I believe the answer is yes.The answer IS yes :D

2012年3月19日星期一

FK

I know that in the case of a primary key (non-clustered)
the index part of the foreign key must be stored
separately from the table. Is this true if you have a
foreign key on a table? Is there data stored externally
to the table for a foreign key?
Also: uou don't have to defrag or reindex a foreign key
do you?A foreign key is just a rule. SQL Server doesn't have to store anything
additional as you add the FK constraint. SQL Server will, however, make sure
that your data adheres to your rules, so a check is performed as you modify
data. Creating an index on the foreign key column can be very beneficial to
assist for this validation. Not to mention that you often perform joins over
primary key - foreign key relationships. And remember that a FK column can
be a good candidate for the clustered index.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"GXX" <anonymous@.discussions.microsoft.com> wrote in message
news:442201c4020f$c3ca8150$a601280a@.phx.gbl...
> I know that in the case of a primary key (non-clustered)
> the index part of the foreign key must be stored
> separately from the table. Is this true if you have a
> foreign key on a table? Is there data stored externally
> to the table for a foreign key?
> Also: uou don't have to defrag or reindex a foreign key
> do you?

2012年3月7日星期三

First time, it times-out. After that it is fine.

I have a stored procedure that runs slowly and takes over 60 seconds sometimes, but almost all of the time it takes only one or two seconds. In both cases the same input data is used and there have been no changes to the table. The slow running is after the query has not been run for several days.

In the background 16 records are inserted every minute. The table has 5 million records. There are two indexes on the table one is clustered the other is unclustered. They are very well tuned. The record contains a two integers and 90 floats.

The stored procedure gets one record from the database which is the latest record for a particular entity. If I go to the web site after a few days the page times out. If I go to Mgmt Studio, and run the sp form there it takes a very long time but eventually comes back with the correct answer. If execute the stored procedure after that the response is a second or two using the same input data. If I change the input data the response is equally as fast. The web site also works very quickly too.

What is going on and how do I fix it?

Pops73

Try running the SP with SET STATISTICS IO ON, then look at the messages tab. It sounds like the first time you run the query, it is having to do physical reads as opposed to logical reads. The second time you run it, it is doing logical reads, which would be much, much faster.|||

Thank you for the response and it sounds reasonable. I restarted the server which I thought would clear the buffer cache and cause the problem to occur. But it didn't after the restart the timeout did not occur. Response was good. Is the buffer cache saved through a restart of a server? How can I clear the cache to easily reproduce the problem and gather the statistics?

Pops73

|||

Restarting the server does clear the the buffer cache. You can run the command DBCC FREEPROCCACHE to clear out the procedure cache, and DBCC DROPCLEANBUFFERS to flush the data cache.

http://msdn2.microsoft.com/en-US/library/ms187762.aspx

Run these commands:

DBCC FREEPROCCACHE

GO

CHECKPOINT

GO

DBCC DROPCLEANBUFFERS

|||

Thank you for your help. This still does not reproduce the problem.

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

More info -- I moved the indexes. I have the data in one file group with three files and the indexes in another file group with two files. I still saw the problem.

running Sql Server 2005 sp1

I had the problem yesterday in another db instance of the same applicaiton on another machine. This table only had one record instead of 5 million. Two different queries will show the same problem

Part of the table definition

CREATE TABLE [dbo].[TempMtrScan](

[ID] [bigint] IDENTITY(1,1) NOT NULL,

[ShovelID] [int] NOT NULL,

[DateTime] [datetime] NOT NULL,

[FHoistNDE] [float] NULL,

[FHoistDE] [float] NULL,

[FHoistIP] [float] NULL,

[FHoistFLD] [float] NULL, ............

Query 1

INSERT INTO @.TempMtrScanUnpivot ([ShovelID],[TempMtrScanName], [Temperature])

SELECT ShovelID, TempMtrScanName, Temperature

FROM

(SELECT ShovelID,[DateTime],

FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

FROM dbo.TempMtrScan

WHERE [Datetime] =

(SELECT MAX([datetime])

FROM dbo.TempMtrScan

WHERE ShovelID = @.ShovelID)

AND ShovelID = @.ShovelID) A

UNPIVOT (

Temperature

FOR TempMtrScanName

IN ( FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

)

) AS unpvt

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Query 2

SELECT

(max(T.FHoistNDE)* 1.8 + 32) as "FHoistNDE" ,

(max(T.FHoistDE)* 1.8 + 32) as "FHoistDE" ,

(max(T.FHoistIP)* 1.8 + 32) as "FHoistIP" ,

(max(T.FHoistFLD)* 1.8 + 32) as "FHoistFLD" ,

(max(T.RHoistNDE)* 1.8 + 32) as "RHoistNDE" ,

(max(T.RHoistDE)* 1.8 + 32) as "RHoistDE" ,

(max(T.RHoistIP)* 1.8 + 32) as "RHoistIP" ,

(max(T.RHoistFLD)* 1.8 + 32) as "RHoistFLD" ,

(max(T.CrowdNDE)* 1.8 + 32) as "CrowdNDE" ,

(max(T.CrowdDE) * 1.8 + 32) as "CrowdDE" ,

(max(T.CrowdIP) * 1.8 + 32) as "CrowdIP" ,

(max(T.CrowdFLD) * 1.8 + 32) as "CrowdFLD" ,

(max(T.SwingNDE)* 1.8 + 32) as "SwingNDE" ,

(max(T.SwingDE) * 1.8 + 32) as "SwingDE" ,

(max(T.SwingIP) * 1.8 + 32) as "SwingIP" ,

(max(T.SwingFLD)* 1.8 + 32) as "SwingFLD" ,

(max(T.RearSwingNDE)* 1.8 + 32) as "RearSwingNDE" ,

(max(T.RearSwingDE) * 1.8 + 32) as "RearSwingDE" ,

(max(T.RearSwingIP) * 1.8 + 32) as "RearSwingIP" ,

(max(T.RearSwingFLD)* 1.8 + 32) as "RearSwingFLD" ,

(max(T.RPropelNDE) * 1.8 + 32) as "RPropelNDE" ,

(max(T.RPropelDE)* 1.8 + 32) as "RPropelDE" ,

(max(T.RPropelIP) * 1.8 + 32) as "RPropelIP" ,

(max(T.RPropelFLD) * 1.8 + 32) as "RPropelFLD" ,

(max(T.LPropelNDE) * 1.8 + 32) as "LPropelNDE" ,

(max(T.LPropelDE) * 1.8 + 32) as "LPropelDE" ,

(max(T.LPropelIP) * 1.8 + 32) as "LPropelIP" ,

(max(T.LPropelFLD) * 1.8 + 32) as "LPropelFLD" ,

(max(T.LFrontSwingNDE)* 1.8 + 32) as "LFrontSwingNDE" ,

(max(T.LFrontSwingDE)* 1.8 + 32) as "LFrontSwingDE" ,

(max(T.LFrontSwingIP) * 1.8 + 32) as "LFrontSwingIP" ,

(max(T.LFrontSwingFLD) * 1.8 + 32) as "LFrontSwingFLD" ,

(max(T.RFrontSwingNDE) * 1.8 + 32) as "RFrontSwingNDE" ,

(max(T.RFrontSwingDE) * 1.8 + 32) as "RFrontSwingDE" ,

(max(T.RFrontSwingIP)* 1.8 + 32) as "RFrontSwingIP" ,

(max(T.RFrontSwingFLD) * 1.8 + 32) as "RFrontSwingFLD" ,

(max(T.TransPhaseA)* 1.8 + 32) as "TransPhaseA" ,

(max(T.TransPhaseB)* 1.8 + 32) as "TransPhaseB" ,

(max(T.TransPhaseC)* 1.8 + 32) as "TransPhaseC" ,

(max(T.DriveInletTemp1)* 1.8 + 32) as "DriveInletTemp1" ,

(max(T.DriveInletTemp2)* 1.8 + 32) as "DriveInletTemp2" ,

(max(T.DriveInletTemp3)* 1.8 + 32) as "DriveInletTemp3" ,

(max(T.DriveInletTemp4)* 1.8 + 32) as "DriveInletTemp4" ,

(max(T.DriveInletTemp5)* 1.8 + 32) as "DriveInletTemp5" ,

(max(T.DriveInletTemp6)* 1.8 + 32) as "DriveInletTemp6" ,

(max(T.DriveInletTemp7)* 1.8 + 32) as "DriveInletTemp7" ,

(max(T.DriveInletTemp8)* 1.8 + 32) as "DriveInletTemp8" ,

(max(T.CwdMotorTemp)* 1.8 + 32) as "CwdMotorTemp" ,

(max(T.CwdMotorWindABTemp)* 1.8 + 32) as "CwdMotorWindABTemp" ,

(max(T.CwdMotorWindBCTemp)* 1.8 + 32) as "CwdMotorWindBCTemp" ,

(max(T.CwdMotorWindCATemp)* 1.8 + 32) as "CwdMotorWindCATemp" ,

(max(T.PplRhtMtrMotorTemp)* 1.8 + 32) as "PplRhtMtrMotorTemp" ,

(max(T.PplRhtMtrMotorWindABTemp)* 1.8 + 32) as "PplRhtMtrMotorWindABTemp" ,

(max(T.PplRhtMtrMotorWindBCTemp)* 1.8 + 32) as "PplRhtMtrMotorWindBCTemp" ,

(max(T.PplRhtMtrMotorWindCATemp)* 1.8 + 32) as "PplRhtMtrMotorWindCATemp" ,

(max(T.SwgLFntMtrMotorTemp)* 1.8 + 32) as "SwgLFntMtrMotorTemp" ,

(max(T.SwgLFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindABTemp" ,

(max(T.SwgLFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindBCTemp" ,

(max(T.SwgLFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgLFntMtrMotorWindCATemp" ,

(max(T.HstRearMtrMotorTemp)* 1.8 + 32) as "HstRearMtrMotorTemp" ,

(max(T.HstRearMtrMotorWindABTemp)* 1.8 + 32) as "HstRearMtrMotorWindABTemp" ,

(max(T.HstRearMtrMotorWindBCTemp)* 1.8 + 32) as "HstRearMtrMotorWindBCTemp" ,

(max(T.HstRearMtrMotorWindCATemp)* 1.8 + 32) as "HstRearMtrMotorWindCATemp" ,

(max(T.PplLftMtrMotorTemp)* 1.8 + 32) as "PplLftMtrMotorTemp" ,

(max(T.PplLftMtrMotorWindABTemp)* 1.8 + 32) as "PplLftMtrMotorWindABTemp" ,

(max(T.PplLftMtrMotorWindBCTemp)* 1.8 + 32) as "PplLftMtrMotorWindBCTemp" ,

(max(T.PplLftMtrMotorWindCATemp)* 1.8 + 32) as "PplLftMtrMotorWindCATemp" ,

(max(T.SwgRFntMtrMotorTemp)* 1.8 + 32) as "SwgRFntMtrMotorTemp" ,

(max(T.SwgRFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindABTemp" ,

(max(T.SwgRFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindBCTemp" ,

(max(T.SwgRFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgRFntMtrMotorWindCATemp" ,

(max(T.HstFntMtrMotorTemp)* 1.8 + 32) as "HstFntMtrMotorTemp" ,

(max(T.HstFntMtrMotorWindABTemp)* 1.8 + 32) as "HstFntMtrMotorWindABTemp" ,

(max(T.HstFntMtrMotorWindBCTemp)* 1.8 + 32) as "HstFntMtrMotorWindBCTemp" ,

(max(T.HstFntMtrMotorWindCATemp)* 1.8 + 32) as "HstFntMtrMotorWindCATemp" ,

(max(T.SwgRearMtrMotorTemp)* 1.8 + 32) as "SwgRearMtrMotorTemp" ,

(max(T.SwgRearMtrMotorWindABTemp)* 1.8 + 32) as "SwgRearMtrMotorWindABTemp" ,

(max(T.SwgRearMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRearMtrMotorWindBCTemp" ,

(max(T.SwgRearMtrMotorWindCATemp)* 1.8 + 32) as "SwgRearMtrMotorWindCATemp" ,

(max(T.SwgFntMtrMotorTemp)* 1.8 + 32) as "SwgFntMtrMotorTemp" ,

(max(T.SwgFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgFntMtrMotorWindABTemp" ,

(max(T.SwgFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.SwgFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.ACS800Temp)* 1.8 + 32) as "ACS800Temp" ,

(max(T.TempDiffPhsU)* 1.8 + 32) as "TempDiffPhsU" ,

(max(T.TempDiffPhsV)* 1.8 + 32) as "TempDiffPhsV" ,

(max(T.TempDiffPhsW)* 1.8 + 32) as "TempDiffPhsW" ,

Max(T.DateTime) as "TemperatureTime"

FROM TempMtrScan T

JOIN Shovel S ON T.ShovelID = S.ID

WHERE (S.ID =@.ShovelID) AND T.DATETIME <= @.EndTime AND T.DATETIME >= @.StartTime

Group by ShovelID, datepart(yy,T.DateTime), datepart(dy,T.DateTime),datepart(hh,T.DateTime),datepart(mi,T.DateTime)/@.Duration

order by Max(T.DateTime) asc

(1943 row(s) affected)

Table 'Shovel'. Scan count 0, logical reads 2, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 3, logical reads 38911, physical reads 493, read-ahead reads 736, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Warning: Null value is eliminated by an aggregate or other SET operation.

First time, it times out. After that it is fine.

I have a stored procedure that runs slowly and takes over 60 seconds sometimes, but almost all of the time it takes only one or two seconds. In both cases the same input data is used and there have been no changes to the table. The slow running is after the query has not been run for several days.

In the background 16 records are inserted every minute. The table has 5 million records. There are two indexes on the table one is clustered the other is unclustered. They are very well tuned. The record contains a two integers and 90 floats.

The stored procedure gets one record from the database which is the latest record for a particular entity. If I go to the web site after a few days the page times out. If I go to Mgmt Studio, and run the sp form there it takes a very long time but eventually comes back with the correct answer. If execute the stored procedure after that the response is a second or two using the same input data. If I change the input data the response is equally as fast. The web site also works very quickly too.

What is going on and how do I fix it?

Pops73

Try running the SP with SET STATISTICS IO ON, then look at the messages tab. It sounds like the first time you run the query, it is having to do physical reads as opposed to logical reads. The second time you run it, it is doing logical reads, which would be much, much faster.|||

Thank you for the response and it sounds reasonable. I restarted the server which I thought would clear the buffer cache and cause the problem to occur. But it didn't after the restart the timeout did not occur. Response was good. Is the buffer cache saved through a restart of a server? How can I clear the cache to easily reproduce the problem and gather the statistics?

Pops73

|||

Restarting the server does clear the the buffer cache. You can run the command DBCC FREEPROCCACHE to clear out the procedure cache, and DBCC DROPCLEANBUFFERS to flush the data cache.

http://msdn2.microsoft.com/en-US/library/ms187762.aspx

Run these commands:

DBCC FREEPROCCACHE

GO

CHECKPOINT

GO

DBCC DROPCLEANBUFFERS

|||

Thank you for your help. This still does not reproduce the problem.

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

More info -- I moved the indexes. I have the data in one file group with three files and the indexes in another file group with two files. I still saw the problem.

running Sql Server 2005 sp1

I had the problem yesterday in another db instance of the same applicaiton on another machine. This table only had one record instead of 5 million. Two different queries will show the same problem

Part of the table definition

CREATE TABLE [dbo].[TempMtrScan](

[ID] [bigint] IDENTITY(1,1) NOT NULL,

[ShovelID] [int] NOT NULL,

[DateTime] [datetime] NOT NULL,

[FHoistNDE] [float] NULL,

[FHoistDE] [float] NULL,

[FHoistIP] [float] NULL,

[FHoistFLD] [float] NULL, ............

Query 1

INSERT INTO @.TempMtrScanUnpivot ([ShovelID],[TempMtrScanName], [Temperature])

SELECT ShovelID, TempMtrScanName, Temperature

FROM

(SELECT ShovelID,[DateTime],

FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

FROM dbo.TempMtrScan

WHERE [Datetime] =

(SELECT MAX([datetime])

FROM dbo.TempMtrScan

WHERE ShovelID = @.ShovelID)

AND ShovelID = @.ShovelID) A

UNPIVOT (

Temperature

FOR TempMtrScanName

IN ( FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

)

) AS unpvt

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Query 2

SELECT

(max(T.FHoistNDE)* 1.8 + 32) as "FHoistNDE" ,

(max(T.FHoistDE)* 1.8 + 32) as "FHoistDE" ,

(max(T.FHoistIP)* 1.8 + 32) as "FHoistIP" ,

(max(T.FHoistFLD)* 1.8 + 32) as "FHoistFLD" ,

(max(T.RHoistNDE)* 1.8 + 32) as "RHoistNDE" ,

(max(T.RHoistDE)* 1.8 + 32) as "RHoistDE" ,

(max(T.RHoistIP)* 1.8 + 32) as "RHoistIP" ,

(max(T.RHoistFLD)* 1.8 + 32) as "RHoistFLD" ,

(max(T.CrowdNDE)* 1.8 + 32) as "CrowdNDE" ,

(max(T.CrowdDE) * 1.8 + 32) as "CrowdDE" ,

(max(T.CrowdIP) * 1.8 + 32) as "CrowdIP" ,

(max(T.CrowdFLD) * 1.8 + 32) as "CrowdFLD" ,

(max(T.SwingNDE)* 1.8 + 32) as "SwingNDE" ,

(max(T.SwingDE) * 1.8 + 32) as "SwingDE" ,

(max(T.SwingIP) * 1.8 + 32) as "SwingIP" ,

(max(T.SwingFLD)* 1.8 + 32) as "SwingFLD" ,

(max(T.RearSwingNDE)* 1.8 + 32) as "RearSwingNDE" ,

(max(T.RearSwingDE) * 1.8 + 32) as "RearSwingDE" ,

(max(T.RearSwingIP) * 1.8 + 32) as "RearSwingIP" ,

(max(T.RearSwingFLD)* 1.8 + 32) as "RearSwingFLD" ,

(max(T.RPropelNDE) * 1.8 + 32) as "RPropelNDE" ,

(max(T.RPropelDE)* 1.8 + 32) as "RPropelDE" ,

(max(T.RPropelIP) * 1.8 + 32) as "RPropelIP" ,

(max(T.RPropelFLD) * 1.8 + 32) as "RPropelFLD" ,

(max(T.LPropelNDE) * 1.8 + 32) as "LPropelNDE" ,

(max(T.LPropelDE) * 1.8 + 32) as "LPropelDE" ,

(max(T.LPropelIP) * 1.8 + 32) as "LPropelIP" ,

(max(T.LPropelFLD) * 1.8 + 32) as "LPropelFLD" ,

(max(T.LFrontSwingNDE)* 1.8 + 32) as "LFrontSwingNDE" ,

(max(T.LFrontSwingDE)* 1.8 + 32) as "LFrontSwingDE" ,

(max(T.LFrontSwingIP) * 1.8 + 32) as "LFrontSwingIP" ,

(max(T.LFrontSwingFLD) * 1.8 + 32) as "LFrontSwingFLD" ,

(max(T.RFrontSwingNDE) * 1.8 + 32) as "RFrontSwingNDE" ,

(max(T.RFrontSwingDE) * 1.8 + 32) as "RFrontSwingDE" ,

(max(T.RFrontSwingIP)* 1.8 + 32) as "RFrontSwingIP" ,

(max(T.RFrontSwingFLD) * 1.8 + 32) as "RFrontSwingFLD" ,

(max(T.TransPhaseA)* 1.8 + 32) as "TransPhaseA" ,

(max(T.TransPhaseB)* 1.8 + 32) as "TransPhaseB" ,

(max(T.TransPhaseC)* 1.8 + 32) as "TransPhaseC" ,

(max(T.DriveInletTemp1)* 1.8 + 32) as "DriveInletTemp1" ,

(max(T.DriveInletTemp2)* 1.8 + 32) as "DriveInletTemp2" ,

(max(T.DriveInletTemp3)* 1.8 + 32) as "DriveInletTemp3" ,

(max(T.DriveInletTemp4)* 1.8 + 32) as "DriveInletTemp4" ,

(max(T.DriveInletTemp5)* 1.8 + 32) as "DriveInletTemp5" ,

(max(T.DriveInletTemp6)* 1.8 + 32) as "DriveInletTemp6" ,

(max(T.DriveInletTemp7)* 1.8 + 32) as "DriveInletTemp7" ,

(max(T.DriveInletTemp8)* 1.8 + 32) as "DriveInletTemp8" ,

(max(T.CwdMotorTemp)* 1.8 + 32) as "CwdMotorTemp" ,

(max(T.CwdMotorWindABTemp)* 1.8 + 32) as "CwdMotorWindABTemp" ,

(max(T.CwdMotorWindBCTemp)* 1.8 + 32) as "CwdMotorWindBCTemp" ,

(max(T.CwdMotorWindCATemp)* 1.8 + 32) as "CwdMotorWindCATemp" ,

(max(T.PplRhtMtrMotorTemp)* 1.8 + 32) as "PplRhtMtrMotorTemp" ,

(max(T.PplRhtMtrMotorWindABTemp)* 1.8 + 32) as "PplRhtMtrMotorWindABTemp" ,

(max(T.PplRhtMtrMotorWindBCTemp)* 1.8 + 32) as "PplRhtMtrMotorWindBCTemp" ,

(max(T.PplRhtMtrMotorWindCATemp)* 1.8 + 32) as "PplRhtMtrMotorWindCATemp" ,

(max(T.SwgLFntMtrMotorTemp)* 1.8 + 32) as "SwgLFntMtrMotorTemp" ,

(max(T.SwgLFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindABTemp" ,

(max(T.SwgLFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindBCTemp" ,

(max(T.SwgLFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgLFntMtrMotorWindCATemp" ,

(max(T.HstRearMtrMotorTemp)* 1.8 + 32) as "HstRearMtrMotorTemp" ,

(max(T.HstRearMtrMotorWindABTemp)* 1.8 + 32) as "HstRearMtrMotorWindABTemp" ,

(max(T.HstRearMtrMotorWindBCTemp)* 1.8 + 32) as "HstRearMtrMotorWindBCTemp" ,

(max(T.HstRearMtrMotorWindCATemp)* 1.8 + 32) as "HstRearMtrMotorWindCATemp" ,

(max(T.PplLftMtrMotorTemp)* 1.8 + 32) as "PplLftMtrMotorTemp" ,

(max(T.PplLftMtrMotorWindABTemp)* 1.8 + 32) as "PplLftMtrMotorWindABTemp" ,

(max(T.PplLftMtrMotorWindBCTemp)* 1.8 + 32) as "PplLftMtrMotorWindBCTemp" ,

(max(T.PplLftMtrMotorWindCATemp)* 1.8 + 32) as "PplLftMtrMotorWindCATemp" ,

(max(T.SwgRFntMtrMotorTemp)* 1.8 + 32) as "SwgRFntMtrMotorTemp" ,

(max(T.SwgRFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindABTemp" ,

(max(T.SwgRFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindBCTemp" ,

(max(T.SwgRFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgRFntMtrMotorWindCATemp" ,

(max(T.HstFntMtrMotorTemp)* 1.8 + 32) as "HstFntMtrMotorTemp" ,

(max(T.HstFntMtrMotorWindABTemp)* 1.8 + 32) as "HstFntMtrMotorWindABTemp" ,

(max(T.HstFntMtrMotorWindBCTemp)* 1.8 + 32) as "HstFntMtrMotorWindBCTemp" ,

(max(T.HstFntMtrMotorWindCATemp)* 1.8 + 32) as "HstFntMtrMotorWindCATemp" ,

(max(T.SwgRearMtrMotorTemp)* 1.8 + 32) as "SwgRearMtrMotorTemp" ,

(max(T.SwgRearMtrMotorWindABTemp)* 1.8 + 32) as "SwgRearMtrMotorWindABTemp" ,

(max(T.SwgRearMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRearMtrMotorWindBCTemp" ,

(max(T.SwgRearMtrMotorWindCATemp)* 1.8 + 32) as "SwgRearMtrMotorWindCATemp" ,

(max(T.SwgFntMtrMotorTemp)* 1.8 + 32) as "SwgFntMtrMotorTemp" ,

(max(T.SwgFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgFntMtrMotorWindABTemp" ,

(max(T.SwgFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.SwgFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.ACS800Temp)* 1.8 + 32) as "ACS800Temp" ,

(max(T.TempDiffPhsU)* 1.8 + 32) as "TempDiffPhsU" ,

(max(T.TempDiffPhsV)* 1.8 + 32) as "TempDiffPhsV" ,

(max(T.TempDiffPhsW)* 1.8 + 32) as "TempDiffPhsW" ,

Max(T.DateTime) as "TemperatureTime"

FROM TempMtrScan T

JOIN Shovel S ON T.ShovelID = S.ID

WHERE (S.ID =@.ShovelID) AND T.DATETIME <= @.EndTime AND T.DATETIME >= @.StartTime

Group by ShovelID, datepart(yy,T.DateTime), datepart(dy,T.DateTime),datepart(hh,T.DateTime),datepart(mi,T.DateTime)/@.Duration

order by Max(T.DateTime) asc

(1943 row(s) affected)

Table 'Shovel'. Scan count 0, logical reads 2, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 3, logical reads 38911, physical reads 493, read-ahead reads 736, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Warning: Null value is eliminated by an aggregate or other SET operation.

First time it times out after that it is fine

I have a stored procedure that runs slowly and takes over 60 seconds sometimes, but almost all of the time it takes only one or two seconds. In both cases the same input data is used and there have been no changes to the table. The slow running is after the query has not been run for several days.

In the background 16 records are inserted every minute. The table has 5 million records. There are two indexes on the table one is clustered the other is unclustered. They are very well tuned. The record contains a two integers and 90 floats.

The stored procedure gets one record from the database which is the latest record for a particular entity. If I go to the web site after a few days the page times out. If I go to Mgmt Studio, and run the sp form there it takes a very long time but eventually comes back with the correct answer. If execute the stored procedure after that the response is a second or two using the same input data. If I change the input data the response is equally as fast. The web site also works very quickly too.

What is going on and how do I fix it?

Pops73

Try running the SP with SET STATISTICS IO ON, then look at the messages tab. It sounds like the first time you run the query, it is having to do physical reads as opposed to logical reads. The second time you run it, it is doing logical reads, which would be much, much faster.|||

Thank you for the response and it sounds reasonable. I restarted the server which I thought would clear the buffer cache and cause the problem to occur. But it didn't after the restart the timeout did not occur. Response was good. Is the buffer cache saved through a restart of a server? How can I clear the cache to easily reproduce the problem and gather the statistics?

Pops73

|||

Restarting the server does clear the the buffer cache. You can run the command DBCC FREEPROCCACHE to clear out the procedure cache, and DBCC DROPCLEANBUFFERS to flush the data cache.

http://msdn2.microsoft.com/en-US/library/ms187762.aspx

Run these commands:

DBCC FREEPROCCACHE

GO

CHECKPOINT

GO

DBCC DROPCLEANBUFFERS

|||

Thank you for your help. This still does not reproduce the problem.

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

More info -- I moved the indexes. I have the data in one file group with three files and the indexes in another file group with two files. I still saw the problem.

running Sql Server 2005 sp1

I had the problem yesterday in another db instance of the same applicaiton on another machine. This table only had one record instead of 5 million. Two different queries will show the same problem

Part of the table definition

CREATE TABLE [dbo].[TempMtrScan](

[ID] [bigint] IDENTITY(1,1) NOT NULL,

[ShovelID] [int] NOT NULL,

[DateTime] [datetime] NOT NULL,

[FHoistNDE] [float] NULL,

[FHoistDE] [float] NULL,

[FHoistIP] [float] NULL,

[FHoistFLD] [float] NULL, ............

Query 1

INSERT INTO @.TempMtrScanUnpivot ([ShovelID],[TempMtrScanName], [Temperature])

SELECT ShovelID, TempMtrScanName, Temperature

FROM

(SELECT ShovelID,[DateTime],

FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

FROM dbo.TempMtrScan

WHERE [Datetime] =

(SELECT MAX([datetime])

FROM dbo.TempMtrScan

WHERE ShovelID = @.ShovelID)

AND ShovelID = @.ShovelID) A

UNPIVOT (

Temperature

FOR TempMtrScanName

IN ( FHoistNDE,FHoistDE,

RHoistNDE,RHoistDE,

CrowdNDE,CrowdDE,

SwingNDE,SwingDE,

RearSwingNDE,RearSwingDE,

RPropelNDE,RPropelDE,

LPropelNDE,LPropelDE,

LFrontSwingNDE,LFrontSwingDE,

RFrontSwingNDE,RFrontSwingDE,

TransPhaseA,TransPhaseB,TransPhaseC,

DriveInletTemp1,DriveInletTemp2,DriveInletTemp3,DriveInletTemp4,

DriveInletTemp5,DriveInletTemp6,DriveInletTemp7,DriveInletTemp8,

CwdMotorTemp,CwdMotorWindABTemp,CwdMotorWindBCTemp,CwdMotorWindCATemp,

PplRhtMtrMotorTemp,PplRhtMtrMotorWindABTemp,PplRhtMtrMotorWindBCTemp,PplRhtMtrMotorWindCATemp,

SwgLFntMtrMotorTemp,SwgLFntMtrMotorWindABTemp,SwgLFntMtrMotorWindBCTemp,SwgLFntMtrMotorWindCATemp,

HstRearMtrMotorTemp,HstRearMtrMotorWindABTemp,HstRearMtrMotorWindBCTemp,HstRearMtrMotorWindCATemp,

PplLftMtrMotorTemp,PplLftMtrMotorWindABTemp,PplLftMtrMotorWindBCTemp,PplLftMtrMotorWindCATemp,

SwgRFntMtrMotorTemp,SwgRFntMtrMotorWindABTemp,SwgRFntMtrMotorWindBCTemp,SwgRFntMtrMotorWindCATemp,

HstFntMtrMotorTemp,HstFntMtrMotorWindABTemp,HstFntMtrMotorWindBCTemp,HstFntMtrMotorWindCATemp,

SwgRearMtrMotorTemp,SwgRearMtrMotorWindABTemp,SwgRearMtrMotorWindBCTemp,SwgRearMtrMotorWindCATemp,

SwgFntMtrMotorTemp, SwgFntMtrMotorWindABTemp, SwgFntMtrMotorWindBCTemp, SwgFntMtrMotorWindCATemp, ACS800Temp,

TempDiffPhsU, TempDiffPhsV, TempDiffPhsW

)

) AS unpvt

Table '#7F0FE051'. Scan count 0, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 1, logical reads 8, physical reads 3, read-ahead reads 607, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Query 2

SELECT

(max(T.FHoistNDE)* 1.8 + 32) as "FHoistNDE" ,

(max(T.FHoistDE)* 1.8 + 32) as "FHoistDE" ,

(max(T.FHoistIP)* 1.8 + 32) as "FHoistIP" ,

(max(T.FHoistFLD)* 1.8 + 32) as "FHoistFLD" ,

(max(T.RHoistNDE)* 1.8 + 32) as "RHoistNDE" ,

(max(T.RHoistDE)* 1.8 + 32) as "RHoistDE" ,

(max(T.RHoistIP)* 1.8 + 32) as "RHoistIP" ,

(max(T.RHoistFLD)* 1.8 + 32) as "RHoistFLD" ,

(max(T.CrowdNDE)* 1.8 + 32) as "CrowdNDE" ,

(max(T.CrowdDE) * 1.8 + 32) as "CrowdDE" ,

(max(T.CrowdIP) * 1.8 + 32) as "CrowdIP" ,

(max(T.CrowdFLD) * 1.8 + 32) as "CrowdFLD" ,

(max(T.SwingNDE)* 1.8 + 32) as "SwingNDE" ,

(max(T.SwingDE) * 1.8 + 32) as "SwingDE" ,

(max(T.SwingIP) * 1.8 + 32) as "SwingIP" ,

(max(T.SwingFLD)* 1.8 + 32) as "SwingFLD" ,

(max(T.RearSwingNDE)* 1.8 + 32) as "RearSwingNDE" ,

(max(T.RearSwingDE) * 1.8 + 32) as "RearSwingDE" ,

(max(T.RearSwingIP) * 1.8 + 32) as "RearSwingIP" ,

(max(T.RearSwingFLD)* 1.8 + 32) as "RearSwingFLD" ,

(max(T.RPropelNDE) * 1.8 + 32) as "RPropelNDE" ,

(max(T.RPropelDE)* 1.8 + 32) as "RPropelDE" ,

(max(T.RPropelIP) * 1.8 + 32) as "RPropelIP" ,

(max(T.RPropelFLD) * 1.8 + 32) as "RPropelFLD" ,

(max(T.LPropelNDE) * 1.8 + 32) as "LPropelNDE" ,

(max(T.LPropelDE) * 1.8 + 32) as "LPropelDE" ,

(max(T.LPropelIP) * 1.8 + 32) as "LPropelIP" ,

(max(T.LPropelFLD) * 1.8 + 32) as "LPropelFLD" ,

(max(T.LFrontSwingNDE)* 1.8 + 32) as "LFrontSwingNDE" ,

(max(T.LFrontSwingDE)* 1.8 + 32) as "LFrontSwingDE" ,

(max(T.LFrontSwingIP) * 1.8 + 32) as "LFrontSwingIP" ,

(max(T.LFrontSwingFLD) * 1.8 + 32) as "LFrontSwingFLD" ,

(max(T.RFrontSwingNDE) * 1.8 + 32) as "RFrontSwingNDE" ,

(max(T.RFrontSwingDE) * 1.8 + 32) as "RFrontSwingDE" ,

(max(T.RFrontSwingIP)* 1.8 + 32) as "RFrontSwingIP" ,

(max(T.RFrontSwingFLD) * 1.8 + 32) as "RFrontSwingFLD" ,

(max(T.TransPhaseA)* 1.8 + 32) as "TransPhaseA" ,

(max(T.TransPhaseB)* 1.8 + 32) as "TransPhaseB" ,

(max(T.TransPhaseC)* 1.8 + 32) as "TransPhaseC" ,

(max(T.DriveInletTemp1)* 1.8 + 32) as "DriveInletTemp1" ,

(max(T.DriveInletTemp2)* 1.8 + 32) as "DriveInletTemp2" ,

(max(T.DriveInletTemp3)* 1.8 + 32) as "DriveInletTemp3" ,

(max(T.DriveInletTemp4)* 1.8 + 32) as "DriveInletTemp4" ,

(max(T.DriveInletTemp5)* 1.8 + 32) as "DriveInletTemp5" ,

(max(T.DriveInletTemp6)* 1.8 + 32) as "DriveInletTemp6" ,

(max(T.DriveInletTemp7)* 1.8 + 32) as "DriveInletTemp7" ,

(max(T.DriveInletTemp8)* 1.8 + 32) as "DriveInletTemp8" ,

(max(T.CwdMotorTemp)* 1.8 + 32) as "CwdMotorTemp" ,

(max(T.CwdMotorWindABTemp)* 1.8 + 32) as "CwdMotorWindABTemp" ,

(max(T.CwdMotorWindBCTemp)* 1.8 + 32) as "CwdMotorWindBCTemp" ,

(max(T.CwdMotorWindCATemp)* 1.8 + 32) as "CwdMotorWindCATemp" ,

(max(T.PplRhtMtrMotorTemp)* 1.8 + 32) as "PplRhtMtrMotorTemp" ,

(max(T.PplRhtMtrMotorWindABTemp)* 1.8 + 32) as "PplRhtMtrMotorWindABTemp" ,

(max(T.PplRhtMtrMotorWindBCTemp)* 1.8 + 32) as "PplRhtMtrMotorWindBCTemp" ,

(max(T.PplRhtMtrMotorWindCATemp)* 1.8 + 32) as "PplRhtMtrMotorWindCATemp" ,

(max(T.SwgLFntMtrMotorTemp)* 1.8 + 32) as "SwgLFntMtrMotorTemp" ,

(max(T.SwgLFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindABTemp" ,

(max(T.SwgLFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgLFntMtrMotorWindBCTemp" ,

(max(T.SwgLFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgLFntMtrMotorWindCATemp" ,

(max(T.HstRearMtrMotorTemp)* 1.8 + 32) as "HstRearMtrMotorTemp" ,

(max(T.HstRearMtrMotorWindABTemp)* 1.8 + 32) as "HstRearMtrMotorWindABTemp" ,

(max(T.HstRearMtrMotorWindBCTemp)* 1.8 + 32) as "HstRearMtrMotorWindBCTemp" ,

(max(T.HstRearMtrMotorWindCATemp)* 1.8 + 32) as "HstRearMtrMotorWindCATemp" ,

(max(T.PplLftMtrMotorTemp)* 1.8 + 32) as "PplLftMtrMotorTemp" ,

(max(T.PplLftMtrMotorWindABTemp)* 1.8 + 32) as "PplLftMtrMotorWindABTemp" ,

(max(T.PplLftMtrMotorWindBCTemp)* 1.8 + 32) as "PplLftMtrMotorWindBCTemp" ,

(max(T.PplLftMtrMotorWindCATemp)* 1.8 + 32) as "PplLftMtrMotorWindCATemp" ,

(max(T.SwgRFntMtrMotorTemp)* 1.8 + 32) as "SwgRFntMtrMotorTemp" ,

(max(T.SwgRFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindABTemp" ,

(max(T.SwgRFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRFntMtrMotorWindBCTemp" ,

(max(T.SwgRFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgRFntMtrMotorWindCATemp" ,

(max(T.HstFntMtrMotorTemp)* 1.8 + 32) as "HstFntMtrMotorTemp" ,

(max(T.HstFntMtrMotorWindABTemp)* 1.8 + 32) as "HstFntMtrMotorWindABTemp" ,

(max(T.HstFntMtrMotorWindBCTemp)* 1.8 + 32) as "HstFntMtrMotorWindBCTemp" ,

(max(T.HstFntMtrMotorWindCATemp)* 1.8 + 32) as "HstFntMtrMotorWindCATemp" ,

(max(T.SwgRearMtrMotorTemp)* 1.8 + 32) as "SwgRearMtrMotorTemp" ,

(max(T.SwgRearMtrMotorWindABTemp)* 1.8 + 32) as "SwgRearMtrMotorWindABTemp" ,

(max(T.SwgRearMtrMotorWindBCTemp)* 1.8 + 32) as "SwgRearMtrMotorWindBCTemp" ,

(max(T.SwgRearMtrMotorWindCATemp)* 1.8 + 32) as "SwgRearMtrMotorWindCATemp" ,

(max(T.SwgFntMtrMotorTemp)* 1.8 + 32) as "SwgFntMtrMotorTemp" ,

(max(T.SwgFntMtrMotorWindABTemp)* 1.8 + 32) as "SwgFntMtrMotorWindABTemp" ,

(max(T.SwgFntMtrMotorWindBCTemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.SwgFntMtrMotorWindCATemp)* 1.8 + 32) as "SwgFntMtrMotorWindBCTemp" ,

(max(T.ACS800Temp)* 1.8 + 32) as "ACS800Temp" ,

(max(T.TempDiffPhsU)* 1.8 + 32) as "TempDiffPhsU" ,

(max(T.TempDiffPhsV)* 1.8 + 32) as "TempDiffPhsV" ,

(max(T.TempDiffPhsW)* 1.8 + 32) as "TempDiffPhsW" ,

Max(T.DateTime) as "TemperatureTime"

FROM TempMtrScan T

JOIN Shovel S ON T.ShovelID = S.ID

WHERE (S.ID =@.ShovelID) AND T.DATETIME <= @.EndTime AND T.DATETIME >= @.StartTime

Group by ShovelID, datepart(yy,T.DateTime), datepart(dy,T.DateTime),datepart(hh,T.DateTime),datepart(mi,T.DateTime)/@.Duration

order by Max(T.DateTime) asc

(1943 row(s) affected)

Table 'Shovel'. Scan count 0, logical reads 2, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'TempMtrScan'. Scan count 3, logical reads 38911, physical reads 493, read-ahead reads 736, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Warning: Null value is eliminated by an aggregate or other SET operation.

2012年2月24日星期五

First attempt at Stored Procedure - can anyone offer advice

SQL SERVER 2000

Hi all

This is my first attempt at writing a stored procedure. I have managed to
get it working but its unlikely to be the best way of handling the problem.
While writing it I found some things that I don't understand so if any one
could shed any light it would be much appreciated. I have posted these at
the end.

Sorry about the length but I thought it might be worthwhile posting the code

The purpose of the procedures is as follows : we have a view of lots of bits
of information that need automatically mailing to different people. each
element of information has a name allocated against it. If we had 100 pieces
of data, 50 could go to manager 1 25 could go to manager 2 and 25 to manager
3 etc...

Both SP's look at the same view

The first SP generates a distinct list of managers and for each manager
calls the second SP

The second SP filters the view for the data belonging to the selected
manager, and builds an HTML mail. It then sends all the bits of information
belonging to that manager off in an EMAIL to him/her. ( It uses a brilliant
bit of code from sqldev.net to handle the mail)

the first mail then repeats for all the managers in the list

CODE -- SP 1
ALTER PROCEDURE dbo.PR_ADMIN_CLIENT_WEEKLY_NOTIFICATION_2
AS
begin
SET NOCOUNT ON
declare @.no_of_managers as int
declare @.current_record as int
declare @.manager_name as varchar(100)

-- count how many distinct managers we need to send the mail to
select @.no_of_managers = COUNT(DISTINCT manager_name) FROM
dbo.vw_client_notification_email_1

-- open a cursor to the same distinct list
declare email_list cursor for select distinct manager_name from
dbo.vw_client_notification_email_1 dsc
open email_list

-- for each distinct manager get the managers name and pass it to the stored
procedure that generates the mail.
set @.current_record = 0
while (@.current_record) < @.no_of_managers
begin
fetch next from email_list into @.manager_name
EXECUTE dbo.pr_admin_client_weekly_notification @.manager_name
set @.current_record = @.current_record+1
end
-- close the cursor
close email_list
deallocate email_list
end

CODE -- SP2
ALTER PROCEDURE dbo.PR_ADMIN_CLIENT_WEEKLY_NOTIFICATION
(@.current_manager_name as varchar(100))
-- a unique managers name is passed from the calling procedure
as begin
SET NOCOUNT ON
-- declarations for use in the stored procedure
DECLARE @.to as varchar(100)
DECLARE @.entry varchar(500)
DECLARE @.region as varchar(100)
DECLARE @.type as varchar(100)
DECLARE @.site_ref as varchar(100)
DECLARE @.aborted as varchar(100)
DECLARE @.weblink as varchar(1000)
DECLARE @.manager_name as varchar(100)
DECLARE @.manager_email as varchar(100)
DECLARE @.body VARCHAR(8000)
DECLARE @.link varchar(150)
DECLARE @.web_base VARCHAR(150)

-- set up a connection to the view that contains the details for the mail

DECLARE email_contents cursor for select region,type,
site_ref,aborted_visit,link,manager_name,manager_e mail from
vw_client_notification_email_1 where manager_name = @.current_manager_name
open email_contents
--some initial text
set @.body = '<font color="#FF8040"><b>Reports W/E ' +convert(char(50),
getdate()) + '</b></font><br><br> <a href = http://xxxx > Click here to log
on to xxxxx </a><br><br> '
--fetch the first matching record from the table and build the body of the
message
fetch next from email_contents into
@.region,@.type,@.site_ref,@.aborted,@.link,@.manager_na me,@.manager_email
set @.web_base = 'http://'
set @.weblink = @.web_base + @.link
if @.aborted = 0 set @.aborted = '' else set @.aborted = 'ABORTED'
set @.body = @.body + '<font size="2"><b> Region </b>' + @.region
+ ' <b>Type</b> ' + @.type
+ ' <b>Site Reference </b> <a href = "' + @.weblink + '">' + @.site_ref+
'</a>'
+ ' <b>Unique Report Reference </b>' + @.link + '<br>'

-- continue reading the records for this particular message and adding on to
the body of the text
while(@.@.fetch_status = 0)
begin
fetch next from email_contents into
@.region,@.type,@.site_ref,@.aborted,@.link,@.manager_na me,@.manager_email
if @.aborted = 0 set @.aborted = '' else set @.aborted = 'ABORTED'
if (@.@.fetch_status = 0) set @.body = @.body + '<b> Region </b>' + @.region
+ ' <b>Type</b> ' + @.type
+ ' <b>Site Reference </b> <a href = "' + @.weblink + '">' + @.site_ref+
'</a>'
+ '<b>Unique Report Reference </b>' + @.link + '<br>'
end

-- close the cursor
set @.body = @.body + '</font>'
close email_contents
deallocate email_contents
-- generate the mail
DECLARE @.rc int EXEC @.rc = master.dbo.xp_smtp_sendmail
@.FROM = N'FROM ME',
@.TO = @.manager_email,
@.server = N'server',
@.subject = N'Weekly Import',
@.message = @.body,
@.type = N'text/html'

end

Questions

is the way I've done it OK. I thought I would be able to do it in a single
SP but I really struggled nesting the cursor things.

@.@.fetchstatus seems to be global, so if your using nested cursors, how do
you know which one you are refering to. If you have multiple calls to the
same SP how does it know which instance of the SP it refers to.

When I first wrote it, I used a cursor in SP1 to call SP2, but I couldn't
get the while loop working - I have a feeling it was down to the @.@.
fetchstatus in the 'calling' procedure being overwritten by the
@.@.fetchstatus in the 'called' procedure.

The whole @.@.fetchatus thing seems a bit odd. In the second procedure, I have
to fetch, then check, manipulate then fetch again, meaning that the same
manipulation code is written twice. thats why in the first procedure I used
the select distint count to know how long the record set is so I only have
to run the manipulation code once. Is what I have done wrong?

its possible that the body of the mail could be > 8K, is there another
datatype I can use to hold more than 8K

many thanks for any help or advice

Andy"aaj" <a.b@.c.com> wrote in message
news:40236978$0$15408$afc38c87@.news.easynet.co.uk. ..
> SQL SERVER 2000
> Hi all
> This is my first attempt at writing a stored procedure. I have managed to
> get it working but its unlikely to be the best way of handling the
problem.
> While writing it I found some things that I don't understand so if any one
> could shed any light it would be much appreciated. I have posted these at
> the end.
> Sorry about the length but I thought it might be worthwhile posting the
code

<snip
> Questions
> is the way I've done it OK. I thought I would be able to do it in a single
> SP but I really struggled nesting the cursor things.
> @.@.fetchstatus seems to be global, so if your using nested cursors, how do
> you know which one you are refering to. If you have multiple calls to the
> same SP how does it know which instance of the SP it refers to.
> When I first wrote it, I used a cursor in SP1 to call SP2, but I couldn't
> get the while loop working - I have a feeling it was down to the @.@.
> fetchstatus in the 'calling' procedure being overwritten by the
> @.@.fetchstatus in the 'called' procedure.
> The whole @.@.fetchatus thing seems a bit odd. In the second procedure, I
have
> to fetch, then check, manipulate then fetch again, meaning that the same
> manipulation code is written twice. thats why in the first procedure I
used
> the select distint count to know how long the record set is so I only have
> to run the manipulation code once. Is what I have done wrong?
> its possible that the body of the mail could be > 8K, is there another
> datatype I can use to hold more than 8K
>
> many thanks for any help or advice
> Andy

I must admit I didn't read your code in detail, but I'm not sure why you
need two procedures. The inner one appears to go through every row in
dbo.vw_client_notification_email_1, so I don't see the benefit of the outer
one (unless perhaps you removed some code to simplify it). Also, the outer
procedure seems to use a counter to find the end of the cursor, but
@.@.FETCH_STATUS will tell you when you have reached the end of the cursor
anyway.

As for the data type, there are larger data types available (text and
ntext), but xp_smtp_sendmail doesn't support them. If you need to send large
emails, I would consider using an external script instead of pure SQL code -
it's much easier to create attachment files, do text/HTML formatting and
validation etc.If you want to keep control within a procedure, then you
could use xp_cmdshell to call your script, or perhaps just schedule it as a
SQL Agent job.

Simon|||aaj (a.b@.c.com) writes:
> is the way I've done it OK. I thought I would be able to do it in a single
> SP but I really struggled nesting the cursor things.

It could be done in a single SP, but splitting it in two has the
advantage that you don't risk that the value of some variable spill
over from the previous manager.

You could also do it with one cursor only. In this case you would
have something like:

IF @.old_manager IS NOT NULL AND @.manager <> @.old_manager
BEGIN
EXEC master.dbo.xp_sendmail ...
-- Reset all variables.
SELECT @.old_manager = @.manager
END

You could of course have to order the cursor by manager.

> @.@.fetchstatus seems to be global, so if your using nested cursors, how do
> you know which one you are refering to. If you have multiple calls to the
> same SP how does it know which instance of the SP it refers to.

@.@.fetch_status refers to the cursor you last operated on. And if you
check @.@.fetch_status directly after FETCH you are safe. Here is the
idiom for writing a cursor loop:

DECLARE some_cur INSENSITIVE CURSOR FOR
SELECT yadayada

OPEN some_cur

WHILE 1 = 1
BEGIN
FETCH some_cur INTO ...
IF @.@.fetch_status <> 0
BREAK
...
END

DEALLOCATE some_cur

The INSENSITIVE is there, because the default keyset-driven cursors can
sometimes come with nasty suprises.

By only using one FETCH statement your code is easier to maintain; if you
need another column in the cursor, you only have to change in two places.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks chaps

I'll have a read in detail and see if I can pick up some tips

Andy

"aaj" <a.b@.c.com> wrote in message
news:40236978$0$15408$afc38c87@.news.easynet.co.uk. ..
> SQL SERVER 2000
> Hi all
> This is my first attempt at writing a stored procedure. I have managed to
> get it working but its unlikely to be the best way of handling the
problem.
> While writing it I found some things that I don't understand so if any one
> could shed any light it would be much appreciated. I have posted these at
> the end.
> Sorry about the length but I thought it might be worthwhile posting the
code
> The purpose of the procedures is as follows : we have a view of lots of
bits
> of information that need automatically mailing to different people. each
> element of information has a name allocated against it. If we had 100
pieces
> of data, 50 could go to manager 1 25 could go to manager 2 and 25 to
manager
> 3 etc...
> Both SP's look at the same view
> The first SP generates a distinct list of managers and for each manager
> calls the second SP
> The second SP filters the view for the data belonging to the selected
> manager, and builds an HTML mail. It then sends all the bits of
information
> belonging to that manager off in an EMAIL to him/her. ( It uses a
brilliant
> bit of code from sqldev.net to handle the mail)
> the first mail then repeats for all the managers in the list
> CODE -- SP 1
> ALTER PROCEDURE dbo.PR_ADMIN_CLIENT_WEEKLY_NOTIFICATION_2
> AS
> begin
> SET NOCOUNT ON
> declare @.no_of_managers as int
> declare @.current_record as int
> declare @.manager_name as varchar(100)
> -- count how many distinct managers we need to send the mail to
> select @.no_of_managers = COUNT(DISTINCT manager_name) FROM
> dbo.vw_client_notification_email_1
> -- open a cursor to the same distinct list
> declare email_list cursor for select distinct manager_name from
> dbo.vw_client_notification_email_1 dsc
> open email_list
> -- for each distinct manager get the managers name and pass it to the
stored
> procedure that generates the mail.
> set @.current_record = 0
> while (@.current_record) < @.no_of_managers
> begin
> fetch next from email_list into @.manager_name
> EXECUTE dbo.pr_admin_client_weekly_notification @.manager_name
> set @.current_record = @.current_record+1
> end
> -- close the cursor
> close email_list
> deallocate email_list
> end
>
> CODE -- SP2
> ALTER PROCEDURE dbo.PR_ADMIN_CLIENT_WEEKLY_NOTIFICATION
> (@.current_manager_name as varchar(100))
> -- a unique managers name is passed from the calling procedure
> as begin
> SET NOCOUNT ON
> -- declarations for use in the stored procedure
> DECLARE @.to as varchar(100)
> DECLARE @.entry varchar(500)
> DECLARE @.region as varchar(100)
> DECLARE @.type as varchar(100)
> DECLARE @.site_ref as varchar(100)
> DECLARE @.aborted as varchar(100)
> DECLARE @.weblink as varchar(1000)
> DECLARE @.manager_name as varchar(100)
> DECLARE @.manager_email as varchar(100)
> DECLARE @.body VARCHAR(8000)
> DECLARE @.link varchar(150)
> DECLARE @.web_base VARCHAR(150)
> -- set up a connection to the view that contains the details for the mail
> DECLARE email_contents cursor for select region,type,
> site_ref,aborted_visit,link,manager_name,manager_e mail from
> vw_client_notification_email_1 where manager_name = @.current_manager_name
> open email_contents
> --some initial text
> set @.body = '<font color="#FF8040"><b>Reports W/E ' +convert(char(50),
> getdate()) + '</b></font><br><br> <a href = http://xxxx > Click here to
log
> on to xxxxx </a><br><br> '
> --fetch the first matching record from the table and build the body of the
> message
> fetch next from email_contents into
> @.region,@.type,@.site_ref,@.aborted,@.link,@.manager_na me,@.manager_email
> set @.web_base = 'http://'
> set @.weblink = @.web_base + @.link
> if @.aborted = 0 set @.aborted = '' else set @.aborted = 'ABORTED'
> set @.body = @.body + '<font size="2"><b> Region </b>' + @.region
> + ' <b>Type</b> ' + @.type
> + ' <b>Site Reference </b> <a href = "' + @.weblink + '">' + @.site_ref+
> '</a>'
> + ' <b>Unique Report Reference </b>' + @.link + '<br>'
> -- continue reading the records for this particular message and adding on
to
> the body of the text
> while(@.@.fetch_status = 0)
> begin
> fetch next from email_contents into
> @.region,@.type,@.site_ref,@.aborted,@.link,@.manager_na me,@.manager_email
> if @.aborted = 0 set @.aborted = '' else set @.aborted = 'ABORTED'
> if (@.@.fetch_status = 0) set @.body = @.body + '<b> Region </b>' + @.region
> + ' <b>Type</b> ' + @.type
> + ' <b>Site Reference </b> <a href = "' + @.weblink + '">' + @.site_ref+
> '</a>'
> + '<b>Unique Report Reference </b>' + @.link + '<br>'
> end
> -- close the cursor
> set @.body = @.body + '</font>'
> close email_contents
> deallocate email_contents
> -- generate the mail
> DECLARE @.rc int EXEC @.rc = master.dbo.xp_smtp_sendmail
> @.FROM = N'FROM ME',
> @.TO = @.manager_email,
> @.server = N'server',
> @.subject = N'Weekly Import',
> @.message = @.body,
> @.type = N'text/html'
>
> end
>
>
>
> Questions
> is the way I've done it OK. I thought I would be able to do it in a single
> SP but I really struggled nesting the cursor things.
> @.@.fetchstatus seems to be global, so if your using nested cursors, how do
> you know which one you are refering to. If you have multiple calls to the
> same SP how does it know which instance of the SP it refers to.
> When I first wrote it, I used a cursor in SP1 to call SP2, but I couldn't
> get the while loop working - I have a feeling it was down to the @.@.
> fetchstatus in the 'calling' procedure being overwritten by the
> @.@.fetchstatus in the 'called' procedure.
> The whole @.@.fetchatus thing seems a bit odd. In the second procedure, I
have
> to fetch, then check, manipulate then fetch again, meaning that the same
> manipulation code is written twice. thats why in the first procedure I
used
> the select distint count to know how long the record set is so I only have
> to run the manipulation code once. Is what I have done wrong?
> its possible that the body of the mail could be > 8K, is there another
> datatype I can use to hold more than 8K
>
> many thanks for any help or advice
> Andy

Firing already made DTS Package -- from vb.net

Someone please help me with this.

I'm trying to fire off an already created DTS package. This package is stored within SQL Server's -- underneith the Data Transformation Services / Local Packages section.

HOW CAN I FIRE THIS OFF FROM A VB .NET APPLICATION

I'm familiar with strored procedures and using them in vb.net so if somone could lead me down that road I would be very much appriciated.

Thanks in advance everyone,

RB

Try this url you may find your solution. Hope this helps.

http://www.sqldts.com

|||

I have this in my stored proc already:

Here is what I am trying to run within my stored proc:

dtsrun /F'CSI_DTSPackages/GTProPaymentExtract.dts'/N'GTProPaymentExtract'/M''

PLEASE TELL ME WHAT I'M DOING WRONG!!!

Thanks in advance everyone.

RB

|||

I gave you Darren Green's site because if you can do it in DTS he may have done it. I have found another link, it may solve your problem. Hope this helps.

http://www.vbdotnetheaven.com/Code/Jun2003/2070.asp

|||

here a stored procedure I got from the DTS site. As this fires off a DTS package on the server your path will have to be a UNC path. In my case the path is source to a file I am reading data from.

CREATE PROCEDURE spExecuteDTSPKG
@.Server varchar(255),
@.PkgName varchar(255), -- Package Name (Defaults to most recent version)
@.ConnName varchar(25) = '',
@.XLSPath varchar(255) = '', -- Path to Source File
@.ServerPWD varchar(255) = Null, -- Server Password if using SQL Security to load Package (UID is SUSER_NAME())
@.IntSecurity bit = 1, -- 0 = SQL Server Security, 1 = Integrated Security
@.PkgPWD varchar(255) = '' -- Package Password
AS
SET NOCOUNT ON

DECLARE @.hr int, @.ret int, @.oPKG int, @.Cmd varchar(1000), @.Connection varchar(255)
Select @.Connection = 'Connections.Item(' + @.ConnName + ').DataSource'
Select @.ret = 0

-- Create a Pkg Object
EXEC @.hr = sp_OACreate 'DTS.Package', @.oPKG OUTPUT


SET @.Cmd = 'LoadFromSQLServer("' + @.Server +'", "", "", 256, "' + @.PkgPWD + '", , , "' + @.PkgName + '")'

EXEC @.hr = sp_OAMethod @.oPKG, @.Cmd, NULL

EXEC @.hr = sp_OASetProperty @.oPKG, @.Connection, @.XLSPath
If @.hr <> 0
BEGIN
PRINT '*** OASetProperty for Connection Failed***'
EXEC sp_OAGetErrorInfo @.oPkg
RETURN
END

-- Execute Pkg
EXEC @.hr = sp_OAMethod @.oPKG, 'Execute'

-- Unitialize the Pkg
EXEC @.hr = sp_OAMethod @.oPKG, 'UnInitialize'

-- Clean Up
EXEC @.hr = sp_OADestroy @.oPKG

RETURN @.ret
GO

Firing a java application from stored procedure

Hey all,
I've got a question and after doing some research I've found only a vague reference but no clear answer.

I have a java app that will be passing parameters to my stored procedure. I'll grab the requested info from the tables but instead of sending it back to the java app that sent the request, I need to send it to a "different" java app (the second java app will not be running at the time).

Can someone point me to a good source for executing java applications from a stored procedure?

Thanks in advance ...
tamSee this http://www.onjava.com/pub/a/onjava/2003/08/13/stored_procedures.html link is any help.

Another link http://www.sswug.org/searchresults.asp%3Fkeywordstofind%3Djava,%2520sto red%2520procedures for information.

2012年2月19日星期日

Fireshose mode

About 2 times a week our SQL Server 2000 system will go into "firehose
mode" and we aren't able to run certain stored procedures. It seems
the ones that are affected are any procedures accessing linked
servers. The only way we can solve the problem is by restarting the
sql service on the server. This is starting to become a drag on our
business because all data processing stops when restart SQL. Has
anybody else encountered this issue with your systems?
Thanks,
JK
PS - We are not using Enterprise Manager so that is not the cause of
the problem. We are suspicous of the SQL Object Browser in Query
Analyzer and wonder if that uses the same "firehose" cursor as EM.Hello JK,
These links will help you to work on your problem.
PRB: SQL Enterprise Manager Returns "Cannot Start
Transaction While in Firehose Mode" Error
http://www.support.microsoft.com/?id=237398
FIX: Cursor Overhead Higher on SQL Server 7.0 for Small
Result Sets
http://support.microsoft.com/support/kb/articles/Q197/8/00.
ASP
Good Luck!
-SQLVarad (MCDBA-1999,MCSE-1999)
>--Original Message--
>About 2 times a week our SQL Server 2000 system will go
into "firehose
>mode" and we aren't able to run certain stored
procedures. It seems
>the ones that are affected are any procedures accessing
linked
>servers. The only way we can solve the problem is by
restarting the
>sql service on the server. This is starting to become a
drag on our
>business because all data processing stops when restart
SQL. Has
>anybody else encountered this issue with your systems?
>Thanks,
>JK
>PS - We are not using Enterprise Manager so that is not
the cause of
>the problem. We are suspicous of the SQL Object Browser
in Query
>Analyzer and wonder if that uses the same "firehose"
cursor as EM.
>.
>|||One of those articles references Enterprise Manager, which we are not
using. The other one talks about SQL Server 7.0, which we are not
using.
Thanks for your help.
"SQLVarad" <SQLVarad@.hotmail.com> wrote in message news:<074101c3ad67$6182baa0$a101280a@.phx.gbl>...
> Hello JK,
> These links will help you to work on your problem.
> PRB: SQL Enterprise Manager Returns "Cannot Start
> Transaction While in Firehose Mode" Error
> http://www.support.microsoft.com/?id=237398
> FIX: Cursor Overhead Higher on SQL Server 7.0 for Small
> Result Sets
> http://support.microsoft.com/support/kb/articles/Q197/8/00.
> ASP
> Good Luck!
> -SQLVarad (MCDBA-1999,MCSE-1999)
> >--Original Message--
> >About 2 times a week our SQL Server 2000 system will go
> into "firehose
> >mode" and we aren't able to run certain stored
> procedures. It seems
> >the ones that are affected are any procedures accessing
> linked
> >servers. The only way we can solve the problem is by
> restarting the
> >sql service on the server. This is starting to become a
> drag on our
> >business because all data processing stops when restart
> SQL. Has
> >anybody else encountered this issue with your systems?
> >
> >Thanks,
> >
> >JK
> >
> >PS - We are not using Enterprise Manager so that is not
> the cause of
> >the problem. We are suspicous of the SQL Object Browser
> in Query
> >Analyzer and wonder if that uses the same "firehose"
> cursor as EM.
> >.
> >