sql server连接查询存储过程 [英] sql server join query for stored procedure
问题描述
先生/女士,我有两个表Employee和Department我通过主键和外键联接.这是两个示例表
雇员
empid(pk)
姓名
depid(fk)
部门
depid(pk)
部门名称
因为我已经插入了所有部门名称(部门名称),但是当我从员工Windows窗体插入时,我从组合框中选择部门名称,并希望从名称中插入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) but when i insert from employee windows form i select department name from combo box and want to insert depid(fk) from name. so please tell me how will be the query in stored procedure for inserting data of employee. (for update also when employee is updated)
Thanking you
推荐答案
首先插入部门ID和名称.
然后,您可以按照此处中所述使用@@Identity
获取标识列值. [ ^ ].
一旦有了这个,您就可以在employee表中插入一行(带有外键).
如果希望部门和员工都一起插入,则将所有这些事务都保留在一个事务中.
Insert the department id and name first.
You can then get the identity column value using@@Identity
as described here[^].
Once you have this, you can insert a row into your employee table (with the foreign key).
Keep all of this on one transaction if you want both department and employee to be inserted together.
嘿,
在DropDownlist中用值填充Department并存储ddl.SelectedValue而不是text
祝您好运
Hey,
Fill Department with value in DropDownlist and store ddl.SelectedValue instead text
Best luck
您告诉过您的表结构就是这样.
员工部
empid(pk)depid(pk)
名字Deptname
depid(fk)
只需编写以下查询,即可将数据插入表中.
As you told your structure of the tables is like this.
Employee Department
empid(pk) depid(pk)
name Deptname
depid(fk)
Just write the following queries to insert the data in the table.
Declare @Id int
Insert into Department values(1, 'DptName');
set @Id=@@Identity
Insert into Employee values(1, 'Test', @Id)
试试吧..最好的..
Try this.. All the best..
这篇关于sql server连接查询存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!