Pass Parameter

  • I am trying to pass a parameter to my report.

    This parameter is a calculated one , so needs to be int for calculation purposes, but I may also have to pass "*" which is a varchar.

    My current query has the following.

    Select

    (Cast(DeviceType as Int)-Cast(DeviceTypezone as Int)= @Device or @Device Is Null or @Device = "*")

    Can anyone advice?

  • I don't see how the * value behaves differently from NULL; it seems extraneous. Either a calculated value equals @Device, or there was no numeric @Device value entered.

    (Cast(DeviceType as Int)-Cast(DeviceTypezone as Int)= @Device or @Device Is Null or @Device = "*")

    The above is functionally no different from

    (Cast(DeviceType as Int)-Cast(DeviceTypezone as Int)= @Device or @Device Is Null)

    If users insist on passing *, you could maybe try adding something at the start of your query like

    IF @Device = '*'

    SET @Device = NULL

    And just use the shorter form of the select without the (or @Device = "*") clause.

  • You can also just use:

    NULLIF(@device, '*') IS NULL


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply