Create query dynamically based on parameter values

(2 posts) (2 voices)
  1. rkmourya, Member

    Can we build dynamic queries based on the values passed in the parameters.

    Example:
    I want pass in 3 parameters to a stored procedure.
    1) Start Date
    2) End Date
    3) Field Name (created_date, order_date, delivered_date)

    Based on the values passed in the parameters, I want to build the WHERE clause of my query.

    So if Field Name value is created_date, I want WHERE clause to be
    [created_date >= 'Start Date' AND created_date <= 'End Date']

    if Field Name value is order_date, I want WHERE clause to be
    [order_date >= 'Start Date' AND order_date <= 'End Date']

  2. myDBR Team, Key Master

    Hi,
    you can use PREPARE statement to create dynamic SQL statement or just use conditions:

    select ...
    from mydata
    where
    (inField = 'create' and created_date between in_start_date and in_end_date) or
    (inField = 'order' and order_date between in_start_date and in_end_date) or
    (inField = 'deliver' and delivered_date between in_start_date and in_end_date)

    --
    myDBR Team


Reply

You must log in to post.