Passing a Table Valued parameter to a stored procedure

怎甘沉沦 提交于 2019-12-19 03:56:06

问题


Here's an example of how to use table-valued parameters in a SQL Server 2008 stored procedure using .NET.

And here's the list of parameter types in CF9.

Q: Is it possible for ColdFusion to pass a table-valued parameter to a Microsoft SQL Server 2008 stored procedure?


回答1:


Not sure.

If you need to pass a table of information, probably your best bet is to use the XML data type.

Code sample here.




回答2:


Short Answer: No support, it should, vote for it!

Long Answer: Coldfusion can use JDBC, which does not yet support TVP's, but it should. Vote for the feature here: http://mssqlconn.uservoice.com/forums/113295-feature-feedback/suggestions/2269687-table-valued-parameters-tvp-support-in-jdbc

XML will work, but using TVP's makes both the client and sproc code easier to read, write, review, and debug. It is also faster in most cases depending on the API's implementation.

FYI, using Oracle is no better. They have the ARRAY SQL data type (which is closest to TVP). It is also not supported by JDBC: Using Array Objects




回答3:


I discovered this workaround. You can call a stored procedure from within a cfquery, this way you can pass in a Table valued parameter TVP.

<cfquery datasource="" name="">
     DECLARE        @return_value int

     -- Create table value parameter
     DECLARE @DataTVP tDataTable;

     --Build Table
     INSERT INTO @DataTVP(DataId)
     VALUES (1),(2),(3)

     EXEC        @return_value = P_DeleteItems
                 @tvpData = @DataTVP --Pass table into Stored Procedure

     SELECT        'Return Value' = @return_value 
</cfquery>



回答4:


I think the same you need XML Data Type to do so. I am providing an example over here. ##

-- Create a Database
CREATE DATABASE DataTableTest
USE DataTableTest
GO

--Create the sample tables
CREATE TABLE Employees (
    EmployeeID BIGINT IDENTITY(1,1),
    EmployeeName VARCHAR(50),
    DepartmentID BIGINT )

CREATE TABLE Departments (
    DepartmentID BIGINT IDENTITY(1,1),
    DepartmentName VARCHAR(50) )

GO

-- Populate the Sample Tables
INSERT INTO Departments ( DepartmentName)
SELECT 'IT'

INSERT INTO Employees (EmployeeName, DepartmentID )
SELECT 'JCB', 1
GO

Now let us create a stored procedure which returns two result sets.

CREATE PROCEDURE GetEmployeeInfo
AS
SET NOCOUNT ON  

  SELECT EmployeeName, DepartmentID
    FROM Employees
    WHERE EmployeeID = 1

    SELECT DepartmentName FROM Departments
    WHERE DepartmentID = 1
    GO

Let us create the next stored procedure which accepts an XML parameter. This procedure will insert the data from the XML parameter, into the Employee Table.

CREATE PROCEDURE ProcessXml
(
    @data XML
)
AS

INSERT INTO Employees(EmployeeName, DepartmentID)
SELECT
    x.d.value('EmployeeName[1]','VARCHAR(50)') AS EmployeeName,
    x.d.value('DepartmentID[1]','INT') AS DepartmentID
FROM @data.nodes('/NewDataSet/Table') x(d)

GO


来源:https://stackoverflow.com/questions/2683869/passing-a-table-valued-parameter-to-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!