0

私は 2 つのデータベース AgencyDB (プライマリ DB) と AgencyBackupDB (ユーザーによって AgencyDB から削除された情報) を持っており、両方の DB にこの 2 つのテーブルがあります (ただし、AgencyBackupDB にはテーブル間に関係はありません)。これは私の質問です: すべての情報を選択したいテーブル SabtHazine の AgencyBackupDB から、および関連テーブル Hazine.i からの情報が必要です。これは、テストデータを含むすべてのテーブルのスクリプトです

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 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(データベースから実行すると仮定) 、(できれば) NULL 以外の値を選択するためにAgencyDB使用します。COALESCE

于 2013-05-25T14:07:49.273 に答える
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 に答える