循环遍历数据库列表以删除并重新创建视图
我不确定是否可以使用 TSQL 中的循环来完成此操作。
我有一个数据库列表,我想循环遍历每个数据库,并根据需要重新创建视图。
我已经有一个脚本可以删除并重新创建视图。然而,目前在我的脚本顶部有一堆 use 语句,我只需沿着列表取消注释并重新运行脚本即可。不过我想将其自动化,这样会更快。我过去做过类似的事情,循环遍历数据库名称字符串并使用它来执行 use 语句。除了创建视图时以外,它都有效,因为视图必须由 go 语句分隔。这是我的代码的代码片段,用于解析数据库,任何有助于处理视图的帮助都会很棒。
DECLARE @DBs AS VARCHAR(MAX);
DECLARE @OneDB AS VARCHAR(255);
DECLARE @CmdToExec AS VARCHAR(MAX);
SET @DBs = 'db1,db2,db3,db4,db5,db6,db7,db8,db9,db10,db11,db12,db13,db14,db15,db16'
DECLARE DB_Cursor CURSOR FOR
SELECT SUBSTRING( ',' + @DBs + ',', n + 1,
CHARINDEX( ',', ',' + @DBs + ',', n + 1 ) - n - 1 ) AS "dbInfo"
FROM CommunityPAL.dbo.Numbers
WHERE SUBSTRING( ',' + @DBs + ',', n, 1 ) = ','
AND n < LEN( ',' + @DBs + ',' );
OPEN DB_Cursor;
FETCH NEXT FROM DB_Cursor INTO @OneDB;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CmdToExec = 'USE ' + @OneDB;
EXEC @CmdToExec;
--statements to execute
FETCH NEXT FROM DB_Cursor INTO @OneDB;
END
CLOSE DB_Cursor
DEALLOCATE DB_Cursor
I am unsure if it is possible to do this with a loop in TSQL.
I have a list of Databases I would like to loop through and for each database drop and recreate the view as necessary.
I have a script already to drop and recreate the view. However, presently at the top of my script I have a bunch of use statements and i just go down the list uncommenting and reruning the script. However I would like to automate it so it would be much faster. I have done a similar thing in the past looping though a string of database names and using it to execute a use statement. Which works except when creating views because the view has to be seperated by go statements. Here is a code snippet of my code to parse databases any help to make work with views would be great.
DECLARE @DBs AS VARCHAR(MAX);
DECLARE @OneDB AS VARCHAR(255);
DECLARE @CmdToExec AS VARCHAR(MAX);
SET @DBs = 'db1,db2,db3,db4,db5,db6,db7,db8,db9,db10,db11,db12,db13,db14,db15,db16'
DECLARE DB_Cursor CURSOR FOR
SELECT SUBSTRING( ',' + @DBs + ',', n + 1,
CHARINDEX( ',', ',' + @DBs + ',', n + 1 ) - n - 1 ) AS "dbInfo"
FROM CommunityPAL.dbo.Numbers
WHERE SUBSTRING( ',' + @DBs + ',', n, 1 ) = ','
AND n < LEN( ',' + @DBs + ',' );
OPEN DB_Cursor;
FETCH NEXT FROM DB_Cursor INTO @OneDB;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CmdToExec = 'USE ' + @OneDB;
EXEC @CmdToExec;
--statements to execute
FETCH NEXT FROM DB_Cursor INTO @OneDB;
END
CLOSE DB_Cursor
DEALLOCATE DB_Cursor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定你不能只在 T-SQL 中做你想做的事。
CREATE VIEW
(和ALTER VIEW
)不使用三部分命名约定 (database.schema.object)。它必须是批处理中的唯一语句,因此不能与动态 SQL 调用(EXEC
或sp_executeSQL
)中的USE
命令组合使用),并且USE
的效果只会持续到该批次的持续时间(即直到任何嵌入的GO
)。然而,我已经从“外部”SQL Server 完成了类似的工作,更新某种“类型”的所有数据库中的特定对象(每个客户端/客户都有一个)。它变得复杂。简要概述:
[RE]CREATE VIEW
脚本I am pretty sure you can't do what you want exclusively within T-SQL.
CREATE VIEW
(andALTER VIEW
) do not use the three-part naming convention (database.schema.object). It has to be the only statement in a batch, so it cannot be combined with aUSE
command within a dynamic SQL call (EXEC
orsp_executeSQL
), and the effects of thatUSE
will only last until the duration of that batch (i.e. until any embeddedGO
).However, I have done similar work from “outside” SQL server, updating specific objects in all databases of a certain "type" (where we have one per client/customer). It gets complex. A brief outline:
[RE]CREATE VIEW
script请检查
sp_msforeachdb
过程以循环所有数据库:利用未记录的 SQL Server 迭代过程
Please check the
sp_msforeachdb
procedure to loop for the all databases:Take advantage of undocumented SQL Server iteration procedures