简体   繁体   中英

How to insert data into a vertically partitioned table in sql server

I have a problem with the partitioning of a table in the SQL Server . I have a table with over 103 columns out of which only 20 are used very frequently and are referenced by a number of tables.

As the table contains thousands of rows I have created a vertical partition and divided the table into multiple tables so as to save the table data in different file groups.

I have also created a view by joining those tables. And now, how do i insert data into different tables without using INSTEAD OF triggers?

You can use stored procedure to encapsulate all your insert logic.

If you have, let's say, 3 tables that share same ID column and each have it additional columns, your stored procedure might look something like this:

CREATE PROCEDURE usp_Insert
(
    @Val1 VARCHAR(5)
  , @Val2 VARCHAR(5)
  , @Val3 VARCHAR(5)
)
AS 
BEGIN

    DECLARE @id INT;

    INSERT INTO Table1 (Col1) VALUES (@Val1);

    SELECT @id = SCOPE_IDENTITY();

    INSERT INTO Table2 (ID, Col2) VALUES (@id, @Val2);
    INSERT INTO Table3 (ID, Col3) VALUES (@id, @Val3);

END
GO

SQLFiddle Working demo and sample tables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM