sql how can I use my uder-defined table type code example

Example 1: create user defined table type in sql

CREATE TYPE UT_Employee AS TABLE  
(  
Emp_Id int NOT NULL,  
EmployeeName nvarchar(MAX),  
EmpSalary varchar(50),  
StateId varchar(50),  
CityId varchar(50)  
);
CREATE PROCEDURE USP_Insert_Employee_Infi(@Employee_Details [UT_Employee])  
AS  
BEGIN  
  
INSERT INTO dbo.Employee  
(  
Emp_Id,  
EmployeeName,  
EmpSalary,  
StateId,  
CityId  
)  
SELECT * FROM @Employee_Details  
END;

Example 2: usp table variable input

/* Create a table type. */
CREATE TYPE LocationTableType 
   AS TABLE
      ( LocationName VARCHAR(50)
      , CostRate INT );
GO
/* Create a procedure to receive data for the table-valued parameter. */
CREATE PROCEDURE dbo. usp_InsertProductionLocation
   @TVP LocationTableType READONLY
      AS
      SET NOCOUNT ON
      INSERT INTO AdventureWorks2012.Production.Location
         (
            Name
            , CostRate
            , Availability
            , ModifiedDate
         )
      SELECT *, 0, GETDATE()
      FROM @TVP;
GO
/* Declare a variable that references the type. */
DECLARE @LocationTVP AS LocationTableType;
/* Add data to the table variable. */
INSERT INTO @LocationTVP (LocationName, CostRate)
   SELECT Name, 0.00
   FROM AdventureWorks2012.Person.StateProvince;
  
/* Pass the table variable data to a stored procedure. */
EXEC usp_InsertProductionLocation @LocationTVP;

Tags:

Misc Example