久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

幫助處理類似 sp_msforeachdb 的查詢

Help with sp_msforeachdb -like queries(幫助處理類似 sp_msforeachdb 的查詢)
本文介紹了幫助處理類似 sp_msforeachdb 的查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我所在的地方有一個在大型機系統上運行的軟件包.大型機每晚都會轉儲到 sql server,這樣我們的每個客戶端在服務器中都有自己的數據庫.服務器實例中還有一些其他數據庫,以及一些沒有數據的舊客戶端數據庫.

Where I'm at we have a software package running on a mainframe system. The mainframe makes a nightly dump into sql server, such that each of our clients has it's own database in the server. There are a few other databases in the server instance as well, plus some older client dbs with no data.

我們經常需要跨所有客戶運行報告或檢查數據.我希望能夠使用 sp_msforeachdb 或類似的東西運行查詢,但我不確定如何從列表中過濾不需要的數據庫.關于這如何工作的任何想法?

We often need to run reports or check data across all clients. I would like to be able to run queries using sp_msforeachdb or something similar, but I'm not sure how I can go about filtering unwanted dbs from the list. Any thoughts on how this could work?

我們仍在使用 SQL Server 2000,但應該會在幾個月后遷移到 2005.

We're still on SQL Server 2000, but should be moving to 2005 in a few months.

更新:
我覺得我問這個問題做得不好,所以我要澄清我的目標,然后發布我最終使用的解決方案.

Update:
I think I did a poor job asking this question, so I'm gonna clarify my goals and then post the solution I ended up using.

我想在這里完成的是讓程序員可以輕松地處理要在其程序中使用的查詢,使用一個客戶端數據庫編寫查詢,然后幾乎立即運行(測試)在一個客戶端的數據庫上設計和構建的代碼在所有 50 個左右的客戶端數據庫上,幾乎沒有修改.

What I want to accomplish here is to make it easy for programmers working on queries for use in their programs to write the query using one client database, and then pretty much instantly run (test) code designed and built on one client's db on all 50 or so client dbs, with little to no modification.

考慮到這一點,這是我目前位于 Management Studio 中的代碼(部分混淆):

With that in mind, here's my code as it currently sits in Management Studio (partially obfuscated):

use [master]
declare @sql varchar(3900) 

set @sql = 'complicated sql command added here'

-----------------------------------
declare @cmd1 varchar(100)
declare @cmd2 varchar(4000)
declare @cmd3 varchar(100)
set @cmd1 = 'if ''?'' like ''commonprefix_%'' raiserror (''Starting ?'', 0, 1) with nowait'
set @cmd3 = 'if ''?'' like ''commonprefix_%'' print ''Finished ?'''
set @cmd2 = 
    replace('if ''?'' like ''commonprefix_%'' 
    begin 
        use [?]
        {0} 
    end', '{0}', @sql)

exec sp_msforeachdb @command1 = @cmd1, @command2 = @cmd2, @command3 = @cmd3

這方面的好處是您只需將@sql 變量設置為您的查詢文本.很容易變成存儲過程.它是動態 sql,但同樣:它僅用于開發(著名的遺言;)).缺點是您仍然需要轉義查詢中使用的單引號,并且大部分時間您最終會在選擇列表中放置一個額外的 ''?'' As ClientDB 列,否則它運作良好.

The nice thing about this is all you have to do is set the @sql variable to your query text. Very easy to turn into a stored procedure. It's dynamic sql, but again: it's only used for development (famous last words ;) ). The downside is that you still need to escape single quotes used in the query and much of the time you'll end up putting an extra ''?'' As ClientDB column in the select list, but otherwise it works well enough.

除非我今天得到另一個非常好的主意,否則我想把它變成一個存儲過程,并使用臨時表將所有結果放在一個結果集中作為表值函數的一個版本(僅適用于選擇查詢).

Unless I get another really good idea today I want to turn this into a stored procedure and also put together a version as a table-valued function using a temp table to put all the results in one resultset (for select queries only).

推薦答案

只需將要執行的語句包裝在 IF NOT IN 中:

Just wrap the statement you want to execute in an IF NOT IN:

EXEC    sp_msforeachdb  "
IF      '?'     NOT IN ('DBs','to','exclude')   BEGIN
        EXEC    sp_whatever_you_want_to
END
"

這篇關于幫助處理類似 sp_msforeachdb 的查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品成人国产乱一区 | 久久国产精彩视频 | 毛片区| 久久国产欧美日韩精品 | 91超碰caoporn97人人 | 久久新| 日韩在线中文 | 天天躁日日躁狠狠很躁 | 色av一区二区三区 | 欧美大片一区 | 日本一区视频在线观看 | 日韩精品一区二区三区 | 自拍偷拍第一页 | 一级做a爰片性色毛片 | 韩日一区二区三区 | 美女视频黄的免费 | 亚洲三区在线 | 国产96在线| 中文字幕一区二区三区四区不卡 | 日韩视频一区二区 | 成人美女免费网站视频 | 91精品国产综合久久久久久丝袜 | 一区二区免费视频 | 久久久亚洲一区 | 高清一区二区 | 亚洲精品视频一区二区三区 | 日韩在线不卡视频 | 久久国产视频网 | 成人性视频在线播放 | 高清成人av | 亚洲精品68久久久一区 | 日韩视频在线观看中文字幕 | 中文字幕一区二区三区乱码在线 | 精品久久一区 | 精品久久久久久久久久久久 | 色天天综合 | 在线免费观看成人 | 成人一区二区视频 | 精品成人佐山爱一区二区 | 久久久久久中文字幕 | 91色视频在线观看 |