困惑从两个DB中选择信息

时间:2013-05-25 13:01:31

标签: sql sql-server sql-server-2008 tsql

我有两个数据库AgencyDB(主数据库)和AgencyBackupDB(用户从AgencyDB删除了信息),我在两个数据库中都有这两个表(但在两个表之间的AgencyBackupDB中没有关系)。这是我的问题: 我想在表SabtHazine中选择来自AgencyBackupDB的所有信息,并从相关表中选择信息Hazine.i想要如果来自Hazine表的HazineID在AgencyBackupDB中存在,则从AgencyDB中选择它。 这是All Tabel With Test Data的脚本

CREATE TABLE [dbo].[Hazine](
    [HazineID] [int] IDENTITY(1,1) NOT NULL,
    [HazineGroupID] [smallint] NOT NULL,
    [Mablagh] [int] NOT NULL,
    [HazineName] [nvarchar](250) NOT NULL,
 CONSTRAINT [PK_Hazine] PRIMARY KEY CLUSTERED 
(
    [HazineID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Hazine] ON
INSERT [dbo].[Hazine] ([HazineID], [HazineGroupID], [Mablagh], [HazineName]) VALUES (1, 1, 20000, N'صابون گل')
INSERT [dbo].[Hazine] ([HazineID], [HazineGroupID], [Mablagh], [HazineName]) VALUES (3, 2, 10000, N'بروس')
SET IDENTITY_INSERT [dbo].[Hazine] OFF
/****** Object:  Table [dbo].[SabtHazine]    Script Date: 04/07/2013 17:34:59 ******/
CREATE TABLE [dbo].[SabtHazine](
    [SabtHazineID] [int] IDENTITY(1,1) NOT NULL,
    [HazineID] [int] NOT NULL,
    [EndUserName] [nvarchar](20) NOT NULL,
    [Tedad] [smallint] NOT NULL,
    [sabtHazineDate] [date] NOT NULL,
    [Describtion] [nvarchar](500) NULL,
 CONSTRAINT [PK_SabtHazine] PRIMARY KEY CLUSTERED 
(
    [SabtHazineID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[SabtHazine] ON
INSERT [dbo].[SabtHazine] ([SabtHazineID], [HazineID], [EndUserName], [Tedad], [sabtHazineDate], [Describtion]) VALUES (6, 3, N'SA', 3, CAST(0xC2360B00 AS Date), N'
')
SET IDENTITY_INSERT [dbo].[SabtHazine] OFF
/****** Object:  ForeignKey [FK_SabtHazine_Hazine]    Script Date: 04/07/2013 17:34:59 ******/
ALTER TABLE [dbo].[SabtHazine]  WITH CHECK ADD  CONSTRAINT [FK_SabtHazine_Hazine] FOREIGN KEY([HazineID])
REFERENCES [dbo].[Hazine] ([HazineID])
GO
ALTER TABLE [dbo].[SabtHazine] CHECK CONSTRAINT [FK_SabtHazine_Hazine]
GO

2 个答案:

答案 0 :(得分:0)

这可能会为您指明一个更好的方向:

select BH.SabtHazineId, BH.HazineID, BH.EndUserNAme, BH.Tedad, BH.sabtHazineDate, BH.Describtion,
  Coalesce( BSH.HazineName, H.HazineName ) as [HazineName],
  Coalesce( BSH.Mablagh, H.Mablagh ) as [Mablagh]
  from AgencyBackupDB.dbo.SabtHazine as BSH left outer join
    AgencyBackupDB.dbo.Hazine as BH on BH.HazineID = BSH.HazineID left outer join
    dbo.Hazine as H on H.HazineID = BSH.HazineID

它连接两个数据库中的Hazine表(假设您从AgencyDB数据库运行它)并使用COALESCE来选择(希望)非NULL值。

答案 1 :(得分:0)

select 
 BsH.SabtHazineId,
 BsH.HazineID,
 BsH.EndUserNAme,
 BsH.Tedad, 
 BsH.sabtHazineDate, 
 BsH.Describtion,
  Coalesce( BH.HazineName, H.HazineName ) as [HazineName],
  Coalesce( BH.Mablagh, H.Mablagh ) as [Mablagh]
  from AgencyBackupDB.dbo.SabtHazine as BSH 
    left outer join    AgencyBackupDB.dbo.Hazine as BH on BH.HazineID = BSH.HazineID
    left outer join   dbo.Hazine as H on H.HazineID = BSH.HazineID