[英]SP executing error
I am writing below SP.But when i try to run this query i am getting this error: 我写在SP下面。但是当我尝试运行此查询时,我收到此错误:
There is already an object named '#myCourses1' in the database.
数据库中已经有一个名为“#myCourses1”的对象。
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. SQL解析器因为你在
IF
语句的不同部分使用了相同的临时表名而令人窒息。 The IF
does not have scope like other programming languages. IF
没有其他编程语言的范围。
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. 如果您不需要在每个
IF
块之外引用临时表,则可以通过在每个部分中使用不同的表名来解决问题。
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 ). 使用此语法时,Sql Server会尝试根据列列表( 源 )动态创建#TempTable。
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. 要解决这个问题,可以在存储过程开始时
Drop #TempTable
(如果您不需要超出SP范围的数据),或者将其设置为永久表。
Explicitly create the temp table at the beginning of the proc. 在proc的开头显式创建临时表。
CREATE TABLE #myCourses1 (
...
)
Then write your SELECT
statements as: 然后将
SELECT
语句编写为:
INSERT INTO #myCourses1
select a.*, b.CategoryText, Cast('' as Varchar(10)) as SectionsViewed, PurchasedDate as dateadded
from dbo.Tbl_CourseInformations
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.