在Coldfusion中执行SQL函数

Mad*_*shM 5 sql coldfusion function

我在MS SQL(2005)中有一个用户定义的函数,我想通过Coldfusion(8)执行.知道我怎么能让这个工作吗?

[这不是存储过程,因此cfstoredproc已经完成].

先感谢您.

Dan*_*sen 14

您可以在常规CFQUERY块内调用SQL UDF,假设您的Coldfusion数据源对所需的UDF具有适当的EXECUTE权限.您不必在CFQuery中返回select语句.如果要将任何Coldfusion变量传递给数据库,请记住使用CFQUERYPARAM来确保安全.

要调用UDF,您需要包含其架构*.在我们的例子中,我们使用了默认模式,因此我们的UDF以"dbo"为前缀.如在dbo.FunctionName()中.

这是一个例子:

<!--- We want to convert this numeric category into its English name.
      Thankfully our database administrator has a simple function to 
      resolve it without extra work on our part. --->

<cfset myCategory = 100428>

<!--- We call this Coldfusion variable using CFQUERYPARAM to prevent SQL Injection --->

<cfquery datasource="mydatasource" name="test">
  SELECT dbo.CategoryAsString(<cfqueryparam cfsqltype="cf_sql_integer" value="#myCategory#">) AS CategoryString
</cfquery>

<!--- And output the result here --->
<cfdump var="#test.CategoryString#">
Run Code Online (Sandbox Code Playgroud)

*如果您不包含UDF的架构,您将收到"执行数据库查询时出错.[Macromedia] [SQLServer JDBC驱动程序] [SQLServer]'[FUNCTION NAME]'不是可识别的内置函数名称." (CF 8)