2016-11-21 124 views
0

執行功能我有四個表:創建和SQL Server

  1. dbo.Projects
  2. dbo.Locations(ID,地點名稱)
  3. dbo.Purpose(ID,Purposename)
  4. dbo.Types(ID,typname)

我有一個搜索條件,這個條件是填充數據庫表中的數據:locations,purposetypes

我想創建一個函數,該函數根據其他表從projects的搜索結果返回表。我創建了一個,但它並沒有做我所需要的:

ALTER FUNCTION SearchProjects 
(
    @location nvarchar(50), 
    @purpose nvarchar(50), 
    @type nvarchar(50) 
) 
RETURNS TABLE 
AS 
RETURN 
(
     SELECT dbo.Projects.ProjectName, dbo.Projects.Areas,   
      dbo.Projects.PaymentSystem, dbo.Projects.ReceivedDate,  
      dbo.Projects.PropertyClassification, 
      dbo.Projects.ProjectImage 
     FROM dbo.Locations INNER JOIN 
     INNER JOIN dbo.Projects ON dbo.Locations.ID = dbo.Projects.ID  
     INNER JOIN dbo.Purpose ON dbo.Locations.ID = dbo.Purpose.ID 
     INNER JOIN dbo.Types ON dbo.Locations.ID = dbo.Types.ID 
     WHERE (Projects.ProjectName like N'%'+ @location +'%' 
     and Purpose.PurposeName = N'%'+ @purpose +'%' 
     and Types.TypeName like N'%'+ @type  +'%') 
) 
GO 
SELECT * FROM dbo.SearchProjects('',' ',''); 

我是SQL SERVER的新手,所以不勝感激。

+0

我想僅搜索不保存或將記錄插入到數據庫中 – hashim

+0

您是否有錯誤消息? –

+0

沒有表沒有數據返回我不知道是否選擇語句是否爲真我想從項目表中選擇項目取決於其他表 – hashim

回答

1

我會去:

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'); 
+0

它的工作非常感謝,如果你不介意我想通過Skype或郵件與你聯繫。 – hashim

+0

太好了,我很高興你有你想要的解決方案。我不會將我的聯繫方式發送給我不認識的人,儘管我害怕 - 爲什麼你想和我一起發送電子郵件/ Skype? – 3N1GM4

+0

,因爲我想從你那裏學到更多,如果你不介意,因爲我是初學者,可以獲得更多的經驗? – hashim

0

這些連接看起來不合適我。你沒有完全指定你的模式,所以我猜測,但有一個使用這些連接的情況是非常不尋常的。

在這個示例中,我更改了爲每個連接指定的列,但我猜測列中的列可能會被命名。

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 
    FROM dbo.Projects As p 
    LEFT JOIN dbo.Locations As l ON p.LocationID = l.ID  
    LEFT JOIN dbo.Purpose ON p.PurposeID = Purpose.ID 
    LEFT JOIN dbo.Types As t ON p.TypeID = t.ID 
    WHERE (l.LocationName like N'%'+ @location +'%' 
     OR Purpose.PurposeName = N'%'+ @purpose +'%' 
     OR t.TypeName like N'%'+ @type  +'%') 
) 
GO 

SELECT * FROM dbo.SearchProjects('',' ',''); 

很抱歉,如果我錯過了問題或顯示註釋東西,這不是答案...

+0

這不是假設'Projects'表的每個'LocationId','PurposeId'和'TypeID'字段中的值都與'Locations.ID','Purpose.ID'或'Types.ID'分別?我認爲我們應該在這裏使用'LEFT OUTER JOIN'而不是'INNER JOIN'。除非[hashim](http://stackoverflow.com/users/6704418/hashim)可以確認'Projects'中的任何給定記錄的每個連接表中都會有記錄。 – 3N1GM4

+0

並且調用'SELECT * FROM dbo.SearchProjects('','','');'將只返回關聯的PurposeName包含空格的記錄。 – 3N1GM4

+0

關於'LEFT'和'INNER'的好處 – mendosi