TSQL存储过程语法错误

时间:2015-11-14 09:10:22

标签: sql-server tsql stored-procedures

我尝试编写一个存储过程,但是当我把所有内容放在一起时单个部分似乎是正确的我得到(3)语法错误:

  

靠近关键字'CASE'

     

关键字'ELSE'

附近      

关键字'FROM'

附近

**增加:
查询的目的是计算当前的跟踪编号,将最新种子的1加到给定批次中的数字,并且如果达到当前批次的最后一个数字......将新批次复制到当前批次中 **

这是存储过程:如果有人可以给出提示:

CREATE PROCEDURE dbo.spGetTrackingNumber
(
  @SUID         nvarchar (60),
  @CarrierID    nvarchar (20)
)
AS
SET NOCOUNT ON;
DECLARE @S      nvarchar(max)='',
        @P      nvarchar(max)='',
        @OUT    nvarchar(max)=''

SELECT @S= 'CASE WHEN (s.LastSeed+1=s.FinalSeed )
            THEN    
                BEGIN
                    UPDATE t SET 
                        LastSeed=NewInitialSeed, 
                        FinalSeed=NewFinalSeed, 
                        NewInitialSeed=NULL, 
                        NewFinalSeed=NULL 
                    OUTPUT 
                        deleted.LastSeed+1 AS CurrentSeed, 
                        deleted.Prefix, 
                        deleted.PostFix, 
                        deleted.CarrierName, 
                        deleted.TrackingUrl
                        (FinalSeed - inserted.LastSeed) AS RemainingSeeds
                    FROM TrackingSeed   t
                    JOIN Users          u   ON  t.UID=u.UID
                    JOIN SubUsers       su  ON  su.UID=u.UID
                    WHERE 
                        su.SUID=@_SUID 
                    AND t.CarrierCode=@_CarrierID 
                END
            ELSE 
                BEGIN
                    UPDATE t SET 
                        LastSeed=LastSeed+1 
                    OUTPUT 
                        inserted.LastSeed+1 AS CurrentSeed, 
                        inserted.Prefix, Inserted.PostFix, 
                        inserted.CarrierName, 
                        inserted.TrackingUrl, 
                        (FinalSeed - inserted.LastSeed) AS RemainingSeeds
                    FROM TrackingSeed t
                    JOIN Users u ON t.UID=u.UID
                    JOIN SubUsers su ON su.UID=u.UID
                    WHERE 
                        su.SUID=@_SUID 
                    AND t.CarrierCode=@_CarrierID 
                END
            END  
        FROM TrackingSeed       s 
            JOIN Users          u   ON  s.UID=u.UID 
            JOIN SubUsers       su  ON  u.UID=su.UID 
            WHERE 
                s.CarrierCode=@_CarrierID 
            AND su.SUID=@_SUID'

            PRINT @S                    


SET @P= '@_SUID         nvarchar(60),
         @_CarrierID    nvarchar(20),
         @_OUT          nvarchar(max) OUTPUT'

EXEC sp_executesql @S, @P, @SUID, @CarrierID,@OUT OUTPUT
SET NOCOUNT OFF

谢谢!

2 个答案:

答案 0 :(得分:3)

我不明白为什么你甚至诉诸动态SQL - 完全不需要它!

我完全不了解您从示例代码 fig = plt.figure() ax = fig.add_subplot(111) ax.set_ylim(max_l,max_r) ax.set_xlim(max_l,max_r) x1 = data[int(frame_1)][int(frame_2)][1] x2= data[int(frame_1)][int(frame_2)][2] y1 = data[int(frame_1)][int(frame_2)][3] y2 = data[int(frame_1)][int(frame_2)][4] m_left = data[int(frame_1)][int(frame_2)][5] m_right= data[int(frame_1)][int(frame_2)][6] # Draws a central dotted line with slightly transparent lines1, = plt.plot([0, 0], [max_l, max_r], 'grey', alpha=0.25) lines2, = plt.plot([max_l, max_r], [0, 0], 'grey', alpha=0.25) # Draws a perimeter of a rectangle lines3, = plt.plot([(x1),(x1)], [(y1), (y2)],'red', linewidth=2.0) lines4, = plt.plot([(x2),(x2)], [(y1), (y2)],'red', linewidth=2.0) lines5, = plt.plot([(x1),(x2)],[(y1),(y1)],'red', linewidth=2.0) lines6, = plt.plot([(x1),(x2)],[(y2),(y2)],'red', linewidth=2.0) # Inserts special rectangles into the figure for i in np.arange(len(n)): pos = mpatches.Rectangle([max_l, pos_numbers[i]], pos_left[i] , 10, ec="none") patches.append(pos) pos = mpatches.Rectangle([pos_right[i], pos_numbers[i]],pos_right[i]),10 , ec="none") patches.append(pos) collection = PatchCollection(patches, alpha=0.25) ax.add_collection(collection) # I created two slider for frame_1 and frame_2 but I am struck with the PatchCollection. I do not know what to do with it def update(val): # update all the lines (1 to 7) # update special rectangles I even added ax.add_collection(collection) here. it did not help. plt.show() 中使用的内容中获取价值 - 我只是猜测在这里 - 您可能需要根据实际情况进行调整。

但最后,我认为像这样的代码应该做你要求的:

CASE

答案 1 :(得分:3)

首先,您从查询开始就错过了SELECT 然后你在" deleted.TrackingUrl"后失去了昏迷, 然后你不能在案例中使用UPDATE(或使用UDF) 和UPDATE OUTPUT需要INTO 和marc_c说"为什么你甚至使用动态SQL"

尝试这样的事情:

DECLARE @LastSeed INT
   DECLARE @FinalSeed INT
   SELECT TOP 1 
    @LastSeed = ISNULL(s.LastSeed,0)   ,
    @FinalSeed = ISNULL(s.FinalSeed,0)
    FROM TrackingSeed       s 
        JOIN Users          u   ON  s.UID=u.UID 
        JOIN SubUsers       su  ON  u.UID=su.UID 
        WHERE 
        s.CarrierCode=@_CarrierID 
        AND 
        su.SUID=@_SUID


    IF  @LastSeed+1=@FinalSeed
    BEGIN
            UPDATE t SET 
                    LastSeed=NewInitialSeed, 
                    FinalSeed=NewFinalSeed, 
                    NewInitialSeed=NULL, 
                    NewFinalSeed=NULL 
                OUTPUT 
                    deleted.LastSeed+1 AS CurrentSeed, 
                    deleted.Prefix, 
                    deleted.PostFix, 
                    deleted.CarrierName, 
                    deleted.TrackingUrl,
                    (FinalSeed - inserted.LastSeed) AS RemainingSeeds INTO TABLENAMEFOROUTPUT
                FROM TrackingSeed   t
                JOIN Users          u   ON  t.UID=u.UID
                JOIN SubUsers       su  ON  su.UID=u.UID
                WHERE 
                    su.SUID=@_SUID 
                AND t.CarrierCode=@_CarrierID 
    END
    ELSE
    BEGIN
                UPDATE t SET 
                    LastSeed=LastSeed+1 
                OUTPUT 
                    inserted.LastSeed+1 AS CurrentSeed, 
                    inserted.Prefix, Inserted.PostFix, 
                    inserted.CarrierName, 
                    inserted.TrackingUrl, 
                    (FinalSeed - inserted.LastSeed) AS RemainingSeeds INTO TABLENAMEFOROUTPUT
                FROM TrackingSeed t
                JOIN Users u ON t.UID=u.UID
                JOIN SubUsers su ON su.UID=u.UID
                WHERE 
                    su.SUID=@_SUID 
                AND t.CarrierCode=@_CarrierID 
    END