Groups | Blog | Home
all groups > sql server clients > april 2006 >

sql server clients : Temp table.


Rogers
4/12/2006 3:22:39 PM
Is there any one let me know, in the stored procedure I am creating temprary
table and droping table at the end, when single user access it is working
fine but when simultaneous user access of that stored procedure it giving
error like #Temp table is not exists...

any idea ?

Thanks

Andrew J. Kelly
4/12/2006 8:03:34 PM
Can you post the actual code?

--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Rogers
4/13/2006 11:13:13 AM

CREATE PROCEDURE [dbo].[SPSelectOrdersByCategory]
(
@EntityID VARCHAR(10),
@EntityspecificID INT,
@StartDate DATETIME,
@EndDate DATETIME
)

AS

DECLARE @SQLStatement VARCHAR(8000),
@Col VARCHAR(8000)

SET @Col = (SELECT DBO.FNGetAssignCategory(@EntityID,@EntityspecificID))
PRINT @Col

IF @Col <> ''
BEGIN
SET @SQLStatement = 'CREATE TABLE dbo.OrderDate (Dates VARCHAR(20),
EntityID VARCHAR(10),
--- IsNonOrder BIT,
EntityspecificID INT, IsLock BIT,'+@Col+',TotalAmount AS
(dbo.FNGetTotalAmountbyEntity(EntityID,EntityspecificID,Dates)) )'
END
ELSE
BEGIN
SET @SQLStatement = 'CREATE TABLE dbo.OrderDate (
Dates VARCHAR(20),
---IsNonOrder BIT,
EntityID VARCHAR(10),
EntityspecificID INT,
IsLock BIT
,TotalAmount AS
(dbo.FNGetTotalAmountbyEntity(EntityID,EntityspecificID,Dates)) )'
END

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'OrderDate' AND
type = 'U')
BEGIN
DROP TABLE dbo.OrderDate
END
exec (@SQLStatement)

IF (@@ERROR <>0)
BEGIN
DROP TABLE dbo.OrderDate
END
ELSE
BEGIN
WHILE (@StartDate <= @EndDate)
BEGIN
INSERT INTO dbo.OrderDate (
Dates,
EntityID,
EntityspecificID,
--IsNonOrder,
IsLock
)
SELECT convert(varchar(20),@StartDate,110),
@EntityID,
@EntityspecificID,
--dbo.IsNonDate(dbo.FNGetEntityIDbyAlias(@EntityID),@EntityspecificID,@StartDate),
dbo.IsDateLocked(dbo.FNGetEntityIDbyAlias(@EntityID),@EntityspecificID,@StartDate,getdate())

SET @StartDate = dateadd(day,1,@StartDate)
--print @StartDate
END

SELECT *
FROM
OrderDate
WHERE
dbo.IsNonDate(dbo.FNGetEntityIDbyAlias(EntityID),EntityspecificID,Dates) = 0
--where IsNonOrder = 0
AND (DATEPART(DW,Dates)) NOT IN (1,7)

DROP TABLE dbo.OrderDate
END
GO

[quoted text, click to view]

Andrew J. Kelly
4/13/2006 8:20:23 PM
These are not temporary tables, they are real ones. Temporary tables always
begin with #. Why on earth are you dynamically creating real tables just to
insert some rows, select out of it and drop it again? If you need to do
that then create a real temp table (#). You can do that all in a dynamic
sql scope if you wrap all the code into the dynamic sql string. I think your
problem comes from these lines of code:

[quoted text, click to view]

If @@ERROR was <> 0 then the table would never have been created in the
first place so you can't drop it.


But when it all comes down to it I don't see a reason to use tables at all.
Without looking too deep here I believe it can all be done with straight
select statements. If you need to dynamically create a column name then you
may have to use dynamic SQL but you should not need any intermediate tables.



--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

AddThis Social Bookmark Button