[英]How can I insert one register into related tables in SQL Server
I have this table 我有这张桌子
Usuario 乌苏里奥
And I have this other table: 我还有另外一张桌子:
Imagenes Imagenes
The relation: 关系:
Usuario.id_usuario = Imagenes.id_usuario
And I try to insert a picture into pictures but I need to take the id_usuario
from Usuario
我尝试将图片插入图片,但我需要采取id_usuario
从Usuario
This is for a login system, and when I try to insert a picture, I have to do a query inserting the id_usuario
. 这是用于登录系统的,当我尝试插入图片时,我必须执行查询以插入id_usuario
。 I want to write code that will automatically recognize the id for the moment when I insert the picture in Imagenes
. 我想编写将图像插入Imagenes
时会自动识别ID的代码。
How can I do this? 我怎样才能做到这一点?
You would use scope_identity() to get the identity value generated by the previous insert statement. 您将使用scope_identity()来获取由先前的insert语句生成的标识值。 So something like: 所以像这样:
declare @id_usuario int
insert into Usuario
([User], [Password], FristName, Lastname, [EMail], Photo_avatar)
values (@User, @Password, @FristName, @Lastname, @EMail, @Photo_avatar)
set @id_usuario = SCOPE_IDENTITY()
insert into Imagenes
(id_usuario, Picture, Picture_name)
values (@id_usuario, @Picture, @Picture_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.