跨DB查询

时间:2017-12-07 23:40:34

标签: sql sql-server tsql

我试图跨多个数据库运行查询。每个数据库都是不同的客户,但我在所有数据库中查询相同的表。我想遍历所有数据库(大约100个)并将所有结果放在一个表中。我尝试了几种不同的方法,但我似乎无法让它工作,似乎它可能是一种语法问题。以下是我的代码:

IF OBJECT_ID('KW.dbo.Result') IS NOT NULL DROP TABLE KW.dbo.Result;

SELECT name as DBName, ROW_NUMBER() OVER(PARTITION BY 1 ORDER BY name) AS RNo 
INTO #DBName
FROM sys.databases
WHERE name LIKE 'Customer%'
ORDER BY name;

SELECT * FROM #DBName

DECLARE @sql varchar;

SET @sql = N'SELECT bh.ID,
b.LINE AS Line#,
c.State,
bh.ZIP,
REPLACE(p.Phone, '-', '') AS Phone,
cc.COMPANY_NAME,
b.Amount
INTO KW.dbo.Result
FROM dbo.Customer c
     LEFT JOIN dbo.Change ch ON ch.CIDNo = c.CIDNo
     LEFT JOIN dbo.Provider p ON p.PIDNo = ch.PIDNo
     LEFT JOIN dbo.BossHead bh ON bh.CHIDNo = ch.CHIDNo
     LEFT JOIN dbo.Bills b ON b.BIDNo = bh.BIDNo
     LEFT JOIN dbo.PartnerTerms pt ON pt.BIdNO = b.BIdNo
     LEFT JOIN dbo.CompanyCode cc ON cc.CompanyCode = pt.CompanyCode
WHERE REPLACE(p.Phone, '-', '') IN
(
    SELECT *
    FROM KW.dbo.PhoneNumbers
)
      AND bh.CreateDate BETWEEN "09-01-2016" AND "10-01-2017"
      AND pt.CompanyCode IS NOT NULL
ORDER BY 1';

DECLARE @DB varchar;

DECLARE @i int;

BEGIN TRANSACTION;

SET @i = 1;

    WHILE @i <= (SELECT MAX(RNo) FROM #DBName)

        BEGIN

           SET @DB = (SELECT DBName FROM #DBName WHERE RNo = @i)

           USE @DB;

           EXECUTE sp_executesql @sql

           SET @i = @i + 1

        END

COMMIT TRANSACTION;

GO

错误消息:&#34;消息102,级别15,状态1,行55 &#39; @ DB&#39;。&#34;

附近的语法不正确

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

您无法使用变量来切换表格。您可以做的是创建一个动态查询,该查询使用完全限定的数据库和表名来同时遍历所有数据库。

创建一个创建查询的查询:

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:local="clr-namespace:IrisApp.Core.Views"
             prism:ViewModelLocator.AutowireViewModel="True"
             Title="Binding {Title}"
             x:Class="IrisApp.Core.Views.RegistrationPage" >
    <ContentPage.Content>
        <StackLayout VerticalOptions="StartAndExpand">
            <Label Text="User Name"/>
            <Entry Text="{Binding UserName}" Placeholder="User Name" />

            <Label Text="Password"/>
            <Entry Text="{Binding Password}" Placeholder="Password" />

            <Label Text="Email"/>
            <Entry Text="{Binding Email}" Placeholder="Email Address" />

            <Picker Title="Select your favorite shop">
                ItemsSource="{Binding AvailableShops}"
                SelectedItem="{Binding FavoriteShop}"
                IsEnabled="{Binding UseNearestShop, Converter={StaticResource BooleanNegationConverter}}"
            </Picker>

            <StackLayout Orientation="Horizontal">
                <Label Text="Always use nearest shop"/>
                <Switch IsToggled="{Binding UseNearestShop}"/>
            </StackLayout>

            <StackLayout Orientation="Horizontal"  HorizontalOptions="FillAndExpand">
                <StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand">
                    <Button Text="Register" Command="{Binding FinalizeRegistrationCommand}" />
                </StackLayout>
                <StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand">
                    <Button Text="Cancel" Command="{Binding CancelRegistrationCommand}" />
                </StackLayout>
            </StackLayout>`enter code here`
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

使用它直到查询执行您想要的操作,然后删除前1并对所有数据库运行整个查询。