user5115180
user5115180

Reputation:

Calling stored procedure to insert multiple values

In our application we have a multiline grids which have many records. For inserting or updating we are calling a stored procedure.

As per the current implementation the stored procedure is calling for each line in the grid. For each line it checks the existence in the table. If data is already there, it will update the table else insert new data into the table.

Instead of calling the procedure for each line, we thought create a table value parameter and pass all the grid values at the same time.

My questions are:

  1. Is it a good approach?

  2. How to handle the existence check (for insert or update) if I pass the values as table-valued parameter? Do I need to loop through the table and check it?

  3. Is it better to have separate stored procedures for insert and update?

Please provide your suggestions. Thanks in advance.

Upvotes: 2

Views: 1069

Answers (4)

Lukasz Szozda
Lukasz Szozda

Reputation: 175736

1) Using TVP is good approach, but send only new or updated rows as TVP, no need to send entire datagrid.

2) For INSERT/UPDATE use MERGE example:

MERGE [dbo].[Contact] AS [Target] 
USING @Contact AS [Source] ON [Target].[Email] = [Source].[Email]
WHEN MATCHED THEN 
   UPDATE SET [FirstName] = [Source].[FirstName],
              [LastName] = [Source].[LastName]
WHEN NOT MATCHED THEN
    INSERT ( [Email], [FirstName], [LastName] )
    VALUES ( [Source].[Email], [Source].[FirstName], [Source].[LastName] );

3) For your case one stored procedure is enough.

Upvotes: 0

e64 Web Developers
e64 Web Developers

Reputation: 31

1) TVP is a good approach. And a single stored proc call is more efficient with fewer calls to the Database.

2) You haven't made it clear if each row in the grid has some kind of ID column that determines if the data exists in the Table, however assuming there is, make sure that it is indexed then use INSERT INTO and UPDATE statements like this:

To add new rows:

INSERT INTO [grid_table] 
SELECT * FROM [table_valued_parameter]
WHERE [id_column] NOT IN (SELECT [id_column] FROM [grid_table])

To update existing rows:

UPDATE gt
SET gt.col_A = tvp.col_A,
    gt.col_B = tvp.col_B,
    gt.col_C = tvp.col_C,
    ...
    gt.col_Z = tvp.col_Z
FROM [grid_table] gt 
INNER JOIN [table_valued_parameter] tvp ON gt.id_column = tvp.id_column

NB:

  • No need to do an IF EXISTS() or anything as the WHERE and JOIN clauses will run the same checks,so no need to do a 'pre-check' before running each statement.
  • This assumes the TVP data isthe same structure as the Table in the database.
  • YOU MUST make sure the id_column is indexed.
  • I've use 'INNER JOIN' instead of just 'JOIN' to make the point it is an inner join

3) Using the approach above you just new one stored proc, simple and effective

Upvotes: 2

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

  1. Yes, it is a good approach. Calling procedure for each row is bad for performance. TVPs make life easier.
  2. Yes, you can do that check in stored procedure, which should be a simple SELECT on uniqueId in most of the cases.
  3. With this approach, yes, it is better to have both in same stored procedure.

Upvotes: 1

Jagadish Sharma U
Jagadish Sharma U

Reputation: 496

  1. It's a good approach

  2. Any way try to put the logic through object level for iterating and checking and finally insert/update in T-SQL. This reduces overhead for RDMS as object level functionality is faster than operations in RDBMS.

  3. Dont put too may stored procedures for each type of operation have a minimised procedures with multiple operations based on parameters you send to it.

Hope it helps!

Upvotes: 1

Related Questions