sql server - T-SQL Check if list has values, select and Insert into Table -
i'm quite new t-sql , struggling insert statement in stored procedure: use parameter in stored procedure list of ids of type int.
if list not empty, want store ids table delivery.
to pass list of ids, use table type:
create type tidlist table ( id int null ); go
maybe know better way pass list of ids stored procedure?
however, procedure looks follows:
-- parameter @deliverymodelids tidlist readonly ... declare @storeid int = 1; -- delivery if exists (select * @deliverymodelids) insert [mydb].[delivery] ([deliverymodelid], [storeid]) output inserted.deliveryid select id @deliverymodelids;
if list has values, want store values db storeid 1.
if insert deliveryids 3,7,5 result in table delivery should this:
deliveryid | storeid | deliverymodelid
1...............| 1...........| 3
2...............| 1...........| 7
3...............| 1...........| 5
do have idea on how solve issue?
thanks !
you can add @storeid
select
insert
.
... if exists (select * @deliverymodelids) insert [mydb].[delivery] ([deliverymodelid], [storeid]) output inserted.deliveryid select id, @storeid @deliverymodelids;
additionally, if want insert deliverymodelid
not exist in target table, can use not exists()
in where
clause so:
... if exists (select * @deliverymodelids) insert [mydb].[delivery] ([deliverymodelid], [storeid]) output inserted.deliveryid select dmi.id, @storeid @deliverymodelids dmi not exists ( select 1 mydb.delivery i.storeid = @storeid , i.deliverymodeid = dmi.id );
Comments
Post a Comment