I have a query where a record gets added to the table channels. Each record there is then assigned a channelID which is the PK for the table, and auto increments. After a record is added to the channel table I want to insert strings associate with that record into another table called channel keywords. My problem is that I also need to pass in the channelID that created in the previous query as a parameter for the insert statement for the channelskeywords table, but I am not quite sure how to do this.
IF @matchBy ='title'
BEGIN
insert into channels
(userID,matchTitle,matchTitleAbstract, fromMyPage)
values
(@userID,1,0,@fromMyPage)
END
IF @matchBy ='TitleAbstract'
insert into channels
(userID,matchTitle,matchTitleAbstract, fromMyPage)
values
(@userID,0,1,@fromMyPage)
BEGIN
IF (NULLIF(@keyword1, '')) IS NOT NULL
insert into channelsKeywords
Try something like this
Additional Information:
@@IDENTITYreturns the last identity value generated for any table in the current session, across all scopes. You need to be careful here, since it’s across scopes. You could get a value from a trigger, instead of your current statement.SCOPE_IDENTITYreturns the last identity value generated for any table in the current session and the current scope. Generally what you want to use.IDENT_CURRENTreturns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren’t quite what you need (very rare).See also here