将行插入到具有外键的表中 [英] Insert row into table that have foreign key
问题描述
嗨
我有两个名为Tbl_manager和Tbl_subteam的表,其关系为1到n
Tbl_manager表:
折叠|复制代码
UserId uniqueidentifier --->主键
name_manager nvarchar(50)
familli_manager nvarchar(50)
Code_m nvarchar(256)
, ...
------------------------------- ---------
Tbl_SubTeam表:
折叠|复制代码
ID_number int
SubTeam_name nvarchar(50)
SubTeam_Familli nvarchar(50)
Code_m nvarchar(50)
Manage_Id uniqueidentifie ---> foriegn key
,...
我的问题是:
如何插入与tbl_manager相关的Tbl_subteam表?我尝试使用follow命令,但是错了:
Hi
I have two tables with names Tbl_manager and Tbl_subteam that have relation 1 to n
Tbl_manager Table:
Collapse | Copy Code
UserId uniqueidentifier ---> primary Key
name_manager nvarchar(50)
familli_manager nvarchar(50)
Code_m nvarchar(256)
,...
----------------------------------------
Tbl_SubTeam Table:
Collapse | Copy Code
ID_number int
SubTeam_name nvarchar(50)
SubTeam_Familli nvarchar(50)
Code_m nvarchar(50)
Manage_Id uniqueidentifie ---> foriegn key
,...
my problem is :
how I Could Insert into Tbl_subteam table that is related to tbl_manager?. I tried with follow command , but is wrong :
INSERT INTO Tbl_subteam(SubTeam_name, SubTeam_Familli, Code_m, Manage_Id)
VALUES(@SubTeam_name,@SubTeam_Familli,@Code_m,IDENT_CURRENT('Tbl_Manager'))
where User_Name=@Username
推荐答案
如果您尝试插入1个表,然后获取任何列值并将其插入另一个表然后尝试
创建用户定义的表类型
首先插入用户定义的表,然后通过SCOPE_IDENTITY()获取主键,然后将其插入另一个表中了解更多详细信息请参阅这些链接
用户定义的表类型
表 - 值参数(数据库引擎)
sql-server-table-valued-parameters-in-sql-server
如果它在这里不起作用我会发布查询
If You are trying to Insert into 1 table then get any column value and insert it into another table then try to
Create User-Defined Table Types
First Insert into user defined table then get primary key by SCOPE_IDENTITY() and then insert it into another table For More Detail See these Links
User-Defined Table Types
Table-Valued Parameters (Database Engine)
sql-server-table-valued-parameters-in-sql-server
If it doesn't work post here I will post the query
你必须在管理器表中创建记录才能尝试插入记录如果经理还不存在,则在你的Subteam表格中,如果经理还不存在。
类似于:
You have to create the record in your Manager table before trying to insert a record in your Subteam table that is related to it, if the manager does not exist yet.
Something like:
DECLARE id uniqueidentifier;
SET @id = NEWID();
INSERT INTO Tbl_manager (UserId, name_manager, famili_manager, Code_m) VALUES (@id, N'Name', N'Family', N'Code');
GO
INSERT INTO Tbl_SubTeam (SubTeam_name, SubTeam_Familli, Code_m, Manage_Id) VALUES (N'MySubteam', N'STFamily', N'MyCode', @id);
GO
希望这有帮助。
Hope this helps.
INSERT INTO Tbl_subteam(SubTeam_name,SubTeam_Familli,Code_m,Manage_Id)
VALUES (@ SubTeam_name,@ SubTeam_Familli,@ Code_m,IDENT_CURRENT('Tbl_Manager'))
其中User_Name = @用户名
INSERT INTO Tbl_subteam(SubTeam_name, SubTeam_Familli, Code_m, Manage_Id)
VALUES(@SubTeam_name,@SubTeam_Familli,@Code_m,IDENT_CURRENT('Tbl_Manager'))
where User_Name=@Username
据我所知,你不能在 INSERT
语句中有一个 where
子句。你必须更新已经存在的记录。
使用关节获取ID并更新记录。
As far as i know,you cannot have a where
clause in INSERT
statement.You have to update the already existing record.
Use joints to get the ID and update the record.
这篇关于将行插入到具有外键的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!