我寫下面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
涉及:http://stackoverflow.com/questions/6018284/sql-query-not-getting-values – 2011-05-16 14:22:12
如果你不這樣做需要'#myCourses1'嘗試用一個drop table來結束SP:'drop table#myCourses1'。如果在SP完成運行後確實需要這張表,請將'Select .... into myCourses1'改爲'Insert into #myCourses ...'問題是,我認爲這個SP只能運行一次,之後,您必須刪除#myCourses1或使用「插入到#myCourses1」 – 2011-05-16 14:25:16