• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL Help with a simple Stored Procedure

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL Help with a simple Stored Procedure

    This should be basic – but it driving me nuts on how to do it so any help would be much appreciated.

    I have a column that contains [eventType] as follows

    HAIL
    TORNADO
    FUNNEL
    DOWNBURST

    I want to pass to the Proc parameters to filter on [eventType] depending on weather check boxes on the web page are selected.

    EXEC MyProc @_hail =1, @_tornado =1, @_ funnel=0, @ _downburt=0

    MyProc
    @_hail BIT,
    @_tornado BIT,
    @_ funnel BIT,
    @ _downburt BIT

    SELECT * from eventsTable
    WHERE [eventType] = HAIL
    OR [eventType] = TORNADO

    I am not sure how to do the dynamic WHERE clause.
    www.stormtrack.co.uk - My Stormchasing website.

    #2
    Rob, yours looks wrong , ANDs and ORs are mixed up
    needs changing to this:

    PHP Code:
    SELECT
    *
    FROM
    eventsTable
    WHERE
    (
    @
    _hail 1
    and
    [
    eventType] = 'HAIL'
    )
    or
    (
    @
    _tornado 1
    and
    [
    eventType] = 'TORNADO'
    )
    etc... 
    heres another way with less boolean:

    PHP Code:
    SELECT from eventsTable
    WHERE 
    [eventType] = case when @_hail =1 then 'HAIL' else 'NO MATCH' end
    or [eventType] = case when @_tornado =1 then 'TORNADO' else 'NO MATCH' end
    or [eventType] = case when @_funnel =1 then 'FUNNEL' else 'NO MATCH' end
    or [eventType] = case when @_downburt =1 then 'DOWNBURT' else 'NO MATCH' end 
    Last edited by Spacecadet; 8 May 2007, 12:22.
    Coffee's for closers

    Comment


      #3
      Or use UNION ALL to join simple selects:

      PHP Code:
        SELECT 
      FROM eventsTable 
      WHERE 
      @_hail and [eventType] = 'HAIL' 
      UNION ALL 
      SELECT 

      FROM eventsTable 
      WHERE 
      @_tornado and [eventType] = 'TORNADO'
      UNION ALL 
      SELECT 

      FROM eventsTable 
      WHERE 
      @_funnel and [eventType] = 'FUNNEL' 
      UNION ALL 
      SELECT 

      FROM eventsTable 
      WHERE 
      @_downburst and [eventType] = 'DOWNBURST' 
      HTH

      Comment


        #4
        Tsk, 4 selects when 1 will do
        Coffee's for closers

        Comment


          #5
          Originally posted by Spacecadet
          Tsk, 4 selects when 1 will do
          I get paid by the hour!

          Comment


            #6
            A BIG public THANK YOU is for all who have replied is required here Power to UKcontractor indeed

            I know that we are all contractors who charge by the hour - but this is a personal private project that I working on and SQL is not really my area of expertise. So I appreciate the time and effort that people have given to help me out.

            Often thanks is not given when help is required – but I am not that sort of person – so again many thanks for helping me out.

            PS if you ever find yourselves in the Village of Rothey, Leicestershire – the beers ore on me down at the “Woodies”
            www.stormtrack.co.uk - My Stormchasing website.

            Comment


              #7
              Originally posted by Spacecadet
              Tsk, 4 selects when 1 will do
              Guessing at the execution plan that would come out for both SQL Statements, I'd hazard a guess that the first would probably suit a smaller table, record-wise, whilst the second would perform better with a large table, especially if that column had an index against it!!! The first statement would not make best use of an index on eventtype as its nested within a case statement.

              Likewise, shame on both of you for using SELECT *. Thats lazy. Fieldnames please!!!!

              Comment


                #8
                Originally posted by Weltchy
                Likewise, shame on both of you for using SELECT *. Thats lazy. Fieldnames please!!!!
                It's a very bad practice too - in case of changes to table bugs may appear in code and re-compilation won't find them because you were doing select * rather than select specific columns.

                Comment


                  #9
                  Originally posted by AtW
                  It's a ver..blah zzzzzzzzzzzzzzzz
                  get back to general

                  couldn't list the column names as they were not provided in the spec
                  Coffee's for closers

                  Comment


                    #10
                    It is a very bad practice to have such "spec" - if you don't know what columns you going to have, what's good for you to select *?

                    Back to exile now!

                    Comment

                    Working...
                    X