use type in stored procedure sql server code example
Example 1: usp table variable input
CREATE TYPE LocationTableType
AS TABLE
( LocationName VARCHAR(50)
, CostRate INT );
GO
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 @LocationTVP AS LocationTableType;
INSERT INTO @LocationTVP (LocationName, CostRate)
SELECT Name, 0.00
FROM AdventureWorks2012.Person.StateProvince;
EXEC usp_InsertProductionLocation @LocationTVP;
Example 2: best practive to pass multiple table to stored procedure in sql server
CREATE PROCEDURE MySproc
@PrimaryTable primary_tbltype READONLY,
@RelatedTable related_tbltype READONLY
AS
BEGIN
DECLARE @CurrentKey INT
DECLARE @FirstName VARCHAR(30)
DECLARE @LastName VARCHAR(30)
DECLARE @AccountTotal MONEY
DECLARE PersonCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT personkey, firstname, lastname FROM @PrimaryTable
OPEN PersonCursor
FETCH NEXT FROM PersonCursor INTO @CurrentKey, @FirstName, @LastName
WHILE @@FETCH_STATUS= 0 BEGIN
SELECT @AccountTotal = SUM(accountbalance) FROM @RelatedTable
WHERE fk_personkey = @CurrentKey
PRINT @FirstName + ' ' + @LastName + ' - account total: ' + CONVERT(VARCHAR(30), @AccountTotal)
FETCH NEXT FROM PersonCursor INTO @CurrentKey, @FirstName, @LastName
END
END;