以下是我的示例存储过程。当我通过传递DATE执行存储过程时,它显示以下错误。
无效的对象名称'TEMPP'
- 存储过程
ALTER PROCEDURE [dbo].[mystoredprocedure]
@abc DATETIME //coming fron front-end
AS
BEGIN
SET NOCOUNT ON;
WITH TEMPP AS
(
SELECT DISTINCT(ID), NAME
FROM EMPLOYEE WHERE EMPDATE > @abc
)
select DP.NAME INTO xy from DEPARTMENT DP,TEMPP TE
where DP.ID=TE.ID ;
SELECT * FROM xy;
DROP xy;
END
哪里出错?
此致
JN
答案 0 :(得分:0)
来自网上书籍: 递归成员的CTE_query_definition中不允许使用以下项:
SELECT DISTINCT [...]
你可以试试这个:
ALTER PROCEDURE [dbo].[mystoredprocedure]
@abc DATETIME //coming fron front-end
AS
BEGIN
SET NOCOUNT ON;
select DP.NAME from DEPARTMENT DP
inner join
(
SELECT DISTINCT(ID), NAME
FROM EMPLOYEE WHERE EMPDATE > @abc
)TE ON DP.ID=TE.ID;
END
答案 1 :(得分:0)
如果您的ID是您的主键,则它已经是DISTINCT
,这意味着如果@ibram answer是导致错误的原因,您可以将其删除。我认为xy
意味着是一个临时表,并且看不到任何用途。
ALTER PROCEDURE [dbo].[mystoredprocedure]
@abc DATETIME //coming fron front-end
AS
BEGIN
SET NOCOUNT ON;
WITH TEMPP AS
(
SELECT ID, NAME
FROM EMPLOYEE WHERE EMPDATE > @abc
)
SELECT DP.NAME
FROM DEPARTMENT DP
JOIN TEMPP TE
ON DP.ID=TE.ID ;
END
答案 2 :(得分:0)
如果您稍后尝试在过程中引用CTE,通常会看到此错误。它仅对其定义后的声明有效。
ALTER PROCEDURE [dbo].[mystoredprocedure] @abc DATETIME
AS
BEGIN
SET NOCOUNT ON;
WITH TEMPP
AS (SELECT DISTINCT ( name ),
number
from master..spt_values)
select number
INTO xy
from TEMPP;
SELECT *
FROM xy;
DROP table xy;
SELECT * FROM TEMPP /*Not Allowed: Throws this error*/
END