1DECLARE @primaryTVP primary_tbltype
2DECLARE @relatedTVP related_tbltype
3
4INSERT INTO @primaryTVP values (1, 'John', 'Cleese')
5INSERT INTO @primaryTVP values (2, 'Eric', 'Idle')
6INSERT INTO @primaryTVP values (3, 'Graham', 'Chapman')
7
8INSERT INTO @relatedTVP values (1, '29310918', 28934.33)
9INSERT INTO @relatedTVP values (2, '123123', 3418.11)
10INSERT INTO @relatedTVP values (2, '33333', 666.66)
11INSERT INTO @relatedTVP values (3, '554433', 22.22)
12INSERT INTO @relatedTVP values (3, '239482', 151515.15)
13
14
15EXEC MySproc @primaryTVP, @relatedTVP;
1CREATE PROCEDURE MySproc
2 @PrimaryTable primary_tbltype READONLY,
3 @RelatedTable related_tbltype READONLY
4AS
5BEGIN
6DECLARE @CurrentKey INT
7DECLARE @FirstName VARCHAR(30)
8DECLARE @LastName VARCHAR(30)
9DECLARE @AccountTotal MONEY
10
11DECLARE PersonCursor CURSOR LOCAL FAST_FORWARD FOR
12 SELECT personkey, firstname, lastname FROM @PrimaryTable
13OPEN PersonCursor
14FETCH NEXT FROM PersonCursor INTO @CurrentKey, @FirstName, @LastName
15WHILE @@FETCH_STATUS= 0 BEGIN
16
17 SELECT @AccountTotal = SUM(accountbalance) FROM @RelatedTable
18 WHERE fk_personkey = @CurrentKey
19
20 PRINT @FirstName + ' ' + @LastName + ' - account total: ' + CONVERT(VARCHAR(30), @AccountTotal)
21
22 FETCH NEXT FROM PersonCursor INTO @CurrentKey, @FirstName, @LastName
23END
24END;