我的存储过程没有参数。如下运行:-
EXEC sp_Email_spd_CasualNotification
返回表格:-
DeptChangeID| oldDeptID |DeptID | JobID |EmpID
33419 | NULL | 679 | 3742 |16575
42392 | NULL | 783 | 4742 |100000378
我想在View中查看此表。我只是找不到办法。 我的意思是
CREATE View [dbo].[vw_EmploymentLatest] as
Select * from (EXEC sp_Email_spd_CasualNotification)
这不可能。
我的存储过程简化如下:-
declare @empTable table (employeeID int)
declare @selectedDeptChangeIDTable table (deptChangeID int)
declare @rowCount int
declare @rowNum int
declare @selectedDeptChangeID int
declare @empID int
Insert into @empTable (employeeID)
SELECT DISTINCT
E.Employee_ID
FROM Employee E
'
'
WHILE exists (select * from @empTable)
BEGIN
SELECT @empID = (select top 1 employeeID from @empTable order by employeeID asc)
'
'
WHILE @rowNum <= @rowCount
BEGIN
declare @p1 int
declare @p2 int
'
'
'
'
DELETE @empTable WHERE employeeID = @empID
END
Select * from Employment Where DeptChangeID in (Select deptChangeID from @selectedDeptChangeIDTable)
答案 0 :(得分:2)
您的过程应在您要查询的表上包含一个SELECT
语句。
示例:
SELECT * FROM myTable
然后调用过程:
EXEC sp_Email_spd_CasualNotification
如果您的过程未对表进行任何更改,为什么不考虑使用FUNCTION
?
注意:sp_
前缀是为系统存储过程保留的,因此我将更改其名称。
答案 1 :(得分:1)
存储过程不打算在视图(或函数)中使用。您必须在视图或函数上编写查询。
让我们假设这个存储过程:
CREATE PROCEDURE dbo.Test
AS
SELECT * FROM sys.tables
GO
EXEC dbo.Test
可以将其转换为像这样的表值函数:
CREATE FUNCTION dbo.Test
RETURNS TABLE AS
RETURN (
SELECT * FROM sys.tables
)
GO
SELECT * FROM dbo.Test()
现在,如果需要,可以在视图中使用它。
这是更简单的语法(内联函数),但是对于任何复杂的存储过程,您都需要将其转换为多语句表值函数。
CREATE FUNCTION dbo.Test
RETURNS @Test TABLE (object_id int, name varchar(100))
AS
BEGIN
INSERT INTO @Test (object_id, name)
SELECT object_id, name FROM sys.tables
END
GO
SELECT * FROM dbo.Test()
看看您提供的代码,您的功能将如下所示:
CREATE FUNCTION dbo.Email_spd_CasualNotification
RETURNS @CasualNotification TABLE (DeptChangeID int, oldDeptID int, DeptID int, JobID int, EmpID int)
AS
BEGIN
declare @empTable table (employeeID int)
declare @selectedDeptChangeIDTable table (deptChangeID int)
declare @rowCount int
declare @rowNum int
declare @selectedDeptChangeID int
declare @empID int
Insert into @empTable (employeeID)
SELECT DISTINCT
E.Employee_ID
FROM Employee E
'
'
WHILE exists (select * from @empTable)
BEGIN
SELECT @empID = (select top 1 employeeID from @empTable order by employeeID asc)
'
'
WHILE @rowNum <= @rowCount
BEGIN
declare @p1 int
declare @p2 int
'
'
'
'
DELETE @empTable WHERE employeeID = @empID
END
END
insert into @CasualNotification(DeptChangeID, oldDeptID, DeptID, JobID, EmpID)
Select DeptChangeID, oldDeptID, DeptID, JobID, EmpID
from Employment
Where DeptChangeID in (Select deptChangeID from @selectedDeptChangeIDTable)
END
答案 2 :(得分:-1)
.values
upd:在视图中使用(为什么?可以通过表/内联函数使用?)
添加链接服务器(本地)
CREATE PROCEDURE dbo.Test
AS
SELECT * FROM sys.tables
GO
EXEC dbo.Test
然后尝试
EXECUTE sp_addlinkedserver @server = N'LinkedServer', @srvproduct = N'sqlserver', @provider = N'SQLNCLI', @datasrc = N'LinkedServer.domain';