在foregin键列中插入ID [英] inserting id in foregin key column
问题描述
先生/女士,我有两个表Employee和Department我通过主键和外键联接.这是两个示例表
雇员
empid(pk)
姓名
depid(fk)
部门
depid(pk)
部门名称
当我插入所有部门名称(部门名称)时,我创建了depid(pk)作为用户输入.但是,当我从员工Windows窗体插入时,我从组合框中选择部门名称,并希望将Deptname中的depid(fk)插入到列中.所以请告诉我存储过程中查询员工数据的方式.或我如何实施此建议,将对您有所帮助.
谢谢您
sir/Mam , I have two tables Employee and Department i have join with primary key and foreign key.here are two example tables
Emplloyee
empid(pk)
Name
depid(fk)
Department
depid(pk)
Deptname
As I have inserted all the departments names (Deptname).I have created depid(pk) as user input. but when i insert from employee windows form i select department name from combo box and want to insert depid(fk) from Deptname into column. so please tell me how will be the query in stored procedure for inserting data of employee. or how i can implement this any suggestion will be help full.
Thanking you
推荐答案
这是可以与DBCommand一起使用的命令字符串(我不知道您使用哪种连接类型).
This is command string which can be used with DBCommand (I don''t know which type of connection you use).
INSERT INTO Employee
(Name, depid)
VALUES
(@Name, @depid)
可以轻松将其复制到存储过程.
您没有提到要使用哪个RDBMS(我想是来自SQL Server家族的RDBMS)和列的数据类型,因此我无法提供确切的代码,但我可以使用类似以下代码的代码:
It can be easily copied to stored procedure.
You haven''t mentioned which RDBMS you use (I guess some from SQL Server family) and data types of your columns, therefore I can''t supply exact code, but I something like this code will work:
CREATE PROCEDURE Employee_Insert
(@Name nvarchar(50), --you should use data type as defined in table
@depid int)
BEGIN
AS
INSERT INTO Employee
(Name, depid)
VALUES
(@Name, @depid);
SELECT empid, Name, depid FROM Employee WHERE empid = SCOPE_IDENTITY();
END
这篇关于在foregin键列中插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!