這天我遇到了一個問題。在我的MS SQL數據庫中,我有一個文章表(他們的詳細信息,如插入日期,插入用戶和其他工作人員)和一個表與多種語言的文章正文。我希望文章正文處於用戶首選語言。但並非所有文章都是用所有語言寫成的。所以,會很好,首先要用用戶首選語言搜索文章,如果不存在,用第一種語言獲取文章。如果在WHERE子句中存在
爲此,我使用了一個函數。 WHERE子句是這樣的:
WHERE [tblBody].[Language] = @Language AND
[tblTitle].[Language] = @Language
,但將是很好,如果它會是這樣: SELECT(如果身體是我的首選語言獲得該機構;其他給我一個(也許我的理解是))
我希望你明白我想做什麼,女巫是我的問題。
預先感謝您!
UPDATE
這是我這就需要修改實際查詢:
ALTER FUNCTION [fx_GetNews]
(
@MinimumPermission INT,
@UserID UNIQUEIDENTIFIER,
@OwnerID UNIQUEIDENTIFIER = NULL,
@Title NVARCHAR(250) = NULL,
@Body VARCHAR(150) = NULL,
@InsertDateStart DATETIME = NULL,
@InsertDateEnd DATETIME = NULL,
@CreatedByUserID UNIQUEIDENTIFIER = NULL,
@ExpirationDate DATETIME = NULL,
@Language VARCHAR(150)
)
RETURNS @News TABLE
(
[Id] UNIQUEIDENTIFIER
,[OwnerID] UNIQUEIDENTIFIER
,[Title] NVARCHAR(250)
,[TitlePictureUrl] VARCHAR(150)
,[Body] NVARCHAR(max)
,[Visible] BIT
,[InsertDate] DATETIME
,[CreatedByUserId] UNIQUEIDENTIFIER
,[ModifiedDate] DATETIME
,[ModifiedByUserId] UNIQUEIDENTIFIER
,[ModifiedByPerson] VARCHAR(250)
,[ExpirationDate] DATETIME
,[CreatedByPerson] VARCHAR(250)
,[Permission] INT
)
AS
BEGIN
INSERT INTO @News
([Id]
,[OwnerID]
,[Title]
,[TitlePictureUrl]
,[Body]
,[Visible]
,[InsertDate]
,[CreatedByUserId]
,[ModifiedDate]
,[ModifiedByUserId]
,[CreatedByPerson]
,[ModifiedByPerson]
,[ExpirationDate]
,[Permission])
SELECT
[dbo].[tblNews].[Id]
,[dbo].[tblNews].[OwnerID]
,CAST([tblTitle].[Text] AS VARCHAR(150))
,[dbo].[tblNews].[TitlePictureUrl]
,[tblBody].[Text]
,[dbo].[tblNews].[Visible]
,[dbo].[tblNews].[InsertDate]
,[dbo].[tblNews].[CreatedByUserId]
,[dbo].[tblNews].[ModifiedDate]
,[dbo].[tblNews].[ModifiedByUserId]
,[dbo].[tblNews].[CreatedByPerson]
,[dbo].[tblNews].[ModifiedByPerson]
,[dbo].[tblNews].[ExpirationDate]
,[eportofolii].[fx_GetPermissionForObject] (@UserID, [dbo].[tblNews].[ID], 1, 1)
FROM [dbo].[tblNews]
INNER JOIN [dbo].[tblAdmTranslateText] AS [tblBody] ON
[tblBody].[OwnerID] = [dbo].[tblNews].[ID]
INNER JOIN [dbo].[tblAdmTranslateText] AS [tblTitle] ON
[tblTitle].[OwnerID] = [dbo].[tblNews].[ID]
WHERE
[eportofolii].[fx_GetPermissionForObject] (@UserID, [dbo].[tblNews].[ID], 1, 1) >= @MinimumPermission AND
([dbo].[tblNews].[OwnerID] = ISNULL(@OwnerID, [dbo].[tblNews].[OwnerID]) OR [dbo].[tblNews].[OwnerID] IS NULL) AND
[tblTitle].[Text] LIKE '%' + ISNULL(@Title, '') + '%' AND
[tblBody].[Text] LIKE '%' + ISNULL(@Body, '') + '%' AND
([dbo].[tblNews].[InsertDate] BETWEEN ISNULL(@InsertDateStart, ([dbo].[tblNews].[InsertDate] - 7)) AND ISNULL(@InsertDateEnd, [dbo].[tblNews].[InsertDate] + 1)) AND
[dbo].[tblNews].[CreatedByUserID] = ISNULL(@CreatedByUserID, [dbo].[tblNews].[CreatedByUserID]) AND
([dbo].[tblNews].[ExpirationDate] > ISNULL(@ExpirationDate, GETDATE() - 1) OR [dbo].[tblNews].[ExpirationDate] IS NULL) AND
[tblBody].[UDF_2] = 'Body' AND
[tblTitle].[UDF_2] = 'Title' AND
[tblBody].[UDF_1] = 'News' AND
[tblTitle].[UDF_1] = 'News' AND
[tblBody].[Language] = @Language AND
[tblTitle].[Language] = @Language
ORDER BY [InsertDate] DESC
RETURN
END
的問題是在這裏結束:
[tblBody].[Language] = @Language AND
[tblTitle].[Language] = @Language
謝謝!
否則給你*哪一個?請問哪個版本的SQL Server? –
你是否真的將你的文章的正文和標題存儲在單獨的表格中?是否有出版日期的第三張表格,作者ID等的第四張表格?如果您顯示實際的表格結構以及它們之間的關係,您可能會得到有效的查詢而不是猜測...... –
有一張新聞詳細信息表(發佈日期,作者...)和另一個包含不同語言文本的表格。這第二個表格包含不同行上的標題和正文。 –