• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

function calls in cfscript

New Here ,
Oct 07, 2014 Oct 07, 2014

Copy link to clipboard

Copied

i am looking for an example of using cfscript to pass varibles into a query  and NOT HARDCODED into the addParam value attribute.

Sugesstions ?

Views

245

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2014 Oct 08, 2014

Copy link to clipboard

Copied

LATEST

You could just create your own custom function. To illustrate, take the following example from the documentation:

<cfscript>

    queryService = new query();

    queryService.setDatasource("cfdocexamples");

    queryService.setName("getParks");

    queryService.addParam(name="state",value="MD",cfsqltype="cf_sql_varchar");

    queryService.addParam(value="National Capital Region",cfsqltype="cf_sql_varchar");

    result = queryService.execute(sql="SELECT PARKNAME, REGION, STATE FROM Parks WHERE STATE = :state and REGION = ? ORDER BY ParkName, State ");

    getParks = result.getResult();

</cfscript>

<cfdump var="#getParks#">

You could customize it as follows:

<cfscript>

    queryService = new query();

    queryService.setDatasource("cfdocexamples");

    queryService.setName("getParks");

    qParams = getQueryParams();

    result = queryService.execute(sql="SELECT PARKNAME, REGION, STATE FROM Parks WHERE STATE LIKE '#qParams.stateFirstLetter#%' and REGION LIKE '#qParams.regionFirstLetter#%' ORDER BY State, ParkName");

    getParks = result.getResult();

    function getQueryParams() {

        var myParams=structnew();

        myParams.stateFirstLetter = "M";

        myParams.regionFirstLetter = "N";

        return myParams;

    }

</cfscript>

<cfdump var="#getParks#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation