私は ColdFusion を使用しており、特定のアカウントの特定の列の値を取得できる関数を作成しようとしています (各アカウントは独自のレコード/行です)。
このような関数は正常に動作します:
<cffunction name="getColumnValueFromAccount" access="public" returntype="string" >
<cfargument name="accountName" type="string" required="yes" />
<cfquery name="getColumn" datasource="mydatasource">
<!--- Note that the line below is 'hard-coded.' --->
SELECT role_ExampleSystem
FROM table_name
WHERE (accountName = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value='#accountName#'>)
</cfquery>
<!--- It's easy to return the column value when you know what its name was. --->
<cfreturn getColumn.role_ExampleSystem >
</cffunction>
しかし、私が実際に必要としているのは、どの列名から読み取るかを指定できる関数であり、異なるハードコードされた SELECT パラメーターを持つほぼ同一の CF 関数の束を作成する必要がなくなります。私はそれがこのように見えるはずだと思っていますが、実際に返されるはずの単一の文字列を実際に読み取るのに問題があります。
<cffunction name="getColumnValueFromAccount" access="public" returntype="string" >
<cfargument name="accountName" type="string" required="yes" />
<!--- Trying to accept a column name as an argument --->
<cfargument name="columnName" type="string" required="yes" />
<cfquery name="getColumn" datasource="mydatasource">
<!--- I'm trying to use cfqueryparam to add specify the column name to select. --->
SELECT <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value='#columnName#'>
FROM table_name
WHERE (accountName = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value='#accountName#'>)
</cfquery>
<!--- This line doesn't work. --->
<cfreturn getColumn[#columnName#] >
</cffunction>
getColumn[#columnName#]
orのようにブラケット表記で変数を使用できると思ったのは、getColumn[columnName]
誰かがコメントで言及したからです。しかし、自分で変数を使用しようとすると、期待どおりに機能しませんでした。次のエラーが表示されます。
The value returned from the getColumnValueFromAccount function is not of type string. If the component name is specified as a return type, it is possible that either a definition file for the component cannot be found or is not accessible.
cfquery の単一の結果を取得したいが、クエリの SELECT 部分でハードコーディングされた列名を使用していない場合に、どのルートを使用する必要があるか考えていますか? 通常、このプロセスは非常に単純ですが、列名が変数の場合は少し異なるようです。