Passing a Table Valued parameter to a stored procedure

前端 未结 4 452
予麋鹿
予麋鹿 2021-01-06 06:54

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

相关标签:
4条回答
  • 2021-01-06 07:03

    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.

    0 讨论(0)
  • 2021-01-06 07:11

    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

    0 讨论(0)
  • 2021-01-06 07:14

    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>
    
    0 讨论(0)
  • 2021-01-06 07:14

    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
    
    0 讨论(0)
提交回复
热议问题