2014-12-06 113 views
0

我有以下SQL語句:SQL選擇帶有功能

SELECT 
    RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),'')))     AS ItemNo, 
    RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),'')))       AS Season, 
    ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'')   AS Variant1, 
    ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'')   AS Variant2, 
    (SELECT * 
    FROM [dbo].[B2BGetSpringFinal] (LAGKART.VARENUMMER, 
            LAGKART.VARIANT1, 
            LAGKART.VARIANT2 
           )) AS SpringAvailable 
FROM 
    LAGKART 

但我得到這個錯誤:

消息170,級別15,狀態1,行8
附近有語法錯誤''。

但如果我叫具有固​​定值的功能:

SELECT 
    RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),'')))     AS ItemNo, 
    RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),'')))       AS Season, 
    ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'')   AS Variant1, 
    ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'')   AS Variant2, 
    (SELECT * 
    FROM [dbo].[B2BGetSpringFinal] ('6261', 
            'Black', 
            'S' 
            )) AS SpringAvailable 
FROM 
    LAGKART 

我得到了想要的結果。

任何想法?

溴 的Mads

+0

這是什麼方言? TSQL?它看起來有點怪異,但不夠瘋狂,以防止MySQL。我很確定你不能把結果列拉到子查詢中,或者你想要一個CTE? – Jasen 2014-12-06 07:42:13

+0

這是T-SQL 認爲你是對的,我不能在子查詢中使用結果列。 – Dauer 2014-12-06 07:46:59

回答

0

在SQL Server 2000中,只有常量和@local_variables可以傳遞到表-VA賦予功能。在SQL 2005及更高版本中,這是固定的。您可以嘗試使用標量函數來獲取SpringAvailable列值,或者查看升級到更新的SQL Server版本。

+0

我可以從SQL 2000免費升級到SQL Express 2005,所以這將是解決方案。 謝謝! – Dauer 2014-12-06 18:44:36

0

如果您的服務器支持熱膨脹係數,你可以試試這個:

WITH a as (
SELECT VARENUMMER, 
RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),'')))     AS ItemNo, 
RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),'')))       AS Season, 
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'')   AS Variant1, 
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'')   AS Variant2, 
FROM LAGKART 
) 
select *, 
(SELECT * FROM [dbo].[B2BGetSpringFinal] (a.VARENUMMER, 
              a.VARIANT1, 
              a.VARIANT2 
             ))        AS SpringAvailable 
from a ; 
+0

謝謝雅森。 稍後再試,然後回報。 – Dauer 2014-12-06 07:51:35

+0

我收到以下錯誤: Msg 156,Level 15,State 1,Line 2 關鍵字'WITH'附近的語法不正確 – Dauer 2014-12-06 13:17:24

+0

'WITH'前加''' – gotqn 2014-12-06 13:36:57

0

您可以使用APPLYCROSSOUTER)通過列(S)值(S)作爲參數到一個函數:

SELECT RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),''))) AS ItemNo, 
     RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),''))) AS Season, 
     ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'') AS Variant1, 
     ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'') AS Variant2, 
     SpringAvailable.* 
FROM LAGKART 
CROSS APPLY 
(
    SELECT * 
    FROM [dbo].[B2BGetSpringFinal] (LAGKART.VARENUMMER, LAGKART.VARIANT1,LAGKART.VARIANT2) 
) AS SpringAvailable 
+0

感謝gtqn。稍後再試一試。 – Dauer 2014-12-06 10:11:25

+0

我收到以下錯誤: 消息170,級別15,狀態1,行7 行7:'APPLY'附近的語法不正確。 – Dauer 2014-12-06 13:15:14

+0

檢查此http://stackoverflow.com/a/13922649/1080354 – gotqn 2014-12-06 13:38:33