Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 4 tables are joind
SQL
[dbo].[PersonelInfo](
	[pID] [int] IDENTITY(1,1) NOT NULL,
	[PersonelCode] [varchar](50) NOT NULL)



SQL
[dbo].[EmploymentInfo](
	[empID] [int] IDENTITY(1,1) NOT NULL,
	[FK_pID] [int] NOT NULL)
ALTER TABLE [dbo].[EmploymentInfo]  WITH CHECK ADD  CONSTRAINT [FK_EmploymentInfo_PersonelInfo] FOREIGN KEY([FK_pID])
REFERENCES [dbo].[PersonelInfo] ([pID])


SQL
[dbo].[ICardDate](
	[FK_empID] [int] NOT NULL,
	[Imonth] [int] NOT NULL,
	[Iyear] [bigint] NOT NULL)
ALTER TABLE [dbo].[ICardDate]  WITH CHECK ADD  CONSTRAINT [FK_ICardDate_EmploymentInfo] FOREIGN KEY([FK_empID])
REFERENCES [dbo].[EmploymentInfo] ([empID])


SQL
[dbo].[PersonelCard](
	[pcID] [int] IDENTITY(1,1) NOT NULL,
	[FK_empID] [int] NOT NULL,
	[FK_month] [int] NOT NULL,
	[FK_year] [bigint] NOT NULL,
	[VacationHours] [time](7) NULL)
ALTER TABLE [dbo].[PersonelCard]  WITH CHECK ADD  CONSTRAINT [FK_PersonelCard_ICardDate] FOREIGN KEY([FK_empID], [FK_month], [FK_year])
REFERENCES [dbo].[ICardDate] ([FK_empID], [Imonth], [Iyear])
GO


now I want add a row to [dbo].[PersonelCard] base on my storedprocedure input parameters.

SQL
CREATE PROCEDURE [dbo].[InsertRowInPersonelCard]
	(@personelcode varchar (50),
	@fk_month int,
	@fk_year bigint,
	@vacationhours time(7))
as
--question:I dont know how find related FK_empID through @personelcode
insert into PersonelCard(FK_empID,FK_month,FK_year,VacationHours)
	Values(@empID,@fk_month,@fk_year,@vacationhours)

--the same value of @empID,@fk_month,@fk_year should insert in this table too
insert into ICardDate(FK_empID,Imonth,Iyear)
	        Values(@empID,@fk_month,@fk_year)

GO
Posted
Comments
mit62 8-Feb-14 5:44am    
isn't question clear?
abhijitmalkar 8-Feb-14 5:51am    
CREATE PROCEDURE [dbo].[InsertRowInPersonelCard]
(@personelcode varchar (50),
@fk_month int,
@fk_year bigint,
@vacationhours time(7))
as
DECALRE @empID INT

SELECT @empID=empID FROM EmploymentInfo WHERE FK_pID =(SELECT pID FROM PersonelInfo WHERE FK_pID=@personelcode)

insert into PersonelCard(FK_empID,FK_month,FK_year,VacationHours)
Values(@empID,@fk_month,@fk_year,@vacationhours)

insert into ICardDate(FK_empID,Imonth,Iyear)
Values(@empID,@fk_month,@fk_year)

1 solution

CREATE PROCEDURE [dbo].[InsertRowInPersonelCard]
(@personelcode varchar (50),
@fk_month int,
@fk_year bigint,
@vacationhours time(7))
as
DECALRE @empID INT

SELECT @empID=empID FROM EmploymentInfo WHERE FK_pID =(SELECT pID FROM PersonelInfo WHERE FK_pID=@personelcode)

insert into PersonelCard(FK_empID,FK_month,FK_year,VacationHours)
Values(@empID,@fk_month,@fk_year,@vacationhours)

insert into ICardDate(FK_empID,Imonth,Iyear)
Values(@empID,@fk_month,@fk_year)
 
Share this answer
 
Comments
mit62 8-Feb-14 6:29am    
query returns a minus value
DECLARE @return_value int

EXEC @return_value = [dbo].[InsertCa]
@personelcode = N'21',
@fk_month = 1,
@fk_year = 1395,
@vacationhours = '06:05:50'

SELECT 'Return Value' = @return_value

GO
Return Value:-6
abhijitmalkar 8-Feb-14 6:59am    
@return_value is integer variable and you pass a table name this is in a string.
mit62 8-Feb-14 7:25am    
this is not my code. this code is generated when I execute StoredProcedure
abhijitmalkar 8-Feb-14 7:40am    
I HAVE TO SEE STORE PROC
mit62 8-Feb-14 11:21am    
CREATE PROCEDURE [dbo].[InsertRowInPersonelCard]
(@personelcode varchar (50),
@fk_month int,
@fk_year bigint,
@vacationhours time(7))
as
DECALRE @empID INT

SELECT @empID=empID FROM EmploymentInfo WHERE FK_pID =(SELECT pID FROM PersonelInfo WHERE FK_pID=@personelcode)

insert into PersonelCard(FK_empID,FK_month,FK_year,VacationHours)
Values(@empID,@fk_month,@fk_year,@vacationhours)

--the same value of @empID,@fk_month,@fk_year should insert in this table too
insert into ICardDate(FK_empID,Imonth,Iyear)
Values(@empID,@fk_month,@fk_year)

GO

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900