Passing a Table Valued parameter to a stored procedure

前端 未结 4 479
予麋鹿
予麋鹿 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: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
    

提交回复
热议问题