CFIF inside a SQL statment

I have a search page that has 3 possible entries. Service,
City, & Keywords.
Service and City are list/menus and cannot be null, but
Keywords is a text
box and can be null.
I need to write a SQL statement to take the 3 possible
parameters and test
them against the database. Since the first 2 are never null,
I can write
them right inside the SQL string, but how can I only check
for the keywords
if there is something inside the box?
Can I use a <cfif> statement inside the sql string to
only check the 3rd
parameter if it is not NULL?
I wrote this:
Select company, name, phone, city, state, username FROM
members WHERE
city = Form.City AND state = Form.state AND keywords LIKE
Form.keywords
ORDER BY company ASC
Also, since I am passing it from a search page to a results
page, would it
be URL.City verses FORM.City, etc.
Also would like to do this in a component and pass the 3
variables as
arguments with the city and service being required and the
keywords not.
However, I have no idea on what the procedure is for this.
I wrote:
<cfcomponent>
<cffunction name="ServiceSearch" access="remote"
returntype="query">
<cfargument name="service" type="numeric"
required="true">
<cfargument name="city" type="numeric"
required="true">
<cfargument name="keywords" type="string"
required="false">
Select company, name, phone, city, state, username FROM
members WHERE
city = Form.City AND state = Form.state AND keywords LIKE
Form.keywords
ORDER BY company ASC
<cfreturn ServiceResults>
</cffunction>
</cfcomponent>
Do I invoke the component from the results page?
Well, I need to solve the first issue before I attempt the
second. Thanks!
Wally Kolcz
Developer / Support
ProjectProofing.com

Yes you can put <cfif ...> statements inside a
<cfquery...> tag to
modify the SQL string.
WHERE this = that AND
<cfif len(trim(form.something))>
AnotherThing = '#form.something#'
</cfif>
Is valid.
Whether the form variables on the action page are URL or FORM
depends on
the method of your <form ...> tag. method="post"
creates form variables
on the action page, method="get" creates URL variables on the
action page.
You write <cfargument ...> tags for each argument you
want to pass into
a function.
<cfargument name="city" required="true" ...>
OR
<cfargument name="foobar" required="false" default=""
...>
You then use these in the function with the arguments scope.
#arguments.city# and #arguments.foobar#
You then pass these into the function from your calling page.
There are
numerous ways this can be done, here are a few.
<cfinvoke .... city="Sacramento" foobar="george"/>
<finvoke ... >
<cfinvokeArgument name="city" value="Sacramento">
<cfinvokeArgument name="foobar" value="george">
</cfinvoke>
<cfset myComponent =
createObject("component","my.component.path.andName)>
...myComponent.aFunction("Sacramento","george")...
...myComponent.aFunction(city:"Sacrament",foobar:"george")...o
Wally Kolcz wrote:
> I have a search page that has 3 possible entries.
Service, City, & Keywords.
>
> Service and City are list/menus and cannot be null, but
Keywords is a text
> box and can be null.
>
> I need to write a SQL statement to take the 3 possible
parameters and test
> them against the database. Since the first 2 are never
null, I can write
> them right inside the SQL string, but how can I only
check for the keywords
> if there is something inside the box?
>
> Can I use a <cfif> statement inside the sql string
to only check the 3rd
> parameter if it is not NULL?
>
> I wrote this:
>
> Select company, name, phone, city, state, username FROM
members WHERE
> city = Form.City AND state = Form.state AND keywords
LIKE Form.keywords
> ORDER BY company ASC
>
> Also, since I am passing it from a search page to a
results page, would it
> be URL.City verses FORM.City, etc.
>
> Also would like to do this in a component and pass the 3
variables as
> arguments with the city and service being required and
the keywords not.
> However, I have no idea on what the procedure is for
this.
>
> I wrote:
> <cfcomponent>
> <cffunction name="ServiceSearch" access="remote"
returntype="query">
> <cfargument name="service" type="numeric"
required="true">
> <cfargument name="city" type="numeric"
required="true">
> <cfargument name="keywords" type="string"
required="false">
> Select company, name, phone, city, state, username FROM
members WHERE
> city = Form.City AND state = Form.state AND keywords
LIKE Form.keywords
> ORDER BY company ASC
> <cfreturn ServiceResults>
> </cffunction>
> </cfcomponent>
>
> Do I invoke the component from the results page?
>
> Well, I need to solve the first issue before I attempt
the second. Thanks!
>

Similar Messages

  • Need help in SQl statment

    this SQL statment work in oracle database :
    SELECT TO_DATE('20080627','YYYYMMDD') - TO_DATE('20080526','YYYYMMDD') FROM dual
    But didn'twork on Timesten Database and this error is appear
    2789: An interval data type must be specified for a datetime arithmetic result
    The command failed.

    SELECT extract (day from TO_DATE('20080627','YYYYMMDD') - TO_DATE('20080626','YYYYMMDD')) FROM dual;
    See
    http://download.oracle.com/otn_hosted_doc/timesten/703/TimesTenDocumentation/wwhelp/wwhimpl/js/html/wwhelp.htm?&accessible=true
    for details.

  • Problem with DBMS_OUTPUT and SQL statment with a function

    I am using SQL Developer version 3.0.04 and I have a function that has a dbms_output statment in it. I am running the following SQL to select the output of that statment.
    select A.A_FUCTION('TEST') from dual;
    The output works fine but the dbms_output line does not show in the dbms_output window. I turned on the output window etc. and i can get the select to print out the output if i wrap it in a declare begin end wrapper... and then change the query to insert the return value into variable.
    just wondering if there is way to get the dbms_output to flush out with just an SQL statment instead of in a begin end wrapper etc.

    just wondering if there is way to get the dbms_output to flush out with just an SQL statment instead of in a begin end wrapper etc.works fine in sql*plus, so I guess it must be a preference or sth. in sql*developer:
    SQL> set serverout on
    SQL> create or replace function f1 (r int)
      2    return int
      3  as
      4  begin
      5    dbms_output.put_line ('Hello World ' || r);
      6    return r;
      7  end f1;
      8  /
    Function created.
    SQL>
    SQL> select f1 (rownum) f1 from dual connect by level <= 3
      2  /
            F1
             1
             2
             3
    3 rows selected.
    Hello World 1
    Hello World 2
    Hello World 3
    SQL>

  • I cant get Fourth Elephant (Insider for SQL Developer)

    hi,guys
    please, help me.
    I want to user insider for sql developer of fourth elephant.
    but I cant access
    http://fourthelephant.com/sqldeveloper/download/
    who used it?
    thanks.

    yes, I see
    I want to know someone downloaded it.
    can anyone share it?

  • How to find top 10  SQL statments which are consuming more cpu time.

    hi all,
    Is there any command or script to monitor the top 10 sql statments which are consuming more cpu time.
    I know by using AWR REPORT we can find it, i want the command or script to find the top cpu utilization sql statments.
    Regards
    Subhash.

    Subhash,
    A quick and dirty Google search could have get you started with the following:
    Thread: how to get top CPU consuming sql oracle 10g
    Re: how to get top CPU consuming sql oracle 10g
    Oracle SQL top sessions
    http://www.dba-oracle.com/oracle10g_tuning/t_sql_top_sessions.htm
    "How to Find top 10 expensive sql's", version 9.2.0
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:73325450402303
    HTH,
    Thierry

  • Newbee Question to SQL Statment ..

    Hello,
    what does the statment PRODWH refer to in the statment below ....?
    what s the function of {?PRODWH: F_INVOICES.PROD_ID}. Is it an Input Parameter for the user ?
    ist the statment "external Join" replaceble trough Right/left Join ..?
    SQL Statment-------------------
    PRODWH
    SELECT
    "D_PROD_DESC"."PRODUCT_DESC_SHORT",
    "D_PROD"."SKU", "D_PRODUCT"."BU_DEPARTMENT",
    "D_PROD"."PROD_ID"
    FROM
    "D_PROD_DESC" "D_PROD_DESC" INNER JOIN
    "D_PROD" "D_PROD" ON
    "D_PROD_DESC"."PROD_ID"="D_PROD"."PROD_ID"
    EXTERNAL JOIN D_PROD.BU_DEPARTMENT={?PRODWH: Command.BU_DEPARTMENT} AND
    D_PROD.PROD_ID={?PRODWH: F_INVOICES.PROD_ID} AND
    D_PROD.SKU={?PRODWH: D_OLD_CUST.BU_SKU}
    -----END----------------

    The Database is Oracle but the SQL statment is generated by Crystal Reports XI

  • Can i use a environment variable inside a *.sql file?

    Hello,
    I want to create a external table.
    So i am using the command
    create or replace directory abc as 'C:\folder'.... inside a sql file.
    Now i want the path "C:\folder" to be dynamic as i am using this path in many other places also inside the sql file.So i thought to create a environment variable and put this value there.I tried using as %PATH% but it gives error..... where %PATH%=C:\folder.
    Can i use a environment variable inside a *.sql file?
    But how to do that or is there any other way.
    Thanks
    Swapna
    Edited by: user11018268 on Feb 19, 2010 1:03 AM

    user11018268 wrote:
    Actually what i want is the path "C:\folder" is not fixed it can be anything which i may not know the user may decide it later. Not supported. A directory object refers to a specific physical location (directory/folder) on a file system. Not a path.
    You can work around it by (creating and) using a function (running under a super user schema with authid definer privs). The caller (e.g. schema scott ) calls it with a physical path. E.g. GetDirectoryObject( 'C:\folder\2010\feb\week4' ).
    This function determines if there is an existing directory object for the path. If not, it uses a wildcard search to determine if there are any directory objects for parents in the path (e.g. for C:\folder\2010\feb or C:\folder\2010 or C:\folder ).
    If it finds a directory object, it interrogates the data dictionary to determine if the caller, schema scott for example, has read/write access on that directory object. If it has, it creates a new directory object and grants the caller read/write access to it. The function then returns the name of the directory object to the caller.
    The caller thus do not deal directly with directory objects. The function returns the object name given a physical path as input. Also, only a single base directory needs to be created (e.g. for C:\folder ) and access granted to the schema on it. Any sub-directory in that base directory can now be dynamically accessed by the schema.

  • Select statements inside a SQL Expression field

    Hello
    Why cannot we use the Select statements inside a SQL expression field in Crystal Reports ?
    Any alternatives for this...
    SELECT Top(1) "SomePrivateDatabase"."SPDname" FROM "SomePrivateDatabase"
    inner join "Subscriber" on
    "Subscriber"."SPDID" = "SomePrivateDatabase"."SPDID"
    inner join "UserDetails" on
    "UserDetails"."SPDID" = "SubscriberCore"."SPDID"
    Regards
    Srivatsa
    Edited by: Srivatsa Haridas on Dec 3, 2008 12:17 PM

    Hi Srivatsa,
    SQL Expressions is something the Database supports. We query the Client engine for a list of functions they make available. If the SQL Statement is not list it's not an option and therefore it won't work.
    Your work arounds are to use either a Stored Procedure, this is the most efficient way, all processing is done server side and CR simply waits for the data to format in the report. Another option is to use a
    Command Object, use one CO per report so try to get all data within the SQL statement. We simply pass the SQL to the server and wait for the result set, if you get an error it will be the SQL you typed in. Test in a SQL test tool first to confirm you are getting the data correctly.
    For more assistance in designing reports please post your report deisgn questions to the Reprot Design forums. I am moving this one to that queue.
    Thank you
    Don

  • SQL Statment Help

    hiya
    i need help about SQL statment i want get all employee Data if i know the emp_no get specify recored if emp_no = null get all Data
    select * from employe where emp_no=123 if emp_no=null get all records
    thx

    Hi,
    Welcome to the forum!
    Ttushar's solution is what you requested.
    If empno can not be NULL, then you can also modify it like this:
    SELECT  *
    FROM     employee
    WHERE     emp_no = NVL (:v_emp_no, emp_no);

  • Autocommit inside one SQL command

    Hi,
    I have 2 tables A and B each have two columns id and name. I used "update A set A.name = (Select B.name from B where A.id=B.id)" to update table A's name. However, when both tables are very large( say 5M rows), Since Oracle will commit only after a command, so, the DB will hold the 5M rows in the rollback segments, if the rollback segments are not large enough, the updating will be very slow. Is there any way to let Oracle commit inside one SQL command? For exemple, auto commit after every 10000 rows get updated.
    Thanks.

    Thanks for your comments. We have done some tests:
    1. We used a flag to limit the number of rows for updating in 2000, 5000, 10000 and 50000 rows. We found that the time/rows in 2000 rows was much lower than in 50000 rows. The higher the testing rows, the higher time/rows was.
    2. We used single SQL (update ... select .. from) command to update whole table vs used one select SQL and a seperate update SQL to update table A one record by one record using Java. I thought first one should be much fast than second one. But there were not big diferrence.
    Do you have any ideal for tuning this DML?
    Thanks.

  • Viewing table relations inside Oracle SQL Developer

    Hi,
    I'm a PL/SQL beginner and I need to control the table relations for an Oracle database. I'm using the Oracle SQL developer tool.
    Now, how can I see the table relation inside Oracle SQL developer tool?
    Thanks

    itshak wrote:
    When working with tables based on user defined types, the FK relations between the tables are not displayed at the data modeler (and also at the worksheet query builder)
    I don't think that applies here 1041141 mentioned he is a beginner and did not mention any usage of object types.
    If you want to report an issue or possible bug, then please open an extra thread for that. In such a case it helps tremendously if you provide a working test case as well.

  • Embeded SQL Statment VS Store procedure

    On J2ee tecknology, what is the better method of usind data services.
    raping all sql statment in the store procedure or embeded all sql statment in EJB.
    thanks
    null

    Hi,
    This issue looks similar to the the issue described in:
    Re: Exception in reader for ODP.NET 11.1.0.6.2 same work fine in older version
    Note that Bug 9792727 was filed to track the issue described in the above link.
    Regards,
    -Naveen

  • Optimization of CURSORS/SQL inside PL/SQL

    Hi Friends
    I have a query regarding optimization working with CURSOR FOR loops and embedded SQL stmts inside PL/SQL procedure.
    I have a code where i am using a Cursor holding multiple rows and than LOOP over it to process individual rows. I am using nested Cursor FOR Loops too.
    I want to know what optimizations are possible in this kind of scenario, where i ahve to process each row of a Cursor.
    Kindly Guide....
    Thanks and Regards

    Hello Friends.....
    Thanks so very much for your reply!!
    I am attaching my code for your reference. Will also go through the links u have provided.
    Kindly, suggest what can be done in this kind of scenario:
    CREATE OR REPLACE procedure insert_sfdc_account
    as
    --DECLARE
    CURSOR C1 IS
    SELECT customer_code, name1, name2, name3, name4, phone_number, fax, web_address, industry_sector, customer_profile, customer_type,
    address, city, postal_code, country_key, zzcust_type, vat_code
    FROM load_cust_general
    WHERE account_group = 'ZSIT';
    v_cust_cur c1%ROWTYPE;
    -- type sales_tab is table of load_cust_sales_area%rowtype;
    v_sales_area load_cust_sales_area%ROWTYPE;
    -- v_sales_area sales_tab;
         v_salesorg varchar2(10);
         v_sales_district varchar2(10);
         v_salesoff varchar2(10);
         v_custgrp varchar2(10);
         v_salesgrp varchar2(10);
    v_type varchar2(20);
    v_nature varchar2(10);
    v_partner_code varchar2(10);
    v_parent_cust varchar2(20);
    v_credit_blk varchar2(20);
    BEGIN
    open c1;
    loop
    fetch c1 into v_cust_cur;
    exit when c1%NOTFOUND;
    for i in (SELECT customer_code, salesorg from load_cust_partner
    where customer_code = v_cust_cur.customer_code ) LOOP
    dbms_output.put_line(v_cust_cur.customer_code );
    -- BEGIN
                        SELECT partner_code into v_partner_code from load_cust_partner
    where customer_code = i.customer_code and salesorg = i.salesorg and partner_function = 'Z1';
    dbms_output.put_line(v_partner_code||i.customer_code);
    SELECT salesorg, sales_district, salesoff, salesgrp, custgrp INTO v_salesorg, v_sales_district, v_salesoff, v_salesgrp, v_custgrp FROM load_cust_sales_area
              WHERE customer_code = i.customer_code and salesorg = i.salesorg;
                   dbms_output.put_line(v_salesorg||i.salesorg);
                   dbms_output.put_line('Customer_Code : '|| i.customer_code);
                   dbms_output.put_line('SalesOrg : '|| i.salesorg);
         /*     SELECT parent_customer INTO v_parent_cust
    from load_cust_hierarchy
    WHERE customer_code = '1234'
    and salesorg = 'abc'
    and hierarchy_type = 'G'; */
                        SELECT parent_customer
    INTO v_parent_cust
    FROM load_cust_hierarchy
    WHERE lower(customer_code) = lower(trim(i.customer_code))
    AND lower(salesorg) = lower(trim(i.salesorg))
    AND hierarchy_type = 'G';
                   /*     SELECT parent_customer INTO v_parent_cust from load_cust_hierarchy
    WHERE customer_code = i.customer_code and salesorg = i.salesorg and hierarchy_type = 'G';
    dbms_output.put_line(v_parent_cust);
                             dbms_output.put_line('Successfully Executed SQL st. Error is somewhere else');
    exception
    WHEN NO_DATA_FOUND THEN
    v_parent_cust := 'ACHINSUMAN';*/
         --          END;
                        SELECT credit_block INTO v_credit_blk from load_cust_company_cod
              WHERE customer_code = i.customer_code;
    dbms_output.put_line(v_credit_blk);
    for j in (SELECT account_group, customer_type from load_cust_general
    where customer_code IN (select customer_code from load_cust_partner
                                  where partner_code = i.customer_code and salesorg = i.salesorg and partner_function = 'ZS'))
                                                      LOOP
    -- exit when j%NOTFOUND;
         dbms_output.put_line(j.account_group);
    if (j.account_group = 'ZDIS') THEN
    v_type := 'DISAC';
              v_nature := '06';
         --     EXIT ;
    else
    v_type := 'SPACC';
    v_nature := '01';
    -- ********************* Part to be extended**************
    END IF;
    dbms_output.put_line(v_type||' '||v_nature);
    END LOOP;
    INSERT INTO sfdc_account
              (SAP_ACCOUNT_ID__C, NAME, TYPE, RECORDTYPEID, PARENTID, PHONE, FAX, WEBSITE, OWNERID, MARKETING_DOMAIN__C,
    INDUSTRIAL_SECTOR__C, ABC_CLASSIFICATION__C, NAME_1__C, NAME_2__C, NAME_3__C, NAME_4__C, PAYMENT_STATUS__C,
    CUSTOMER_GROUP__C, ADDRESS_STREET__C, CITY__C, POSTAL_CODE__C, COUNTRY__C, SALES_OFFICE__C, SALESORG__C,
    SALESDISTRICT__C, SALESGROUP__C, NATURE__C, VATCODE__C)
    VALUES((i.customer_code||i.salesorg), (v_cust_cur.Name1||' '||v_cust_cur.name2), ' ', v_type, v_parent_cust,
    v_cust_cur.phone_number, v_cust_cur.fax, v_cust_cur.web_address, v_partner_code, SUBSTR(v_cust_cur.industry_sector,1,2),
    v_cust_cur.industry_sector, v_cust_cur.customer_profile, v_cust_cur.name1, v_cust_cur.name2, v_cust_cur.name3,
    v_cust_cur.name4, v_credit_blk, v_custgrp, v_cust_cur.address, v_cust_cur.city, v_cust_cur.postal_code,
    v_cust_cur.country_key, v_salesoff, v_salesorg, v_sales_district,
    v_salesgrp, v_nature, v_cust_cur.vat_code);
    end loop;
    end loop;
    CLOSE c1;
    -- Delete data from Load Table
    -- EXECUTE IMMEDIATE 'TRUNCATE TABLE load_cust_general';
    /* truncate table load_cust_partner;
    truncate table load_cust_hierarhy;
    truncate table load_cust_sales_area;
    truncate table load_cust_company_cod;
    commit;
    exception
    WHEN NO_DATA_FOUND THEN
    raise_application_error( -20001, substr( sqlerrm, 1, 150 ) );
    when others then
    raise_application_error( -20001, substr( sqlerrm, 1, 150 ) );
    END;
    Thanks and Regards

  • Help in writing SQl Statment

    Hi all,
    I have a table which contain project details those are the coulmns:
    Project_number (PK)
    Submit Date
    Project_title
    Project_milestones
    I want to retrieve how many number of project in each mounth per year
    Output Example:
    Date Number of projects
    FEB 2008 *6*
    March 2008 *5*
    FEB 2009 *4*
    March 2009 *7*
    And so on, I tryed to write an sql statment to retrieve the desired results but I couldn't.
    If any one has any idea in retrieving such result please assist me how to write this sql statment.

    Hi,
    If submit_date is a DATE, then TRUNC (submit_date, 'MM') is the beginning of the month that countains it.
    So you can say:
    SELECT    TRUNC (submit_date, 'MM')   AS month
    ,         COUNT (*)                   AS cnt
    FROM      project_table
    GROUP BY  TRUNC (submit_date, 'MM');Of course, you can add TO_CHAR to the first line to format the date however you like.

  • How to access currently executing SQL statment?

    Hello -
    I have a C language external procedure that is called from an INSERT / UPDATE / DELETE trigger.
    Is there a way (using OCI, or elsewhere) to get a string containing the SQL statment that fired the trigger?
    Thanks a bunch for your help!
    -js

    http://databasejournal.com/features/oracle/article.php/3373701
    The second page has more details (there is also a part II to this article if you are interested).

Maybe you are looking for

  • Zen Vision M & Creative Organise

    I updated my Zen Touch to a Zen Vision M last summer and successfully transferred my tracks (sort of) but I am having problems with the new, updated Creative Organiser?5. In the previous version I was using, I could manipulate the information on my Z

  • Running Total of Statuses?

    Apparently I need to create a SQL to pull a "running total" to track certain statuses over its life. So for example, i would have PENDING->PREAPPROVED->APPROVED A person may move through these 3 statuses over an unspecified amount of time. Lets say I

  • Query Explain Plan Help

    I am running the following query against my database SELECT p.ID   FROM rap_counterparties cou,        rap_presentation_cou_ratings pcr,        rap_presentation_states sta,        rap_app_user_profiles aup,        rap_presentations p WHERE cou.unt_id

  • SELECTION OF MULTIPLE ROWS

    Hi all, i am using FM REUSE_ALV_GRID_DISPLAY TO display records. I am able to select only one row ata time . i need to use ctrl or shift key to select multiple rows is there a way that i can select multiple rows with just using mouse and without usin

  • Can i use time capsule as a backup drive?

    i know that sounded stupid... right now i have external hard drive where i back up my files like my music, jobs, music...because i don't have enough space on my internal drive is it possible for me to "backup" by dragging them onto TC? i don't even k