存储过程作为视图或调用函数

时间:2012-08-10 20:17:58

标签: tsql function stored-procedures view

我可以通过存储过程来引发系统视图吗?例如,我有它:

CREATE PROCEDURE DBO.status_serwer
AS
BEGIN
EXECUTE msdb.dbo.sysmail_help_status_sp
END

Sysmail_help_status程序返回服务器的状态:'stopped'或'started'现在,我可以将start_serwer程序作为视图或函数调用吗?有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

好吧,如果你查看那个存储过程,你可以抓住它运行的查询并稍微修改它以便你可以在其他地方使用它,就像这样;

SELECT 
    CASE (SELECT COUNT(*) FROM msdb.sys.service_queues WHERE name = N'ExternalMailQueue' AND is_receive_enabled = 1) 
        WHEN 0 THEN 'STOPPED' 
        ELSE 'STARTED' 
        END AS 'sysmail_status'

以下是原件供参考:

ALTER PROCEDURE [dbo].[sysmail_help_status_sp]
  WITH EXECUTE AS 'dbo'
AS
BEGIN
    IF NOT EXISTS (SELECT * FROM sys.service_queues WHERE name = N'ExternalMailQueue' AND is_receive_enabled = 1)
       SELECT 'STOPPED' AS Status
    ELSE
       SELECT 'STARTED' AS Status
END