如何在存储过程中使用局部变量?

问题描述:

如果我想从一个表中选择任何 id 并将它的值作为外键插入另一个表中,那么我将如何通过存储过程来做到这一点?

if I want to select any id from a table and want to insert it's value in another table as foreign key then how i will do it through stored procedure?

我将如何处理此问题的示例.

An example of how I would approach this.

DECLARE @MyID INT;

SET @MyID = 0;

SELECT @MyID = [TableID]
  FROM [MyTable]
 WHERE [TableID] = 99;

IF @MyID > 0
BEGIN

    INSERT INTO [MySecondTable]
         VALUES (@MyID, othervalues);

END