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

幫助處理類似 sp_msforeachdb 的查詢

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

問題描述

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

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.

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

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?

我們?nèi)栽谑褂?SQL Server 2000,但應(yīng)該會在幾個月后遷移到 2005.

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

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

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.

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

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 變量設(shè)置為您的查詢文本.很容易變成存儲過程.它是動態(tài) sql,但同樣:它僅用于開發(fā)(著名的遺言;)).缺點是您仍然需要轉(zhuǎn)義查詢中使用的單引號,并且大部分時間您最終會在選擇列表中放置一個額外的 ''?'' 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.

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

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).

推薦答案

只需將要執(zhí)行的語句包裝在 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
"

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 久久人体视频 | 久久综合一区二区三区 | 国产一区二区免费 | 免费观看一级特黄欧美大片 | 蜜臀av日日欢夜夜爽一区 | 国产高清自拍视频在线观看 | 人人九九精 | 欧美一区二区在线免费观看 | 影音先锋男 | 午夜性色a√在线视频观看9 | 亚洲精品久久久久久久久久久 | 亚洲毛片一区二区 | 欧美激情在线精品一区二区三区 | 久久久久国产一区二区三区 | 国产精品区二区三区日本 | 日日操夜夜操天天操 | 欧美精品久久久 | 国产精品久久久久不卡 | 欧美日韩大片 | 日韩电影免费在线观看中文字幕 | 国产成人91视频 | 涩在线| 日日夜夜视频 | 91久久精品视频 | 日本精品一区二区三区四区 | 久草欧美视频 | 最新国产视频 | 不卡在线视频 | 二区中文字幕 | 婷婷久久综合 | 日韩aⅴ视频| 精品一二三 | 国产精品不卡视频 | 九九在线| 国产成人在线一区二区 | 91天堂 | 天天干天天操天天射 | 国产精品欧美一区二区三区不卡 | 亚洲高清在线视频 | 免费的日批视频 | 免费在线观看av网址 |