我會去:
ALTER FUNCTION SearchProjects (
@location NVARCHAR(50),
@purpose NVARCHAR(50),
@type NVARCHAR(50))
RETURNS TABLE
AS
RETURN
(
SELECT p.ProjectName,
p.Areas,
p.PaymentSystem,
p.ReceivedDate,
p.PropertyClassification,
p.ProjectImage,
l.LocationName,
pur.PurposeName,
t.TypeName
FROM dbo.Projects AS p
LEFT JOIN dbo.Locations AS l ON p.LocationID = l.ID
LEFT JOIN dbo.Purposes pur ON p.PurposeID = pur.ID
LEFT JOIN dbo.[Types] AS t ON p.TypeID = t.ID
WHERE UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%'
AND UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%'
AND UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%'
)
GO
,如果你只想返回項目其中全部滿足的條件並且輸入參數中的空字符串被視爲通配符:
SELECT * FROM dbo.SearchProjects('','',''); -- Returns all records
SELECT * FROM dbo.SearchProjects('north','',''); -- Returns all records with LocationName containing 'north'
SELECT * FROM dbo.SearchProjects('','research',''); -- Returns all records with PurposeName containing 'research'
SELECT * FROM dbo.SearchProjects('','','closed'); -- Returns all records with TypeName containing 'closed'
SELECT * FROM dbo.SearchProjects('north','research',''); -- Returns all records with LocationName containing 'north' and PurposeName containing 'research'
當將輸入參數值與表中的字段值進行比較時,這還會刪除任何區分大小寫。我仍然會使用LEFT JOIN
而不是INNER JOIN
,以防萬一某些項目記錄可能存在錯誤的LocationID,PurposeID或TypeID值。
如果你想回到那裏從輸入參數標準的任何滿足(當至少一個輸入參數包含的值不把空的輸入參數作爲通配符)項目,你可以改變AND
S IN在WHERE
子句OR
S和傳遞NULL
任何輸入參數,你不希望爲其指定值:
ALTER FUNCTION SearchProjects (
@location NVARCHAR(50),
@purpose NVARCHAR(50),
@type NVARCHAR(50))
RETURNS TABLE
AS
RETURN
(
SELECT p.ProjectName,
p.Areas,
p.PaymentSystem,
p.ReceivedDate,
p.PropertyClassification,
p.ProjectImage,
l.LocationName,
pur.PurposeName,
t.TypeName
FROM dbo.Projects AS p
LEFT JOIN dbo.Locations AS l ON p.LocationID = l.ID
LEFT JOIN dbo.Purposes pur ON p.PurposeID = pur.ID
LEFT JOIN dbo.[Types] AS t ON p.TypeID = t.ID
WHERE UPPER(ISNULL(l.LocationName,N'')) LIKE N'%' + UPPER(@location) + '%'
OR UPPER(ISNULL(pur.PurposeName,N'')) LIKE N'%' + UPPER(@purpose) + '%'
OR UPPER(ISNULL(t.TypeName,N'')) LIKE N'%' + UPPER(@type) + '%'
)
GO
SELECT * FROM dbo.SearchProjects('','',''); -- Returns all records
SELECT * FROM dbo.SearchProjects('north',NULL,NULL); -- Returns all records with LocationName containing 'north'
SELECT * FROM dbo.SearchProjects(NULL,'research',NULL); -- Returns all records with PurposeName containing 'research'
SELECT * FROM dbo.SearchProjects(NULL,NULL,'closed'); -- Returns all records with TypeName containing 'closed'
SELECT * FROM dbo.SearchProjects('north','research',NULL); -- Returns all records with LocationName containing 'north' or PurposeName containing 'research'
NULL
荷蘭國際集團不需要輸入參數是必要的,因爲在這裏調用函數中的任何一個空字符串輸入參數將導致全部重新正在退回的電源線:
SELECT * FROM dbo.SearchProjects('north','','');
SELECT * FROM dbo.SearchProjects('north','research','');
SELECT * FROM dbo.SearchProjects('north','','closed');
我想僅搜索不保存或將記錄插入到數據庫中 – hashim
您是否有錯誤消息? –
沒有表沒有數據返回我不知道是否選擇語句是否爲真我想從項目表中選擇項目取決於其他表 – hashim