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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to return result of temp table in proc

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 13:33:34
Greetings

Because of performance issues, I was trying to use #temp table in my proc which is being called from my front end
via adodb recordset. But the recordset has now rows.

1. Is it not possible to use #temp tables in such a scenario
2. If it is possible, how can I accomplish that.

I am hitting tables created from transaction logs every 15 min. Unfortunately, these tables are junk.
No normalization, very few indexes. #temp tables are my best solutions. But I am stuck with this one.

Thank you

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-07 : 13:40:14
1. Yes you can use #temp tables in a stored procedure, regardless of what is accessing it.
2. You haven't provided enough information for us to help you fix it. I'd start by posting the code.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 14:05:14
Here you go! I just did not want to bother you if the answer was no, but there is a Yes answer! Yippy
DECLARE @StartShipDate AS DATETIME
DECLARE @EndShipDate AS DATETIME
DECLARE @Date AS DATETIME
DECLARE @MaterialType NVARCHAR(100)
DECLARE @MaterialForm NVARCHAR(64)
DECLARE @BranchNum CHAR(2)
DECLARE @CommodityID INT

SET @StartShipDate = '4/11/2009'
SET @EndShipDate = '4/17/2009'
SET @BranchNum = '35'
SET @CommodityID = 2
SELECT @MaterialType = MaterialType
,@MaterialForm = MaterialForm
FROM dbo.Commodities AS c
WHERE c.CommodityID = @CommodityID

SET @Date = DATEADD(dd,DATEDIFF(dd,0, GETDATE()),0)



CREATE TABLE #WorkOrdersLoad(Wo# Char(6), OrdQty FLOAT, Part# CHAR(6), ShipDate DATETIME, Customer CHAR(35), MaterialType NVARCHAR(100), MaterialForm NVARCHAR(100), Branch CHAR(2))


INSERT INTO #WorkOrdersLoad
--SELECT * INTO #WorkOrdersLoad FROM (
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersBackLog AS vwobl
UNION ALL
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersUnscheduled AS vwou
UNION ALL
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersScheduled AS vwos

CREATE INDEX #WorkOrdersLoad_Branch_NU_Nidx ON #WorkOrdersLoad (Branch)
CREATE INDEX #WorkOrdersLoad_ShipDate_NU_Nidx ON #WorkOrdersLoad (ShipDate)
CREATE INDEX #WorkOrdersLoad_MaterialType_NU_Nidx ON #WorkOrdersLoad (MaterialType)
CREATE INDEX #WorkOrdersLoad_MaterialForm_NU_Nidx ON #WorkOrdersLoad (MaterialForm)

SELECT DISTINCT Wo#
,OrdQty
,Part#
,ShipDate
,Customer
,MaterialType
,MaterialForm
FROM #WorkOrdersLoad
WHERE ShipDate >= @StartShipDate
AND ShipDate <= @EndShipDate
AND Branch = @BranchNum
AND MaterialType = @MaterialType
AND MaterialForm = @MaterialForm
ORDER BY ShipDate ASC

DROP TABLE #WorkOrdersLoad

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-07 : 14:07:37
Have you run each part separately to see exactly where you aren't getting rows where you expect to? It's impossible to look at your code and know where it is incorrect unless you are purely talking about a syntax issue. Remember that we have zero knowledge about your environment, so you must be very explicit.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 14:29:46
Tara,

I have ran each section and I get rows in all situations, it is on front end that the recordset is coming out flat with no RecordCount when I use #temp table on proc side otherwise with no use of #temp tables I get the rows I need.

Thank you!

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-07 : 15:14:27
Could you show us the actual stored procedure code as I don't see CREATE PROC in what you provided, so I'm wondering if there are other things missing from your post? Also, show us the front end code that is associated with this call.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 15:19:59
Thank you for your patience...
Front end

Private Function WorkOrdersRecordSet() As Recordset
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

With cmd
.ActiveConnection = CurrentProject.Connection
.CommandType = adCmdStoredProc
.CommandText = "dbo.usp_GetWorkOrdersForWeek"
.Parameters.Append .CreateParameter("@BranchNum", adChar, adParamInput, 2, asOpenArgs(0))
.Parameters.Append .CreateParameter("CommodityID", adInteger, adParamInput, , asOpenArgs(1))
.Parameters.Append .CreateParameter("@StartShipDate", adDate, adParamInput, , asOpenArgs(2))
.Parameters.Append .CreateParameter("@EndShipDate", adDate, adParamInput, , asOpenArgs(3))
.CommandTimeout = 0
End With

Set rs = cmd.Execute


Set WorkOrdersRecordSet = rs

Set rs = Nothing
Set cmd = Nothing

End Function


PROC


IF OBJECT_ID('dbo.usp_GetWorkOrdersForWeek') IS NOT NULL
DROP PROC dbo.usp_GetWorkOrdersForWeek

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC dbo.usp_GetWorkOrdersForWeek
(
@BranchNum CHAR(4),
@CommodityID INT,
@StartShipDate DATETIME,
@EndShipDate DATETIME
)
AS
/*
-- =============================================
-- Object Name: usp_GetWorkOrdersForWeek
-- Author: -- Create date: 3/26/2009
-- Description:
-- History Date Comments
-- =============================================

*/
BEGIN
DECLARE @Date AS DATETIME
--DECLARE @BranchNum AS CHAR(4)
DECLARE @MaterialType NVARCHAR(100)
DECLARE @MaterialForm NVARCHAR(64)

SELECT @MaterialType = MaterialType
,@MaterialForm = MaterialForm
FROM dbo.Commodities AS c
WHERE c.CommodityID = @CommodityID

SET @Date = DATEADD(dd,DATEDIFF(dd,0, GETDATE()),0)


CREATE TABLE #WorkOrdersLoad(Wo# Char(6), OrdQty FLOAT, Part# CHAR(6), ShipDate DATETIME, Customer CHAR(35), MaterialType NVARCHAR(100), MaterialForm NVARCHAR(64), Branch CHAR(2))

CREATE INDEX #WorkOrdersLoad_Branch_NU_Nidx ON #WorkOrdersLoad (Branch)
CREATE INDEX #WorkOrdersLoad_ShipDate_NU_Nidx ON #WorkOrdersLoad (ShipDate)
CREATE INDEX #WorkOrdersLoad_MaterialType_NU_Nidx ON #WorkOrdersLoad (MaterialType)
CREATE INDEX #WorkOrdersLoad_MaterialForm_NU_Nidx ON #WorkOrdersLoad (MaterialForm)

INSERT INTO #WorkOrdersLoad
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersBackLog AS vwobl
UNION ALL
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersUnscheduled AS vwou
UNION ALL
SELECT DISTINCT WONumber AS Wo#
,FB_OPNWO_WAQTYO OrdQty
,FB_OPNWO_PART AS Part#
,CAST(CONVERT(CHAR(10),FB_OPNWO_SHIPDT,112) AS DATETIME) AS ShipDate
,FB_OPNWO_CUNAME AS Customer
,MaterialType
,MaterialForm
,FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersScheduled AS vwos


SELECT DISTINCT Wo#
,OrdQty
,Part#
,ShipDate
,Customer
,MaterialType
,MaterialForm
FROM #WorkOrdersLoad
WHERE ShipDate >= @StartShipDate
AND ShipDate <= @EndShipDate
AND Branch = @BranchNum
AND MaterialType = @MaterialType
AND MaterialForm = @MaterialForm
ORDER BY ShipDate ASC

DROP TABLE #WorkOrdersLoad

GO



<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 15:24:13
[code]DECLARE @StartShipDate DATETIME,
@EndShipDate DATETIME,
@Date DATETIME,
@MaterialType NVARCHAR(100),
@MaterialForm NVARCHAR(64),
@BranchNum CHAR(2),
@CommodityID INT

SELECT @StartShipDate = '4/11/2009',
@EndShipDate = '4/17/2009',
@BranchNum = '35',
@CommodityID = 2
@Date = DATEDIFF(DAY, 0, GETDATE())

SELECT @MaterialType = MaterialType,
@MaterialForm = MaterialForm
FROM dbo.Commodities
WHERE CommodityID = @CommodityID

SELECT [Wo#],
OrdQty,
[Part#],
ShipDate,
Customer,
MaterialType,
MaterialForm
FROM (
SELECT WONumber AS [Wo#],
FB_OPNWO_WAQTYO AS OrdQty,
FB_OPNWO_PART AS [Part#],
CAST(CONVERT(CHAR(10), FB_OPNWO_SHIPDT, 112) AS DATETIME) AS ShipDate,
FB_OPNWO_CUNAME AS Customer,
MaterialType,
MaterialForm,
FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersBackLog
WHERE FB_OPNWO_SHIPDT >= @StartShipDate
AND FB_OPNWO_SHIPDT <= @EndShipDate
AND FB_OPNWO_SHIPBR = @BranchNum
AND MaterialType = @MaterialType
AND MaterialForm = @MaterialForm

UNION

SELECT WONumber AS [Wo#],
FB_OPNWO_WAQTYO AS OrdQty,
FB_OPNWO_PART AS [Part#],
CAST(CONVERT(CHAR(10), FB_OPNWO_SHIPDT, 112) AS DATETIME) AS ShipDate,
FB_OPNWO_CUNAME AS Customer,
MaterialType,
MaterialForm,
FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersUnscheduled
WHERE FB_OPNWO_SHIPDT >= @StartShipDate
AND FB_OPNWO_SHIPDT <= @EndShipDate
AND FB_OPNWO_SHIPBR = @BranchNum
AND MaterialType = @MaterialType
AND MaterialForm = @MaterialForm

UNION

SELECT WONumber AS [Wo#],
FB_OPNWO_WAQTYO AS OrdQty,
FB_OPNWO_PART AS [Part#],
CAST(CONVERT(CHAR(10), FB_OPNWO_SHIPDT, 112) AS DATETIME) AS ShipDate,
FB_OPNWO_CUNAME AS Customer,
MaterialType,
MaterialForm,
FB_OPNWO_SHIPBR AS Branch
FROM dbo.VWorkOrdersScheduled
WHERE FB_OPNWO_SHIPDT >= @StartShipDate
AND FB_OPNWO_SHIPDT <= @EndShipDate
AND FB_OPNWO_SHIPBR = @BranchNum
AND MaterialType = @MaterialType
AND MaterialForm = @MaterialForm
) AS d
ORDER BY ShipDate[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-07 : 15:24:50
Seems to me you have a bug right here: Parameters.Append .CreateParameter("CommodityID", adInteger, adParamInput, , asOpenArgs(1))

Shouldn't that say @CommodityID?

Your CREATE TABLE statement in the stored procedure is not correct. It seems to be cut off. Also you should be using a composite index on the temporary table and not four individual indexes. I'd recommend ShipDate, Branch, MaterialType, MaterialForm all in one index and in that order.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 15:29:08
Better to get a smaller number of records from each view directly.
See suggestion posted 04/07/2009 : 15:24:13


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-04-07 : 15:37:19
Remove the line

set RS = nothing

on the front end.

There is no need to do that, you are in essance Nulling the recordset returning nothing from the function since it is linked to that object.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 15:38:05
Good catch Tara though that will not make a difference I have changed it to @CommoDityID. if you only knew the setup of this company I am doing this for. That is just the point I want to avoid using those views by creating temp table for the where clause, is there no other way? when I do that the time it takes it 1/2 the amount.

Merci

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 15:40:15
How much time do mu suggestion posted 04/07/2009 : 15:24:13 take?


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 15:41:01
there is a need to do set rs = nothing (got to clean up after yourself ;) ) since i am doing
Set WorkOrdersRecordSet = rs just before it :)

Thank y'all for your help


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 15:47:28
Peso,

It takes 23 seconds for 260 rows.

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 15:51:04
And your original procedure?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 15:53:29
about the same time :( shaev off a couple of seconds

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 15:55:10
To further help you in this matter, we need to see the three view definitions.
Maybe the three views derive from same base table?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-04-07 : 16:27:13
Yes Peso part of those views derive from same base tables just different slices based on some conditions. And as I mentioned earlier I have no control over the base tables which are just bulk populated every 15 minutes. All fields are char including fields that should be bit, datetime, float, money everything. One big flat table from which I am carving out what I need. This creates overhead on the performance. But maybe I will try to change the views and see what I can come up with, since calling the same base tables 3 times would definitely slow things down. Let me fix that up!

Thanks!

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-07 : 16:32:58
If all three views derive from same base table, just do one select with OR in the WHERE clause.




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -