0

我有两个数据库 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
4

2 回答 2

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
于 2013-05-26T19:38:47.760 回答
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 值。

于 2013-05-25T14:07:49.273 回答