我有两张桌子:绿色(用户ID,姓名,地址),红色(用户ID,姓名,TemproraryAddress)
我想
select from Green table (Name) Where = userID
AND
INSERT INTO Red(userID,Name,TemproraryAddress)
VALUES (@userID,@Name,@TemproraryAddress)
我的主要问题是如何将选定的值分配给标量变量?
注意:此示例表用于显示示例场景,而不是用于实际使用...
答案 0 :(得分:2)
你的意思是
insert into Red(userId, Name, TemporaryAddress
select userId, Name, Address
from Green
where userId = @userId
或
declare @userId int, @Name varchar(100), @address varchar(100)
select @userId = userId, @Name = Name, @address = Address from Green where userId = 12345
insert into Red (userId, Name, TemporaryAddress) values (@userId, @Name, @address)
或其他什么?
...你的问题不清楚你究竟想做什么。你现有的代码是什么样的,你想要它做什么不做?
修改强>
另一种选择是在Green表上使用插入触发器。我不确定你是否想要基本匹配表格,但如果你这样做,那么这将是如何做到的:
create trigger green_InsToRed_TRG on Green for insert as
insert into Red (UserId, Name, TemporaryAddress)
select UserId, Name, Address from inserted