2017-08-09 217 views
0

我想寫一個存儲過程返回存款記錄一個特定的人,存款的那個人,並且存款的那個人的行數目。參數@personID總是提供正確PERSONID;然而@SpecificRow是0(意思是:返回最近的存款)或行號(意思是:在這個特定的行返回記錄)。SQL Server存儲過程語法錯誤

腳本:

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [schema].[procedure] 
    @personID varchar(5), 
    @SpecificRow int, 
    @RowNumber INT OUTPUT, 
    @RowCount INT OUTPUT 
AS 
BEGIN 
    IF OBJECT_ID('#TempSortedDeposits') IS NOT NULL 
     DROP Table #TempSortedDeposits 

    -- get all deposits with record number into temp file for specific personID 
    SELECT 
     ROW_NUMBER() OVER (ORDER BY Date ASC) AS RecordNo, * 
    INTO 
     #TempSortedDeposits 
    FROM 
     persons.Deposits 
    WHERE 
     personID = @personID 

    -- get record count in file 
    SELECT @RowCount = COUNT(personID) 
    FROM persons.Deposits 
    WHERE personID = @personID 

    IF @SpecificRow = 0 --get most recent record 
     SET @RowNumber = @RowCount; 
    ELSE 
     --get record by rownumber 
     SET @RowNumber = @SpecificRow; 

    SELECT * 
    FROM #TempSortedDeposits 
    WHERE RecordNo = ­@RowNumber 
END 

當我嘗試運行ALTER語句,我得到以下錯誤:

Msg 102, Level 15, State 1, Procedure procedure, Line 33 [Batch Start Line 9]
Incorrect syntax near '­'.

任何見解?

謝謝。

+1

檢查什麼傳遞給帕拉姆@personID? –

+1

@FullMoonFisher這不應該造成編譯錯誤,只是執行。 – scsimon

回答

1

我刪除了這條線SELECT * FROM #TempSortedDeposits WHERE RecordNo = @RowNumber和手動重新輸入那麼它的工作。

這是因爲錯誤的發生是由於複製和粘貼。該結果到粘貼代碼包含不可打印的字符等 d非制動空格。

+0

謝謝。你是如何認定這條線是問題的? – ti034

+1

因爲這個錯誤的附近‘「附近有語法錯誤'。由非打印字符,如無後打印出來幾乎總是被調用’。 –

1

只是一個側面說明...你#TempSortedDeposits表直線下跌,你認爲它是。

這裏有一個簡單的例子:

create table #TempSortedDeposits (i int) 
insert into #TempSortedDeposits 
values 
(1) 

IF OBJECT_ID('#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits 
select * from #TempSortedDeposits 

如果你運行這個第一次,它將返回1,當它不應該,因爲它應該被刪除。在第二次運行(相同的連接),您將收到此錯誤,顯示你的表依然存在,並沒有下降:

Msg 2714, Level 16, State 6, Line 2 There is already an object named '#TempSortedDeposits' in the database.

爲了解決這個問題,語法更改爲:

IF OBJECT_ID('tempdb..#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits 

下面是測試

create table #TempSortedDeposits (i int) 
insert into #TempSortedDeposits 
values 
(1) 

IF OBJECT_ID('tempdb..#TempSortedDeposits') IS NOT NULL DROP TABLE #TempSortedDeposits 
select * from #TempSortedDeposits 

如果您先手動刪除該表(因爲我們用最後的運行創造了它),然後運行它,您將收到錯誤消息:

Msg 208, Level 16, State 0, Line 7 Invalid object name '#TempSortedDeposits'.

這意味着選擇失敗,從而表明該表實際上下降了。

-1
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
create PROCEDURE [procedure] 
    @personID varchar(5), 
    @SpecificRow int, 
    @RowNumber INT OUTPUT, 
    @RowCount INT OUTPUT 
AS 
BEGIN 
    IF OBJECT_ID('#TempSortedDeposits') IS NOT NULL 
     DROP Table #TempSortedDeposits 

    -- get all deposits with record number into temp file for specific personID 
    SELECT 
     ROW_NUMBER() OVER (ORDER BY Date ASC) AS RecordNo, * 
    INTO 
     #TempSortedDeposits 
    FROM 
     persons.Deposits 
    WHERE 
     personID = @personID 

    -- get record count in file 
    SELECT @RowCount = COUNT(personID) 
    FROM persons.Deposits 
    WHERE personID = @personID 

    IF @SpecificRow = 0 --get most recent record 
     SET @RowNumber = @RowCount; 
    ELSE 
     --get record by rownumber 
     SET @RowNumber = @SpecificRow; 
SELECT * 
    FROM #TempSortedDeposits 
    WHERE RecordNo = @RowNumber 
END 
--corrected the proc 
+1

您刪除從PROC名稱的模式,並且應該解決它? – scsimon

1

我已經刪除了下面一行中的額外字符並重新執行此過程。線是 SELECT * FROM #TempSortedDeposits WHERE RecordNo = @RowNumber