I am trying to make stored procedure that:
– Get list of int rows
select ItemId from Items -- this returns: 1,2,3,4,5,6
-
In the second part of procedure I have to add row in another table for each of selected number.
Something like:foreach ItemId in previous result
insert into table (ItemIdInAnotherTable) values (ItemId)
UPDATEI miss one important part from question.
In another part of procedure when I am inserting selected items in another table need to insert a few more columns. Something like this:insert into dbo.ItemsNotificator
(UserId,ItemId)
(13879, (select ItemId from Items))
So it’s not one column. Sorry for confusion 🙁
Edit :
Assuming that the table
[table]already exists, and if User is a constant, then do like so:If
UserIdcomes from another table entirely, you’ll need to figure out what relationship you need between UserId and ItemId. For instance, if all users are linked to all items, then it is:If table
[table]does NOT already exist, then you can useSELECT INTO, and specify a new table name (e.g. a#temptable stored intempdb)The columns in the newly created table will have the names
UserIdandItemIdand have the same types.