Search
 
SCRIPT & CODE EXAMPLE
 

SQL

best practive to pass multiple table to stored procedure in sql server

DECLARE @primaryTVP primary_tbltype
DECLARE @relatedTVP related_tbltype

INSERT INTO @primaryTVP values (1, 'John', 'Cleese')
INSERT INTO @primaryTVP values (2, 'Eric', 'Idle')
INSERT INTO @primaryTVP values (3, 'Graham', 'Chapman')

INSERT INTO @relatedTVP values (1, '29310918', 28934.33)
INSERT INTO @relatedTVP values (2, '123123', 3418.11)
INSERT INTO @relatedTVP values (2, '33333', 666.66)
INSERT INTO @relatedTVP values (3, '554433', 22.22)
INSERT INTO @relatedTVP values (3, '239482', 151515.15)


EXEC MySproc @primaryTVP, @relatedTVP;
Comment

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;
Comment

PREVIOUS NEXT
Code Example
Sql :: "Edad en Oracle" 
Sql :: mysql_error replacement 
Sql :: Grant read-only privilleges to the user 
Sql :: how to check if there is no database schema 
Sql :: Selecting data from table where sum of values in a column equal to the value in another column 
Sql :: psql 
Sql :: 10 random questions use python and SQL 
Sql :: codeigniter MySQL - Issue with SELECT & GROUP BY 
Sql :: how to user id to show in from date to upto date in mssql server 
Sql :: oracle grant alter table constraint 
Sql :: what is group function in sql 
Sql :: practice sql queries 
Sql :: phpmyadmin access denied 
Sql :: sql select merge multiple values 
Sql :: sql multi row insert 
Sql :: backup table mssql 
Sql :: sql replace id with name from another table 
Sql :: check if mysql db is used 
Csharp :: guid.empty 
Csharp :: unity find objects with tag 
Csharp :: c# store byte array as string 
Csharp :: c# find start and end of month from object date 
Csharp :: ALWAYS MAximize window on start c# 
Csharp :: how to call something once in update 
Csharp :: change height of rect transform unity 
Csharp :: unity access child 
Csharp :: c# for loop backwards 
Csharp :: unity log 
Csharp :: path desktop c# 
Csharp :: delete null elements array c# 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =