0

我对这个程序有疑问。它使用了几个临时表,它会删除并重新创建它们。这导致了大量的崩溃,我只能假设冲突正在发生。

ALTER PROCEDURE [dbo].[AdditionalVisits_SelectByOrigID] 
(
@orig_job_ID int
)
as

if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME =   'additionalVisitsPodsCnt')
    drop table additionalVisitsPodsCnt;

SELECT     CONVERT(bit, COUNT(PODS.podID)) AS cnt, AdditionalVisits.this_job_id, AdditionalVisits.orig_job_id
INTO            additionalVisitsPodsCnt
FROM         PODS RIGHT OUTER JOIN
                  AdditionalVisits ON PODS.jobID = AdditionalVisits.this_job_id
GROUP BY AdditionalVisits.this_job_id, AdditionalVisits.orig_job_id
HAVING      (AdditionalVisits.orig_job_id = @orig_job_ID)

if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'additionalVisitsPhotosCnt')
drop table additionalVisitsPhotosCnt;

SELECT     CONVERT(bit, COUNT(Photos.photoID)) AS cnt, AdditionalVisits.this_job_id, AdditionalVisits.orig_job_id
INTO            additionalVisitsPhotosCnt
FROM         Photos RIGHT OUTER JOIN
                  AdditionalVisits ON Photos.jobID = AdditionalVisits.this_job_id
GROUP BY AdditionalVisits.this_job_id, AdditionalVisits.orig_job_id
HAVING      (AdditionalVisits.orig_job_id = @orig_job_ID)

SELECT     AdditionalVisits.id, AdditionalVisits.this_job_ID, AdditionalVisits.orig_job_ID, AdditionalVisits.reason, AdditionalVisits.date, ThisJob.Job_Reference_No, 
                  ThisJob.Job_POD_Filename, ThisJob.Job_Photo_Filename,    ThisJob.Job_Signed_For_Name, ThisJob.Job_Value, ThisJob.Job_Advance_Payment, 
                  ThisJob.Job_Eoj_Payment, ThisJob.Job_Status, ThisJob.Job_Date_Added, ThisJob.Job_Start_Date, additionalVisitsPhotosCnt.cnt AS Job_Photo_Supplied, 
                  additionalVisitsPodsCnt.cnt AS Job_POD_Supplied
FROM         AdditionalVisits INNER JOIN
                  Jobs AS ThisJob ON AdditionalVisits.this_job_ID = ThisJob.Job_ID    INNER JOIN
                  additionalVisitsPodsCnt ON AdditionalVisits.this_job_ID = additionalVisitsPodsCnt.this_job_id INNER JOIN
                  additionalVisitsPhotosCnt ON AdditionalVisits.this_job_ID = additionalVisitsPhotosCnt.this_job_id
WHERE     (AdditionalVisits.orig_job_ID = @orig_job_ID) AND (ThisJob.Job_Status <> 7)

我得到的错误如下:

Cannot drop the table 'additionalVisitsPodsCnt', because it does not exist or you do not have permission. There is already an object named 'additionalVisitsPodsCnt' in the database.

或者

 Invalid object name 'additionalVisitsPhotosCnt'.

这些错误是间歇性的。有时它只是工作。

有没有更好的方法来做到这一点?

4

3 回答 3

0

考虑改用表变量。

这个 StackOverflow 主题比我解释得更好...... ;)

于 2013-10-21T14:29:43.907 回答
0

这是我用于#temp 表的正常模式。(注意“#”前缀)。

IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
        drop table #TableOne
end


CREATE TABLE #TableOne
( 
SurrogateKey int , 
NameOf varchar(12)
)


/*
Do something with temp table
*/




IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
        drop table #TableOne
end
于 2013-10-21T14:12:42.687 回答
0

当然,如果此表仅在这一过程中使用,请使用“真实”本地临时表(在 中创建tempdb),通过在表名前加上#:

SELECT  CONVERT(bit, COUNT(PODS.podID)) AS cnt, 
        AdditionalVisits.this_job_id, AdditionalVisits.orig_job_id
INTO            #additionalVisitsPodsCnt
FROM         PODS ....

这样,即使其他人同时调用此过程,它也与所有其他会话隔离。

于 2013-10-21T13:28:30.957 回答