Search code examples
coldfusionvertica

how to cfloop query the line if there is only 1 column


this is my sql query

`CONST_SQL_clean="
    select    table_name from  tables where  TABLE_NAME  not in ('TBL_SY', 'TBL_USER')

        ";`

i want to modify the result and execute it as new query for every table name selected by this query

<cffunction name="clean_data" access="remote" returntype="struct" >


    <cfscript>
            lSqlManager = createObject("component","#request.stMapping.strCFC#.sqlManager");
            lSqlManager.setSqlString(CONST_SQL_select);                      

    </cfscript> 


      <CFQUERY NAME="qResult" DATASOURCE="#request.stMapping.strODBCName#">
            #lSqlManager.getSqlString()#
      </CFQUERY>

      <cfloop query="qResult">
      <cfset str= "delete" & #qResult# >
      QueryExecute  (str);
      </cfloop>
    <cfscript>
     return qResult;
    </cfscript>

Solution

  • Simplify this. It will be much easier.

    First, get the tables you want to empty.

    <cfquery name = "tablesToClear">
    select table_name
    from tables
    where table_name not in ('TBL_SY', 'TBL_USER')
    </cfquery>
    

    Now create your loop and delete your rows.

    <cfoutput query = "tablesToClear">
    <cfquery>
    delete from #table_name#
    </cfquery>
    </cfoutput>