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. General Programming
  3. C#
  4. how to pass Comparison Operators as parameter to query in C#?

how to pass Comparison Operators as parameter to query in C#?

Scheduled Pinned Locked Moved C#
questioncsharpdatabasetutorial
5 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.
  • O Offline
    O Offline
    obarahmeh
    wrote on last edited by
    #1

    Dear All, I have a query that has some parameters: ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE) AND (T.VOLUME_TRADED >= @Volume) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= @Symbol) ORDER BY ActTime DESC ) I passed the three parameters (@SUBMIT_DATE, @Volume, @Symbol) successfully, but I want to read the operator (>= or <=) as a parameter...I have a combo box with >= and <=, and I want to pass the value of this combo into the query. How can I do this??? this is because the operator may be >= or <=, and I don't want to write two queries.

    Kind Regards OBarahmeh

    L 1 Reply Last reply
    0
    • O obarahmeh

      Dear All, I have a query that has some parameters: ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE) AND (T.VOLUME_TRADED >= @Volume) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= @Symbol) ORDER BY ActTime DESC ) I passed the three parameters (@SUBMIT_DATE, @Volume, @Symbol) successfully, but I want to read the operator (>= or <=) as a parameter...I have a combo box with >= and <=, and I want to pass the value of this combo into the query. How can I do this??? this is because the operator may be >= or <=, and I don't want to write two queries.

      Kind Regards OBarahmeh

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Add a parameter of type BIT, and set it to "0" when you want to perform a "<=" and set it to "1" whenever you want to perform a ">=". You can than add an IF-ELSE-ENDIF construction to differentiate between the two queries;

      IF @MyBIT = 0 THEN
      BEGIN
      SELECT [..]
      WHERE >=
      END
      ELSE
      BEGIN
      SELECT [..]
      WHERE <=
      END

      Enjoy :)

      I are troll :)

      O 1 Reply Last reply
      0
      • L Lost User

        Add a parameter of type BIT, and set it to "0" when you want to perform a "<=" and set it to "1" whenever you want to perform a ">=". You can than add an IF-ELSE-ENDIF construction to differentiate between the two queries;

        IF @MyBIT = 0 THEN
        BEGIN
        SELECT [..]
        WHERE >=
        END
        ELSE
        BEGIN
        SELECT [..]
        WHERE <=
        END

        Enjoy :)

        I are troll :)

        O Offline
        O Offline
        obarahmeh
        wrote on last edited by
        #3

        sorry for posting the same question in two places. I tried your solution as the following: declare @operator bit; set @operator = 1; ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = '20090405') AND (CASE WHEN @operator = 1 THEN T.VOLUME_TRADED >= 500 ELSE T.VOLUME_TRADED <= 500 END) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= 'BOP') ORDER BY ActTime DESC ) but this did not work , it gives me an error : Msg 102, Level 15, State 1, Line 10 Incorrect syntax near '>'.

        Kind Regards OBarahmeh

        L 1 Reply Last reply
        0
        • O obarahmeh

          sorry for posting the same question in two places. I tried your solution as the following: declare @operator bit; set @operator = 1; ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = '20090405') AND (CASE WHEN @operator = 1 THEN T.VOLUME_TRADED >= 500 ELSE T.VOLUME_TRADED <= 500 END) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= 'BOP') ORDER BY ActTime DESC ) but this did not work , it gives me an error : Msg 102, Level 15, State 1, Line 10 Incorrect syntax near '>'.

          Kind Regards OBarahmeh

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          My apologies, I didn't mean to refer to a CASE statement. What you're looking for is a simpeler IF statement;

          DECLARE @operator BIT;
          SET @operator = 1;

          IF @operator = 1 THEN
          BEGIN
          SELECT DISTINCT
          T.TICKET_NUMBER AS TicketNum,
          A.DESCRIPTION AS Action,
          O.SYMBOL_CODE AS Symbol,
          T.TRADE_PRICE AS Price,
          T.VOLUME_TRADED AS FillVol,
          T.EXTENDED_PRICE AS TotalValue,
          T.SUBMITTED_TIME AS ActTime,
          T.SUBMITTED_DATE AS TransDate
          FROM TSDETL AS T
          INNER JOIN TSORDR
          AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE
          AND O.TICKET_NUMBER = T.TICKET_NUMBER
          INNER JOIN TSORDA
          AS A ON O.ORDER_ACTION = A.ACTION_CODE
          WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE)
          AND (T.VOLUME_TRADED >= @Volume)
          AND (T.TICKET_NUMBER IS NOT NULL)
          AND (O.SYMBOL_CODE= @Symbol)
          ORDER BY ActTime DESC
          END
          ELSE
          BEGIN
          SELECT DISTINCT
          T.TICKET_NUMBER AS TicketNum,
          A.DESCRIPTION AS Action,
          O.SYMBOL_CODE AS Symbol,
          T.TRADE_PRICE AS Price,
          T.VOLUME_TRADED AS FillVol,
          T.EXTENDED_PRICE AS TotalValue,
          T.SUBMITTED_TIME AS ActTime,
          T.SUBMITTED_DATE AS TransDate
          FROM TSDETL AS T
          INNER JOIN TSORDR
          AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE
          AND O.TICKET_NUMBER = T.TICKET_NUMBER
          INNER JOIN TSORDA
          AS A ON O.ORDER_ACTION = A.ACTION_CODE
          WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE)
          AND (T.VOLUME_TRADED <= @Volume)
          AND (T.TICKET_NUMBER IS NOT NULL)
          AND (O.SYMBOL_CODE= @Symbol)
          ORDER BY ActTime DESC
          END

          You can make this stored procedure even simpeler by splitting the query into; * a view that does the selection of the fields and the joins * put the filter- and order-statements in a select-query that you run against the aforementioned view :) I hope this helps :thumbsup:

          I are troll :)

          O 1 Reply Last reply
          0
          • L Lost User

            My apologies, I didn't mean to refer to a CASE statement. What you're looking for is a simpeler IF statement;

            DECLARE @operator BIT;
            SET @operator = 1;

            IF @operator = 1 THEN
            BEGIN
            SELECT DISTINCT
            T.TICKET_NUMBER AS TicketNum,
            A.DESCRIPTION AS Action,
            O.SYMBOL_CODE AS Symbol,
            T.TRADE_PRICE AS Price,
            T.VOLUME_TRADED AS FillVol,
            T.EXTENDED_PRICE AS TotalValue,
            T.SUBMITTED_TIME AS ActTime,
            T.SUBMITTED_DATE AS TransDate
            FROM TSDETL AS T
            INNER JOIN TSORDR
            AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE
            AND O.TICKET_NUMBER = T.TICKET_NUMBER
            INNER JOIN TSORDA
            AS A ON O.ORDER_ACTION = A.ACTION_CODE
            WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE)
            AND (T.VOLUME_TRADED >= @Volume)
            AND (T.TICKET_NUMBER IS NOT NULL)
            AND (O.SYMBOL_CODE= @Symbol)
            ORDER BY ActTime DESC
            END
            ELSE
            BEGIN
            SELECT DISTINCT
            T.TICKET_NUMBER AS TicketNum,
            A.DESCRIPTION AS Action,
            O.SYMBOL_CODE AS Symbol,
            T.TRADE_PRICE AS Price,
            T.VOLUME_TRADED AS FillVol,
            T.EXTENDED_PRICE AS TotalValue,
            T.SUBMITTED_TIME AS ActTime,
            T.SUBMITTED_DATE AS TransDate
            FROM TSDETL AS T
            INNER JOIN TSORDR
            AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE
            AND O.TICKET_NUMBER = T.TICKET_NUMBER
            INNER JOIN TSORDA
            AS A ON O.ORDER_ACTION = A.ACTION_CODE
            WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE)
            AND (T.VOLUME_TRADED <= @Volume)
            AND (T.TICKET_NUMBER IS NOT NULL)
            AND (O.SYMBOL_CODE= @Symbol)
            ORDER BY ActTime DESC
            END

            You can make this stored procedure even simpeler by splitting the query into; * a view that does the selection of the fields and the joins * put the filter- and order-statements in a select-query that you run against the aforementioned view :) I hope this helps :thumbsup:

            I are troll :)

            O Offline
            O Offline
            obarahmeh
            wrote on last edited by
            #5

            Thanks a lot man...sometimes we think that most cases can not be solved simply, but always I see that there is a simple solution. :zzz: :wtf: :~:rose::rose::rose:

            Kind Regards OBarahmeh

            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