假设我有这 2 个表,并且我有 2 种语言,en 和 ar,
CREATE TABLE [dbo].[xProduct](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Model] [varchar](255) NULL,
CONSTRAINT [PK_xProduct] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[xProductT](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PID] [int] NOT NULL,
[Lang] [varchar](2) NOT NULL,
[Name] [nvarchar](255) NULL,
[Description] [nvarchar](255) NULL,
[IsDefault] [bit] NULL,
CONSTRAINT [PK_xProductT] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
-- xProduct
ID Model
1 SVE11611
2 SVE11711
-- xProductT
ID PID Lang Name Description IsDefault
1 1 EN Sony Vaio E11611 Sony Vaio E11611 Description 1
2 1 AR سوني فايو E11611 وصف سوني فايو E11611 NULL
3 2 EN Sony Vaio E11711 Sony Vaio E11711 Description 1
这是我尝试过的,
declare @lang varchar(2) ='en'
declare @id int =1
-- Works and give a single row
Select p.*,pt.* from xProduct p inner join xProductT pt on (p.id = pt.pid and pt.lang = @lang)where p.id=@id
set @lang ='ar'
set @id =1
-- Works and give a single row
Select p.*,pt.* from xProduct p inner join xProductT pt on (p.id = pt.pid and pt.lang = @lang)where p.id=@id
set @lang ='en'
set @id =2
-- Works and give a single row
Select p.*,pt.* from xProduct p inner join xProductT pt on (p.id = pt.pid and pt.lang = @lang)where p.id=@id
set @lang ='ar'
set @id =2
-- Does not work I need to select the default one
Select p.*,pt.* from xProduct p inner join xProductT pt on (p.id = pt.pid and pt.lang = @lang)where p.id=@id
但是第四个不工作?