----新建视图 USE DBName go IF EXISTS(SELECT * FROM sysobjects WHERE name=view_Name) DROP VIEW view_Name CREATE VIEW view_Name AS <SELECT语句>
----视图运行存储过程
SELECT *
FROM OPENROWSET('SQLOLEDB', 'SERVER=192.168.101.112;uid=sa;pwd=1;database=AIS202205',
'SET FMTONLY OFF; SET NOCOUNT ON; exec VW_Test ')
注如果提示错误:
1)SQL Server 阻止了对组件“Ad Hoc Distributed Queries”的 STATEMENT“OpenRowset/OpenDatasource”的访问
执行以下语句:开启Ad Hoc Distributed Queries组件
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
2)过程 sys.sp_describe_first_result_set,无法确定元数据,因为语句中存在临时表
因为2012版本后的sqlserver数据库,视图运行存储过程不允许存在临时表,将所有的临时表改为实体表,表中加id 字段 :例
ALTER PROCEDURE Test
AS
BEGIN
DECLARE @UUID VARCHAR(800)
set @UUID= newid()
......................
...........存过程逻辑内容....................
.....................
-- 写在最后
select * from temp where id=@UUID
delete t_OA_YuQi_CSR1 where id=@UUID
END