我有两个数据库 AgencyDB(主数据库)和 AgencyBackupDB(用户从 AgencyDB 删除的信息),我在两个数据库中都有这两个表(但表之间的 AgencyBackupDB 没有关系)。这是我的问题:我想选择所有信息来自表 SabtHazine 中的 AgencyBackupDB 和来自相关表 Hazine 的信息。如果 Hazine 表中的 HazineID 在 AgencyBackupDB 中不存在,则从 AgencyDB 中选择它。这是带有测试数据的所有表的脚本
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