简体   繁体   中英

SP executing error

I am writing below SP.But when i try to run this query i am getting this error:

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

So this getting in two else loops. also

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

    declare @status varchar(500)

    declare @substatus char(2)

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

    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 

Also, the monstrocity of a query you have could be reduced to this:

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

The SQL Parser is choking because you have used the same temp table name in different parts of the IF statement. The IF does not have scope like other programming languages.

If you do not need to reference the temp table outside of each of the IF blocks you can get around the problem by using a different table name in each part.

Have a look at my answer to a similar question .

You syntax is

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

When using this syntax, Sql Server attempts to create #TempTable on the fly based on your column list ( source ).

To get around this, either Drop #TempTable at the beginning of the stored procedure (if you do not need its data beyond the scope of the SP), or make it a permanent table.

Explicitly create the temp table at the beginning of the proc.

CREATE TABLE #myCourses1 (
    ...
)

Then write your SELECT statements as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM