Insert foreign key values using stored procedure in target table in SQL Server -


i have 2 tables this:

create table tblempdetail (      id int primary key,      name nvarchar(20),      email nvarchar(20) unique ) 

and:

create table tblemployee (     empid int,     month nvarchar(20),     year nvarchar(20),     salary int ) 

i have given reference as:

alter table tblemployee      add constraint tblemployee_empid_fk      foreign key (empid) references tblempdetail(id) 

i want create procedure fill values in tblemployee... empid should filled automatically taking reference tblempdetail

first make tblempdetail.id identity column

create table tblempdetail  (id int identity primary key,    name nvarchar(20),    email nvarchar(20) unique) 

then

create procedure saveemployee   @name nvarchar(20) = null,   @email nvarchar(20) = null,   @month nvarchar(20) = null,   @year nvarchar(20) = null,   @salary int = null  set nocount on  declare @id int      insert tblempdetail(name, email)      values(@name, @email)     set @id = scope_identity()      insert tblemployee (empid, month, year, salary)     values( @id, @month, @year, @salary)  return 0 

however, let me suggest read , research comment @marc_s above. schema design poor, , small amount of research proper design (varchars dates or portions of dates bad) save great deal of heartache later on.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -