2011-05-16 101 views
0

我寫下面SP.But當我嘗試運行此查詢我收到此錯誤:SP執行錯誤

There is already an object named '#myCourses1' in the database.

兩個其他迴路所以這讓。也

create proc [dbo].[GetOrdersByUserIDwithSubscription]  
( 
    @UserID int  
)  
as  
begin  

    declare @status varchar(500) 

    declare @substatus char(2) 

    select @substatus=Subscribe_status from tbl_user where [email protected] 

    print @substatus 

    if @substatus='N' 

    BEGIN  
     select a.*, b.CategoryText, Cast('' as Varchar(10)) as SectionsViewed, PurchasedDate as dateadded into #myCourses1 from dbo.Tbl_CourseInformations a JOIN Tbl_Categories b ON a.AssignCategory = b.CategoryID  
     Join Tbl_Orders c ON c.UserID = @UserID and c.CourseID = a.CourseID and c.courseprice<>'subscriber' 
     Order By CategoryText, CourseTitle   
    END 

    else if @substatus='' 

    BEGIN  
     select a.*, b.CategoryText, Cast('' as Varchar(10)) as SectionsViewed, PurchasedDate as dateadded into #myCourses1 from dbo.Tbl_CourseInformations a JOIN Tbl_Categories b ON a.AssignCategory = b.CategoryID  
     Join Tbl_Orders c ON c.UserID = @UserID and c.CourseID = a.CourseID and c.courseprice<>'subscriber' 
     Order By CategoryText, CourseTitle  
    END 

    else if @substatus='Y'  
    BEGIN  
     select a.*, b.CategoryText, Cast('' as Varchar(10)) as SectionsViewed, PurchasedDate as dateadded into #myCourses1 from dbo.Tbl_CourseInformations a JOIN Tbl_Categories b ON a.AssignCategory = b.CategoryID  
     Join Tbl_Orders c ON c.UserID = @UserID and c.CourseID = a.CourseID  
     Order By CategoryText, CourseTitle 
    END 
+0

涉及:http://stackoverflow.com/questions/6018284/sql-query-not-getting-values – 2011-05-16 14:22:12

+0

如果你不這樣做需要'#myCourses1'嘗試用一個drop table來結束SP:'drop table#myCourses1'。如果在SP完成運行後確實需要這張表,請將'Select .... into myCourses1'改爲'Insert into #myCourses ...'問題是,我認爲這個SP只能運行一次,之後,您必須刪除#myCourses1或使用「插入到#myCourses1」 – 2011-05-16 14:25:16

回答

0

在proc的開頭顯式創建臨時表。

CREATE TABLE #myCourses1 (
    ... 
) 

然後再編寫SELECT語句:

INSERT INTO #myCourses1 
    select a.*, b.CategoryText, Cast('' as Varchar(10)) as SectionsViewed, PurchasedDate as dateadded 
     from dbo.Tbl_CourseInformations 
     ... 
1

此外,你有一個查詢的monstrocity可以減少到這樣的:

create proc [dbo].[GetOrdersByUserIDwithSubscription]( 
    @UserID int  
)  
as  
begin 
    declare @substatus char(2) 

    select @substatus = Subscribe_status 
    from tbl_user 
    where userid = @userid 

    select a.*, b.CategoryText, 
     Cast("" as Varchar(10)) as SectionsViewed, 
     PurchasedDate as dateadded 
    from dbo.Tbl_CourseInformations a 
     join Tbl_Categories b ON a.AssignCategory = b.CategoryID 
     join Tbl_Orders c ON c.UserID = @UserID 
      and c.CourseID = a.CourseID 
      and (@substatus = 'N' or c.courseprice <> 'subscriber') 
    order by CategoryText, CourseTitle   

END 
+0

知道誰下降了,並且出於任何原因總是很好的。 – 2011-05-19 09:49:34

+0

對不起,這是我。我不知道如何發生,我沒有理由。我收回了我的downvote。 – heximal 2016-06-22 08:24:05

+0

哈哈! 5年後.. xD – 2016-06-23 13:27:50

1

SQL解釋是窒息,因爲你已經在IF的不同部分使用了相同的臨時表名聲明。 IF沒有其他編程語言的範圍。

如果您不需要在每個IF塊之外引用臨時表,則可以通過在每個零件中使用不同的表名來解決該問題。

看一看my answer to a similar question

0

你的語法是

SELECT [Column-List] INTO #TempTable FROM [Rest-of-Query] 

當使用這種語法,SQL Server試圖在基於您的列清單(source)動態創建#TempTable。

爲了解決這個問題,在存儲過程開始時(如果不需要超出SP範圍的數據),或者使其成爲永久表,則可以使用Drop #TempTable