Results 1 to 4 of 4

Thread: [RESOLVED] Case statements problem in SP

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Case statements problem in SP

    I get an incorrect syntax error in this SP

    Incorrect syntax near key word case.

    I think I may need to parenthasize somewhere but cant figure it out and my deadline is approaching fast, any help please.

    VB Code:
    1. ALTER PROCEDURE dbo.sp_GetAvailableStockInLocationByType
    2.  
    3.     (
    4.         @Location integer,
    5.         @PK_StockTypeID integer
    6.     )
    7.  
    8. AS
    9.     /* SET NOCOUNT ON */
    10.    
    11.     select case @PK_StockTypeID
    12.     case  1
    13.      SELECT     StockItems.PK_StockItemID, CarriageStock.StockSerialNumber
    14.            FROM         CarriageStock INNER JOIN
    15.                                  StockItems ON CarriageStock.FK_StockItemID = StockItems.PK_StockItemID AND CarriageStock.FK_StockTypeID = StockItems.FK_StockTypeID
    16.            WHERE     (CarriageStock.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_CurrentSiteID = @Location)
    17.    
    18.     case  2
    19.     SELECT     StockItems.PK_StockItemID, RailStock.StockSerialNumber
    20.     FROM         RailStock INNER JOIN
    21.                           StockItems ON RailStock.FK_StockItemID = StockItems.PK_StockItemID AND RailStock.FK_StockTypeID = StockItems.FK_StockTypeID
    22.     WHERE     (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_CurrentSiteID = @Location)
    23.  
    24.     case  3
    25.     SELECT    StockItems.PK_StockItemID, PlatformStock.StockSerialNumber
    26.     FROM         PlatformStock INNER JOIN
    27.                           StockItems ON PlatformStock.FK_StockItemID = StockItems.PK_StockItemID AND PlatformStock.FK_StockTypeID = StockItems.FK_StockTypeID
    28.     WHERE     (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_CurrentSiteID = @Location)
    29.  
    30.     end
    31.  
    32.     RETURN

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Case statements problem in SP

    Code:
    Select Case @XYZ When 1 Then 'This'
           When 2 Then 'That'
           Else 'Nothing' End
    
    ...or...
    
    Select Case When @XYZ=1 Then 'This'
               Case When @XYZ=2 Then 'That'
                Else 'Nothing' End
    Both syntax work...

    The second syntax can have AND and OR's in each CASE so it's more powerful.

    The ELSE is always good to have - otherwise it can return a NULL!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Case statements problem in SP

    changed
    VB Code:
    1. ALTER PROCEDURE dbo.sp_GetAvailableStockInLocationByType
    2.  
    3.     (
    4.         @Location integer,
    5.         @PK_StockTypeID integer
    6.     )
    7.  
    8. AS
    9.     /* SET NOCOUNT ON */
    10.    
    11.     if @PK_StockTypeID = 1
    12.     begin
    13.    
    14.      SELECT     StockItems.PK_StockItemID, CarriageStock.StockSerialNumber
    15.            FROM         CarriageStock INNER JOIN
    16.                                  StockItems ON CarriageStock.FK_StockItemID = StockItems.PK_StockItemID AND CarriageStock.FK_StockTypeID = StockItems.FK_StockTypeID
    17.            WHERE     (CarriageStock.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_CurrentSiteID = @Location)
    18.     end
    19.     if @PK_StockTypeID = 2
    20.     begin
    21.    
    22.     SELECT     StockItems.PK_StockItemID, RailStock.StockSerialNumber
    23.     FROM         RailStock INNER JOIN
    24.                           StockItems ON RailStock.FK_StockItemID = StockItems.PK_StockItemID AND RailStock.FK_StockTypeID = StockItems.FK_StockTypeID
    25.     WHERE     (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_CurrentSiteID = @Location)
    26.  
    27.     end
    28.     if @PK_StockTypeID = 3
    29.     begin
    30.    
    31.     SELECT    StockItems.PK_StockItemID, PlatformStock.StockSerialNumber
    32.     FROM         PlatformStock INNER JOIN
    33.                           StockItems ON PlatformStock.FK_StockItemID = StockItems.PK_StockItemID AND PlatformStock.FK_StockTypeID = StockItems.FK_StockTypeID
    34.     WHERE     (StockItems.FK_ShippingStatus = 3) AND (StockItems.FK_StockTypeID = @PK_StockTypeID) AND (StockItems.FK_CurrentSiteID = @Location)
    35.  
    36.     end
    37.  
    38.     RETURN

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Case statements problem in SP

    and yes - it cannot be used like an IF - it's for being buried in a SELECT statement...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width