Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Handling a SELECT in a stored procedure

Handling a SELECT in a stored procedure

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stormydaniels
    wrote on last edited by
    #1

    Hi, I have a stored proc which retrieves a value from my database. If I select the value I then want to do a case statement and return a string. This is my code but I can't get it to work:

    create procedure procGetEventType
    (@EventID int, @EventHeading varchar(12))
    as
    declare@desc varchar(50)
    select @desc = 'Unknown event type'

    begin
    select [EventID], [EventHeading]
    from tblEvents
    where [EventID] = @EventID

    if ([EventHeading] = 'Info') desc = 'Information'
    if ([EventHeading] = 'Warn') desc = 'Warning'
    if ([EventHeading] = 'Error') desc = 'Error'

    return @desc
    end

    I can't even create the stored procedure because I get a syntax error. It complains about EventHeading being an invalid column heading and when I comment out the "if" lines it's OK. If I have done a select on a column can I not then refer to it later on? Any ideas? :confused:

    W 1 Reply Last reply
    0
    • S stormydaniels

      Hi, I have a stored proc which retrieves a value from my database. If I select the value I then want to do a case statement and return a string. This is my code but I can't get it to work:

      create procedure procGetEventType
      (@EventID int, @EventHeading varchar(12))
      as
      declare@desc varchar(50)
      select @desc = 'Unknown event type'

      begin
      select [EventID], [EventHeading]
      from tblEvents
      where [EventID] = @EventID

      if ([EventHeading] = 'Info') desc = 'Information'
      if ([EventHeading] = 'Warn') desc = 'Warning'
      if ([EventHeading] = 'Error') desc = 'Error'

      return @desc
      end

      I can't even create the stored procedure because I get a syntax error. It complains about EventHeading being an invalid column heading and when I comment out the "if" lines it's OK. If I have done a select on a column can I not then refer to it later on? Any ideas? :confused:

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Few issues: - define variables where you place the result from SELECT - don't use parenthesis on parameters if creating a procedure but do use them if creating a function - procedure cannot return a value, use output parameter or create a function. Most likely you want to use a function for the logic you described. In that case it would be something like:

      create function GetEventType (@EventID int)
      returns varchar(12)
      AS
      declare @desc varchar(50)
      declare @eventHeading varchar(12)
      begin
      select @eventHeading = [EventHeading]
      from tblEvents
      where [EventID] = @EventID

      set @desc = 'Unknown event type'
      if (@eventHeading = 'Info') set @desc = 'Information'
      if (@eventHeading = 'Warn') set @desc = 'Warning'
      if (@eventHeading = 'Error') set @desc = 'Error'

      return (@desc)
      end

      The need to optimize rises from a bad design. My articles[^]

      S 1 Reply Last reply
      0
      • W Wendelius

        Few issues: - define variables where you place the result from SELECT - don't use parenthesis on parameters if creating a procedure but do use them if creating a function - procedure cannot return a value, use output parameter or create a function. Most likely you want to use a function for the logic you described. In that case it would be something like:

        create function GetEventType (@EventID int)
        returns varchar(12)
        AS
        declare @desc varchar(50)
        declare @eventHeading varchar(12)
        begin
        select @eventHeading = [EventHeading]
        from tblEvents
        where [EventID] = @EventID

        set @desc = 'Unknown event type'
        if (@eventHeading = 'Info') set @desc = 'Information'
        if (@eventHeading = 'Warn') set @desc = 'Warning'
        if (@eventHeading = 'Error') set @desc = 'Error'

        return (@desc)
        end

        The need to optimize rises from a bad design. My articles[^]

        S Offline
        S Offline
        stormydaniels
        wrote on last edited by
        #3

        Thanks for such a carefully created answer - that's solved my problem perfectly and more importantly, helped me understand the issue I had. Thanks again :cool:

        W 1 Reply Last reply
        0
        • S stormydaniels

          Thanks for such a carefully created answer - that's solved my problem perfectly and more importantly, helped me understand the issue I had. Thanks again :cool:

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          You're welcome :)

          The need to optimize rises from a bad design. My articles[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups