2013-05-21 167 views
0

我創建一個T-SQL函數,我得到了一個錯誤:SQL函數 「聲明標量變量」

Must declare scalar variable @UserId

代碼:

CREATE FUNCTION dbo.AccountsOfTheUser 
(@UserId INT) 
RETURNS TABLE 
AS 
RETURNS 
(
    SELECT 
     dbo.accounts.account_id, dbo.accounts.account_name, 
     dbo.account_type.description, dbo.users.user_name, dbo.users.first_name, 
     dbo.users.midle_name, dbo.users.email, dbo.account_type.type_name 
    FROM 
     dbo.accounts 
    INNER JOIN 
     dbo.account_type ON dbo.account_type.type_id = dbo.accounts.type_id 
    INNER JOIN 
     dbo.users ON dbo.accounts.user_id = dbo.users.user_id 
    WHERE 
     (dbo.accounts.user_id = @UserId) 
); 
+5

'AS RETURNS'應該是'AS RETURN'除了它看起來好像mssql –

+1

@AlexK。做出答案? – gbn

+1

雖然執行時得到的錯誤信息是'RETURNS''附近的錯誤語法。我認爲'必須聲明標量變量@UserId'顯示爲intellisense? –

回答

4
CREATE FUNCTION dbo.AccountsOfTheUser 
(@UserId INT) 
RETURNS TABLE 
AS 
RETURN 
(
SELECT dbo.accounts.account_id, dbo.accounts.account_name,  dbo.account_type.description, dbo.users.user_name, dbo.users.first_name, 
      dbo.users.midle_name, dbo.users.email, dbo.account_type.type_name 
FROM dbo.accounts INNER JOIN 
      dbo.account_type ON dbo.account_type.type_id = dbo.accounts.type_id INNER JOIN 
      dbo.users ON dbo.accounts.user_id = dbo.users.user_id 
    WHERE (dbo.accounts.user_id = @UserId) 
); 
+1

謝謝......併爲我的無知感到抱歉。 –

0

唯一的問題我看到過的手是應該是RETURN的第二個RETURNS

CREATE FUNCTION dbo.AccountsOfTheUser 
(@UserId INT) 
RETURNS TABLE 
AS 
RETURN 
1
CREATE FUNCTION dbo.AccountsOfTheUser 
(@UserId INT) 
RETURNS TABLE 
BEGIN 
    RETURN (SELECT ...) 
END