stored procedures - SQL Server : Insert Multiple Rows into Multiple Tables From Table Type Paramater -
stored procedures - SQL Server : Insert Multiple Rows into Multiple Tables From Table Type Paramater -
i'm trying write stored procedure takes in table type parameter , inserts 2 tables @ once.
i have entity table base of operations table holding id various tables, below entity table , sample site table.
------ entity table ------------------------------------------ | id | bigint | not null | identity(1,1) | primary key | typeid | tinyint | not null | | updated | datetime | null | | created | datetime | not null | | isactive | bit | not null | ------- site table --------------------------------------- | entityid | bigint | not null | primary key | producttypecode | nvarchar(8) | not null | primary key | suppliercode | nvarchar(8) | not null | primary key | name | nvarchar(128) | not null | | description | nvarchar(max) | null |
and here table type used pass stored procedure
------- site table type ---------------------------------- | entitytypeid | tinyint | not null | | producttypecode | nvarchar(8) | not null | primary key | suppliercode | nvarchar(8) | not null | primary key | name | nvarchar(128) | not null | | description | nvarchar(max) | null |
the thought pass in table type parameter stored procedure , insert multiple rows @ 1 time save looping inserting 1 row @ time.
here's have far
create procedure insertsites @sites sitetypetable readonly begin -- insert entity & site tables here, using id entity table in site table insert entity (typeid, updated, created, isactive) output [inserted].[id], s.producttypecode, s.suppliercode, s.name, s.description site select entitytypeid, null, getdate(), 1 @sites s end
i've read using insert , output cannot work. i've read merge cannot work.
any help or pointers can give appreciated.
thanks
neil
---- edit ----
could this? i'm not sure how finish off...
create procedure insertsites @sites sitetypetable readonly begin -- first insert plenty rows entity table, saving inserted ids table variable declare @insertedoutput table (entityid bigint) insert entity (typeid, updated, created, isactive) output [inserted].[id] @insertedoutput select entitytypeid, null, getdate(), 1 @sites s -- utilize ids in @insertedoutput against rows in @sites insert sites end
sql-server stored-procedures
Comments
Post a Comment