2010-09-28 48 views
3

這個函數的語法有什麼問題?我收到以下錯誤:SQL這裏的語法有什麼問題?

 ALTER FUNCTION [dbo].[udf_ReportingLevelStructure2] 
( 
    @CompanyID INT 
) 
RETURNS @result TABLE 
( 
CompanyName VARCHAR(300), 
[rl_Index] INT, 
[rl_Addr1] VARCHAR(MAX), 
[rl_Addr2] VARCHAR(MAX), 
[rl_DisplayName] VARCHAR(MAX) 
) 
AS 
BEGIN 


DECLARE @tmp1 TABLE 
(
CompanyName VARCHAR(300), 
[rl_Index] INT, 
[rl_Addr1] VARCHAR(MAX), 
[rl_Addr2] VARCHAR(MAX), 
[rl_DisplayName] VARCHAR(MAX) 
); 
DECLARE @tmp2 TABLE 
(
[CompanyName] VARCHAR(300), 
[rl_Index] INT, 
[rl_Addr1] VARCHAR(MAX), 
[rl_Addr2] VARCHAR(MAX), 
[rl_DisplayName] VARCHAR(MAX) 
) 

INSERT INTO @tmp1([CompanyName],[rl_Index],[rl_Addr1],[rl_Addr2],[rl_DisplayName]) 
(SELECT 
     rl0.[rl_Name] AS [CompanyName] 
     ,rl_Index = 
     CASE 
      WHEN rl9.[rl_Name] IS NOT NULL THEN rl9.[rl_Index] 
      WHEN rl8.[rl_Name] IS NOT NULL THEN rl8.[rl_Index] 
      WHEN rl7.[rl_Name] IS NOT NULL THEN rl7.[rl_Index] 
      WHEN rl6.[rl_Name] IS NOT NULL THEN rl6.[rl_Index] 
      WHEN rl5.[rl_Name] IS NOT NULL THEN rl5.[rl_Index] 
      WHEN rl4.[rl_Name] IS NOT NULL THEN rl4.[rl_Index] 
      WHEN rl3.[rl_Name] IS NOT NULL THEN rl3.[rl_Index] 
      WHEN rl2.[rl_Name] IS NOT NULL THEN rl2.[rl_Index] 
      WHEN rl1.[rl_Name] IS NOT NULL THEN rl1.[rl_Index] 
     END 
     ,rl_Addr1 = 
     CASE 
      WHEN rl9.[rl_Name] IS NOT NULL THEN rl9.[rl_Addr1] 
      WHEN rl8.[rl_Name] IS NOT NULL THEN rl8.[rl_Addr1] 
      WHEN rl7.[rl_Name] IS NOT NULL THEN rl7.[rl_Addr1] 
      WHEN rl6.[rl_Name] IS NOT NULL THEN rl6.[rl_Addr1] 
      WHEN rl5.[rl_Name] IS NOT NULL THEN rl5.[rl_Addr1] 
      WHEN rl4.[rl_Name]IS NOT NULL THEN rl4.[rl_Addr1] 
      WHEN rl3.[rl_Name] IS NOT NULL THEN rl3.[rl_Addr1] 
      WHEN rl2.[rl_Name] IS NOT NULL THEN rl2.[rl_Addr1] 
      WHEN rl1.[rl_Name] IS NOT NULL THEN rl1.[rl_Addr1] 
     END 
     ,rl_Addr2 = 
     CASE 
      WHEN rl9.[rl_Name] IS NOT NULL THEN rl9.[rl_Addr2] 
      WHEN rl8.[rl_Name] IS NOT NULL THEN rl8.[rl_Addr2] 
      WHEN rl7.[rl_Name] IS NOT NULL THEN rl7.[rl_Addr2] 
      WHEN rl6.[rl_Name] IS NOT NULL THEN rl6.[rl_Addr2] 
      WHEN rl5.[rl_Name] IS NOT NULL THEN rl5.[rl_Addr2] 
      WHEN rl4.[rl_Name] IS NOT NULL THEN rl4.[rl_Addr2] 
      WHEN rl3.[rl_Name] IS NOT NULL THEN rl3.[rl_Addr2] 
      WHEN rl2.[rl_Name] IS NOT NULL THEN rl2.[rl_Addr2] 
      WHEN rl1.[rl_Name]IS NOT NULL THEN rl1.[rl_Addr2] 
     END 
     ,CASE 
      WHEN rl9.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name]+'> '+ rl5.[rl_Name]+'> '+ rl6.[rl_Name]+'> '+rl7.[rl_Name]+'> '+ rl8.[rl_Name]+'> '+ rl9.[rl_Name] 
      WHEN rl8.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name]+'> '+ rl5.[rl_Name]+'> '+ rl6.[rl_Name]+'> '+rl7.[rl_Name]+'> '+ rl8.[rl_Name] 
      WHEN rl7.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name]+'> '+ rl5.[rl_Name]+'> '+ rl6.[rl_Name]+'> '+rl7.[rl_Name] 
      WHEN rl6.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name]+'> '+ rl5.[rl_Name]+'> '+ rl6.[rl_Name] 
      WHEN rl5.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name]+'> '+ rl5.[rl_Name] 
      WHEN rl4.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name]+'> '+rl4.[rl_Name] 
      WHEN rl3.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name]+'> '+rl3.[rl_Name] 
      WHEN rl2.[rl_Name] IS NOT NULL THEN rl1.[rl_Name]+'> '+rl2.[rl_Name] 
      WHEN rl1.[rl_Name] IS NOT NULL THEN rl1.[rl_Name] 
     END AS [rl_DisplayName] 

    FROM 
     dbo.[ReportingLevels] rl0 

     LEFT JOIN ReportingLevels rl1 
      ON (rl1.[rl_CompanyID] = @CompanyID 
      AND rl1.[rl_Indent1] <> 0 
      AND rl1.[rl_Indent2] = 0) 

     LEFT JOIN ReportingLevels rl2 
      ON (rl2.[rl_CompanyID] = @CompanyID 
      AND rl2.[rl_Indent1] = rl1.[rl_Indent1] 
      AND rl2.[rl_Indent2] <> 0 
      AND rl2.[rl_Indent3] = 0) 

     LEFT JOIN ReportingLevels rl3 
      ON (rl3.[rl_CompanyID] = @CompanyID 
      AND rl3.[rl_Indent2] = rl2.[rl_Indent2] 
      AND rl3.[rl_Indent1] = rl2.[rl_Indent1] 
      AND rl3.[rl_Indent3] <> 0 
      AND rl3.[rl_Indent4] = 0) 

     LEFT JOIN ReportingLevels rl4 
      ON (rl4.[rl_CompanyID] = @CompanyID 
      AND rl4.[rl_Indent3]= rl3.[rl_Indent3] 
      AND rl4.[rl_Indent2] = rl3.[rl_Indent2] 
      AND rl4.[rl_Indent1] = rl3.[rl_Indent1] 
      AND rl4.[rl_Indent4] <> 0 
      AND rl4.[rl_Indent5] = 0) 

     LEFT JOIN ReportingLevels rl5 
      ON (rl5.[rl_CompanyID] = @CompanyID 
      AND rl5.[rl_Indent4] = rl4.[rl_Indent4] 
      AND rl5.[rl_Indent3] = rl4.[rl_Indent3] 
      AND rl5.[rl_Indent2] = rl4.[rl_Indent2] 
      AND rl5.[rl_Indent1] = rl4.[rl_Indent1] 
      AND rl5.[rl_Indent5] <> 0 
      AND rl5.[rl_Indent6]= 0) 

     LEFT JOIN ReportingLevels rl6 
      ON (rl6.[rl_CompanyID] = @CompanyID 
      AND rl6.[rl_Indent5] = rl5.rl_Indent5 
      AND rl6.[rl_Indent4] = rl5.rl_Indent4 
      AND rl6.[rl_Indent3] = rl5.rl_Indent3 
      AND rl6.[rl_Indent2] = rl5.rl_Indent2 
      AND rl6.[rl_Indent1] = rl5.rl_Indent1 
      AND rl6.[rl_Indent6] <> 0 
      AND rl6.[rl_Indent7] = 0) 

     LEFT JOIN ReportingLevels rl7 
      ON (rl7.[rl_CompanyID] = @CompanyID 
      AND rl7.[rl_Indent6] = rl6.[rl_Indent6] 
      AND rl7.[rl_Indent5] = rl6.[rl_Indent5] 
      AND rl7.[rl_Indent4] = rl6.[rl_Indent4] 
      AND rl7.[rl_Indent3] = rl6.[rl_Indent3] 
      AND rl7.[rl_Indent2] = rl6.[rl_Indent2] 
      AND rl7.[rl_Indent1] = rl6.[rl_Indent1] 
      AND rl7.[rl_Indent7] <> 0 
      AND rl7.[rl_Indent8] = 0)   

     LEFT JOIN ReportingLevels rl8 
      ON (rl8.[rl_CompanyID] = @CompanyID 
      AND rl8.[rl_Indent7] = rl7.[rl_Indent7] 
      AND rl8.[rl_Indent6] = rl7.[rl_Indent6] 
      AND rl8.[rl_Indent5] = rl7.[rl_Indent5] 
      AND rl8.[rl_Indent4] = rl7.[rl_Indent4] 
      AND rl8.[rl_Indent3] = rl7.[rl_Indent3] 
      AND rl8.[rl_Indent2] = rl7.[rl_Indent2] 
      AND rl8.[rl_Indent1] = rl7.[rl_Indent1] 
      AND rl8.[rl_Indent8]<> 0 
      AND rl7.[rl_Indent9] = 0) 

     LEFT JOIN ReportingLevels rl9 
      ON (rl9.[rl_CompanyID] = @CompanyID 
      AND rl9.[rl_Indent8] = rl8.[rl_Indent8] 
      AND rl9.[rl_Indent7] = rl8.[rl_Indent7] 
      AND rl9.[rl_Indent6] = rl8.[rl_Indent6] 
      AND rl9.[rl_Indent5] = rl8.[rl_Indent5] 
      AND rl9.[rl_Indent4] = rl8.[rl_Indent4] 
      AND rl9.[rl_Indent3] = rl8.[rl_Indent3] 
      AND rl9.[rl_Indent2] = rl8.[rl_Indent2] 
      AND rl9.[rl_Indent1] = rl8.[rl_Indent1] 
      AND rl9.[rl_Indent9] <> 0 
      AND rl9.[rl_Indent10] = 0) 

    WHERE 
     rl0.[rl_CompanyID] = @CompanyID 
     AND rl0.[rl_Indent1] = 0); 

INSERT INTO @tmp2([CompanyName],[rl_Index],[rl_Addr1],[rl_Addr2],[rl_DisplayName]) 
(SELECT DISTINCT 
rl0.[rl_Name] AS [CompanyName] 
,rl1.[rl_Index] 
,rl1.[rl_Addr1] 
,rl1.[rl_Addr2] 
,CASE WHEN (rl1.[rl_indent1] <> 0 AND rl1.[rl_indent2] = 0 AND rl1.[rl_indent3] = 0 AND rl1.[rl_indent4] = 0 AND rl1.[rl_indent5] = 0 AND rl1.[rl_indent6] = 0 AND rl1.[rl_indent7] = 0 AND rl1.[rl_indent8] = 0 AND rl1.[rl_indent9] = 0) THEN rl1.[rl_Name] END AS [rl_DisplayName] 

FROM 
     [dbo].[ReportingLevels] rl0 

     LEFT JOIN ReportingLevels rl1 
      ON (rl1.[rl_CompanyID] = @CompanyID 
      AND rl1.[rl_Indent1] <> 0 
      AND rl1.[rl_Indent2] = 0) 

     LEFT JOIN ReportingLevels rl2 
      ON (rl2.[rl_CompanyID] = @CompanyID 
      AND rl2.[rl_Indent1] = rl1.[rl_Indent1] 
      AND rl2.[rl_Indent2] <> 0 
      AND rl2.[rl_Indent3] = 0) 

     LEFT JOIN ReportingLevels rl3 
      ON (rl3.[rl_CompanyID] = @CompanyID 
      AND rl3.[rl_Indent2] = rl2.[rl_Indent2] 
      AND rl3.[rl_Indent1] = rl2.[rl_Indent1] 
      AND rl3.[rl_Indent3] <> 0 
      AND rl3.[rl_Indent4] = 0) 

     LEFT JOIN ReportingLevels rl4 
      ON (rl4.[rl_CompanyID] = @CompanyID 
      AND rl4.[rl_Indent3] = rl3.[rl_Indent3] 
      AND rl4.[rl_Indent2] = rl3.[rl_Indent2] 
      AND rl4.[rl_Indent1] = rl3.[rl_Indent1] 
      AND rl4.[rl_Indent4] <> 0 
      AND rl4.[rl_Indent5] = 0) 

     LEFT JOIN [ReportingLevels] rl5 
      ON (rl5.[rl_CompanyID] = @CompanyID 
      AND rl5.[rl_Indent4] = rl4.[rl_Indent4] 
      AND rl5.[rl_Indent3] = rl4.[rl_Indent3] 
      AND rl5.[rl_Indent2] = rl4.[rl_Indent2] 
      AND rl5.[rl_Indent1] = rl4.[rl_Indent1] 
      AND rl5.[rl_Indent5] <> 0 
      AND rl5.[rl_Indent6] = 0) 

     LEFT JOIN ReportingLevels rl6 
      ON (rl6.[rl_CompanyID] = @CompanyID 
      AND rl6.[rl_Indent5] = rl5.[rl_Indent5] 
      AND rl6.[rl_Indent4] = rl5.[rl_Indent4] 
      AND rl6.[rl_Indent3] = rl5.[rl_Indent3] 
      AND rl6.[rl_Indent2]= rl5.[rl_Indent2] 
      AND rl6.[rl_Indent1] = rl5.[rl_Indent1] 
      AND rl6.[rl_Indent6] <> 0 
      AND rl6.[rl_Indent7] = 0) 

     LEFT JOIN ReportingLevels rl7 
      ON (rl7.[rl_CompanyID] = @CompanyID 
      AND rl7.[rl_Indent6] = rl6.[rl_Indent6] 
      AND rl7.[rl_Indent5] = rl6.[rl_Indent5] 
      AND rl7.[rl_Indent4] = rl6.[rl_Indent4] 
      AND rl7.[rl_Indent3] = rl6.[rl_Indent3] 
      AND rl7.[rl_Indent2] = rl6.[rl_Indent2] 
      AND rl7.[rl_Indent1] = rl6.[rl_Indent1] 
      AND rl7.[rl_Indent7] <> 0 
      AND rl7.[rl_Indent8] = 0)   

     LEFT JOIN ReportingLevels rl8 
      ON (rl8.[rl_CompanyID] = @CompanyID 
      AND rl8.[rl_Indent7] = rl7.[rl_Indent7] 
      AND rl8.[rl_Indent6] = rl7.[rl_Indent6] 
      AND rl8.[rl_Indent5] = rl7.[rl_Indent5] 
      AND rl8.[rl_Indent4] = rl7.[rl_Indent4] 
      AND rl8.[rl_Indent3] = rl7.[rl_Indent3] 
      AND rl8.[rl_Indent2] = rl7.[rl_Indent2] 
      AND rl8.[rl_Indent1] = rl7.[rl_Indent1] 
      AND rl8.[rl_Indent8] <> 0 
      AND rl7.[rl_Indent9] = 0) 

     LEFT JOIN ReportingLevels rl9 
      ON (rl9.[rl_CompanyID] = @CompanyID 
      AND rl9.[rl_Indent8] = rl8.[rl_Indent8] 
      AND rl9.[rl_Indent7] = rl8.[rl_Indent7] 
      AND rl9.[rl_Indent6] = rl8.[rl_Indent6] 
      AND rl9.[rl_Indent5] = rl8.[rl_Indent5] 
      AND rl9.[rl_Indent4] = rl8.[rl_Indent4] 
      AND rl9.[rl_Indent3] = rl8.[rl_Indent3] 
      AND rl9.[rl_Indent2] = rl8.[rl_Indent2] 
      AND rl9.[rl_Indent1] = rl8.[rl_Indent1] 
      AND rl9.[rl_Indent9] <> 0 
      AND rl9.[rl_Indent10] = 0) 

    WHERE 
     rl0.[rl_CompanyID] = @CompanyID 
     AND rl0.[rl_Indent1] = 0); 


INSERT INTO @result 
SELECT * FROM @tmp1 
UNION 
SELECT * FROM @tmp2 
ORDER BY [rl_DisplayName] 

RETURN 
END 

回答

2

您正在嘗試使用語法的內聯表值函數時,你實際上有一個多語句表值函數。請參閱完整說明here

您使用此:

--Transact-SQL Inline Table-Valued Function Syntax 
CREATE FUNCTION [ schema_name. ] function_name ([ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type 
    [ =default ] [ READONLY ] } 
    [ ,...n ] 
    ] 
) 
RETURNS TABLE 
    [ WITH <function_option> [ ,...n ] ] 
    [ AS ] 
    RETURN [ (] select_stmt [) ] 
[ ; ] 

,但你真的想這樣:

--Transact-SQL Multistatement Table-valued Function Syntax 
CREATE FUNCTION [ schema_name. ] function_name ([ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type 
    [ =default ] [READONLY] } 
    [ ,...n ] 
    ] 
) 
RETURNS @return_variable TABLE <table_type_definition> 
    [ WITH <function_option> [ ,...n ] ] 
    [ AS ] 
    BEGIN 
     function_body 
     RETURN 
    END 
[ ; ] 
+0

現在說,函數中包含的Select語句不能將數據返回給客戶端。 這是不正確末選擇具有這種* FROM @ TMP1 UNION SELECT * FROM @ TMP2 ORDER BY [rl_DisplayName] RETURN END – user380432 2010-09-28 17:43:32

+0

你想'INSERT INTO @return_variable SELECT * FROM @ TMP1 UNION SELECT * FROM @ tmp2 ORDER BY [rl_DisplayName] RETURN END'。它是'@ return_variable',用於將結果集傳回給調用者。 – 2010-09-28 18:11:07

+0

當我現在改變它說不能改變'dbo.udf_ReportingLevelStructure2',因爲它是一個不兼容的對象類型。 – user380432 2010-09-28 18:13:53

0

您的內聯表頭語法值函數

... 
RETURNS TABLE 
AS 
RETURN 
(
    SELECT .. FROM something WHERE ... 
) 
GO --end of function 

你有一個多值表函數。

... 
RETURNS @result TABLE 
(
CompanyName VARCHAR(300), 
[rl_Index] INT, 
[rl_Addr1] VARCHAR(MAX), 
[rl_Addr2] VARCHAR(MAX), 
[rl_DisplayName] VARCHAR(MAX) 
); 
AS 
BEGIN 
... 
Do this 

Do that 

Data into @result 

END --of function 
GO 

See MSDN for examples

+0

現在說包含一個函數內不能將數據返回到客戶端Select語句。這是不正確的有這末尾SELECT * FROM @ tmp1 UNION SELECT * FROM @ tmp2 ORDER BY [rl_DisplayName]返回END – user380432 2010-09-28 17:57:03

+0

當然。我的答案是「Data into @result」,MSDN的例子也是如此...... – gbn 2010-09-28 18:09:07

2

若要更改功能類型,你必須放棄功能,那麼只有你可以通過改變功能類型創造新型功能。

有三種類型的功能。

  • 標量
  • 內嵌表值
  • 多聲明

ALTER不能用來改變功能類型。

您需要刪除並創建功能

0

剛落,並創建SQL語句

enter image description here