ALTER PROCEDURE CREATE_P1_VIEW
AS BEGIN
IF exists(SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[View_PF]') and OBJECTPROPERTY(id, N'IsView') = 1)
DROP VIEW dbo.View_PF
DECLARE test_cursor CURSOR SCROLL FOR
SELECT PFDB_No from dbo.PFDBInfoTable
OPEN test_cursor
DECLARE @PFDBNo nchar(11)
DECLARE @SQL VARCHAR(MAX)
FETCH NEXT FROM test_cursor INTO @PFDBNo
WHILE @@FETCH_STATUS=0 BEGIN
set @SQL = 'use CitySupervision;'
SET @SQL='CREATE VIEW [dbo].[View_PF] AS SELECT * FROM '+@PFDBNo+'.dbo.BarNoTable'
FETCH NEXT FROM test_cursor INTO @PFDBNo EXEC (@SQL)
END
CLOSE test_cursor
DEALLOCATE test_cursor
END GO
exec CREATE_P1_VIEW
有3个数据库(管理1个,猪场2个),管理库CitySupervision中有存储的2个猪场库的名称的字段,该字段用变量@PFDBNo存储,以上是在管理库中新建的存储过程,想建立同时包含2个猪场库中的记录,上面的只能建立第一个猪场的视图。。。
请大神帮忙看看什么问题。。