I am using SQL Server Express 2005.
I have verifed that a valid record with a JobNumber_PK_ID does exist in my JobNumber table and am able to properly enter info through the table, but I keep getting the classic message below when running the sproc.
Msg 547, Level 16, State 0, Procedure Jobs_Insert_NewJobSteps, Line 255
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_Jobs_JobSteps_tbl_Jobs_JobNumber". The conflict occurred in database "ClientPayroll", table "dbo.tbl_Jobs_JobNumber", column 'JobNumber_PK_ID'.
The statement has been terminated.
I believe the problem is in the area of the code where I select the Customer_PK_ID and the JobNumber_PK_ID. There is some validation before getting to this spot. The
overall sproc process explanation is after the code.
Some info after running the sproc:
The Results grid/pane shows the correct Customer_PK_ID,
nothing in the JobNumber_PK_ID,
the @StepNumber_PK_ID is NULL,
the @JobNumber_PK_ID and the @Customer_PK_ID are 0 for each.
@Message is empty.
--
The Message pane shows the error message above.
Quite a bit of validation begins. I do not believe that the problem is in this area.
All data is validated at this point. Begin SELECT and INSERT process.
--PROBLEM STARTS HERE??
--PROBLEM ENDS HERE??
the following puts all the info into the JobsJobSteps table
The Customer_PK_ID and the LocationNumber do not need to be
in the JobSteps table b/c the JobNumber_PK_ID is already
composed of the Customer_PK_ID, the Job Number enterd by the user and the Location Number entered by the user.
Explanation of overall sproc process.
tbl_JobsCustomerBasicInfo Table
The customer must already be set up in the table,
"tbl_JobsCustomerBasicInfo", before their job(s) and the
job steps can be set up in the
tables, "tbl_JobsJobNumber"
and tblJobsJobSteps.
Each customer will have a unique data entry
ID, "CustomerNumber", such as 12, 36,etc. that is
assigned by the data entry person.
This number is entered into
the "tbl_JobsCustomerBasicInfo".
SQL will assign a unique Auto-Incr Identity number in the --column
Customer_PK_ID.
tbl_JobsJobNumber table
The data entry person enters a unique "JobNumber" for the job, such as 0212, 0786, 2633, etc. SQL assigns a unique Auto-Incr Identity number in the column "JobNumber_PK_ID".
A particular Customer/Job combination can exist only once in any one location. Example: Customer 36, Job 0786, Location 1 cannot exist in Location 1 twice, but Customer 36, Job 0786, Location 2 is OK.
Continuing with the Jobs_JobNumber table, upon successful
validation of data entered by the data
entry person, SQL will pull the "Customer_PK_ID" Auto-Incr
Identtity number from the tbl_JobsCustomerBasicInfo table
and place it in the table "tbl_JobsJobNumber" in the column
Customer_PK_ID. THIS INSERT WORKS.
tbl_JobsJobSteps. THIS IS THE INSERT NOT WORKING. CODE ABOVE.
The data entry person enters the Customer, Job, Step
and Location Numbers. The sproc should pull the JobNumber_PK_ID
from the tbl_Jobs_JobNumber. This is determined by the
@CustomerNumber value that selects the Customer_PK_ID from
the CustomerBasicInfo table. This PK_ID is assigned to the variable
Customer_PK_ID in this sproc.
The code then SHOULD select the JobNumber_PK_ID from the JobNumber table based upon the Customer_PK_ID, the @JobNumber and the @LocationNumber. This JobNumber_PK_ID should then be placed into the JobSteps table. The Customer_PK_ID does not need to be in the JobSteps table b/c the JobNumber_PK_ID already reflects this value.
--
--There is currently no error handling.
--There is currently no Transaction, Rollback, Commit code
--
--Right now, trying to get the PK's, unique, and processes to work.
I have verifed that a valid record with a JobNumber_PK_ID does exist in my JobNumber table and am able to properly enter info through the table, but I keep getting the classic message below when running the sproc.
Msg 547, Level 16, State 0, Procedure Jobs_Insert_NewJobSteps, Line 255
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_Jobs_JobSteps_tbl_Jobs_JobNumber". The conflict occurred in database "ClientPayroll", table "dbo.tbl_Jobs_JobNumber", column 'JobNumber_PK_ID'.
The statement has been terminated.
I believe the problem is in the area of the code where I select the Customer_PK_ID and the JobNumber_PK_ID. There is some validation before getting to this spot. The
overall sproc process explanation is after the code.
Some info after running the sproc:
The Results grid/pane shows the correct Customer_PK_ID,
nothing in the JobNumber_PK_ID,
the @StepNumber_PK_ID is NULL,
the @JobNumber_PK_ID and the @Customer_PK_ID are 0 for each.
@Message is empty.
--
The Message pane shows the error message above.
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Jobs_Insert_NewJobSteps]
(@CustomerNumber varchar (3),
@JobNumber varchar (4),
@StepNumber varchar (2),
@LocationNumber varchar (2),
@StepCode varchar (2),
@StepDescription varchar (50),
@PrevailingWage numeric (5,2),
@Standard Numeric (10,1),
@GuaranteedStepWage numeric (5,2),
@StepNumber_PK_ID int output,
@JobNumber_PK_ID int output,
@Customer_PK_ID int output,
@Message varchar (30) output)
as
--declare variables
declare @Continue bit,
@CustomerNumber_Required bit
set @Continue = 1
set @JobNumber_PK_ID = 0
set @Customer_PK_ID = 0
--set @JobNumber = 0 --have tried this with NULL also.
--Neither works.
set @LocationNumber = 0
set @Message = ''
--SET @PrevailingWage = null
--set @Standard = null
--set @GuaranteedStepWage = null
Code:
--validate that the customer number exists in the parameter
--(not NULL or empty
if @CustomerNumber is null or datalength(Ltrim(@CustomerNumber)) = 0
begin
set @Message = 'Customer Number Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@CustomerNumber as varchar (3))) < 1 or
datalength(cast(@CustomerNumber as varchar (3))) > 3
begin
set @Message = 'Wrong Length of Customer Number'
set @Continue = 0
end
--validate the job number
if @JobNumber is null or datalength(Ltrim(@JobNumber)) = 0
begin
set @Message = 'Job Number Required'
set @Continue = 0
end
--then check its length. Must be 4, include leading 0, i.e. 0786
if datalength(cast(@JobNumber as varchar (4))) <> 4
begin
set @Message = 'Wrong Length of Job Number'
set @Continue = 0
end
--validate the Step number
if @StepNumber is null or datalength(Ltrim(@StepNumber)) = 0
begin
set @Message = 'Step Number Required'
set @Continue = 0
end
--then check its length. Must be 2, include leading 0, i.e. 07
if datalength(cast(@StepNumber as varchar (2))) <> 2
begin
set @Message = 'Wrong Length of Step Number'
set @Continue = 0
end
--validate Location
if @LocationNumber is null or datalength(Ltrim(@LocationNumber)) = 0
begin
set @Message = 'Location Number Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@LocationNumber as varchar (2))) < 1 or
datalength(cast(@LocationNumber as varchar (2))) > 2
begin
set @Message = 'Wrong Length of Location Number'
set @Continue = 0
end
--validate Step Code
if @StepCode is null or datalength(Ltrim(@StepCode)) = 0
begin
set @Message = 'Step Code Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@StepCode as varchar (2))) < 1 or
datalength(cast(@StepCode as varchar (2))) > 2
begin
set @Message = 'Wrong Length of Step Code'
set @Continue = 0
end
--validate description
if @StepDescription is null or datalength(@StepDescription) = 0
begin
set @Message = 'Description Required'
set @Continue = 0
end
--validate PrevailingWage
if @PrevailingWage is null or datalength(Ltrim(@PrevailingWage)) = 0
begin
set @Message = 'Prevailing Wage Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@PrevailingWage as numeric (5,2))) < 1 or
datalength(cast(@PrevailingWage as numeric (5,2))) > 5
begin
set @Message = 'Check prevailing Wage Value'
set @Continue = 0
end
else
begin
set @PrevailingWage = null
end
--end
--
--Enter and validate Standard
--Add If statement for PR?
if @StepCode = 'PR'
begin
if @Standard is null or datalength(Ltrim(@Standard)) = 0
begin
set @Message = 'Standard Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@Standard as varchar (6))) < 1 or
datalength(cast(@Standard as varchar (6))) > 6
begin
set @Message = 'Check Value of Standard'
set @Continue = 0
end
else
begin
set @Standard = null
end
end
--validate GuaranteedStepWage (5,2)
if @GuaranteedStepWage is null or
datalength(Ltrim(@GuaranteedStepWage)) = 0
begin
set @Message = 'Guaranteed Step Wage Required'
set @Continue = 0
end
--then check its length
if datalength(cast(@GuaranteedStepWage as numeric (5,2))) < 1 or
datalength(cast(@GuaranteedStepWage as numeric (5,2))) > 5
begin
set @Message = 'Check Guaranteed Step Wage Value'
set @Continue = 0
end
else
begin
set @GuaranteedStepWage = null
end
--end
Code:
if @Continue = 0
begin
return 1
end
else
if @StepNumber_PK_ID is null or
@StepNumber_PK_ID = 0
begin
Code:
select Customer_PK_ID
from
tbl_Customers_CustomerBasicInfo
where
CustomerNumber = @CustomerNumber
select JobNumber_PK_ID
from
tbl_Jobs_JobNumber
where
Customer_PK_ID = Customer_PK_ID
and
tbl_Jobs_JobNumber.JobNumber = @JobNumber
and
tbl_Jobs_JobNumber.LocationNumber = @LocationNumber
--order by
the following puts all the info into the JobsJobSteps table
The Customer_PK_ID and the LocationNumber do not need to be
in the JobSteps table b/c the JobNumber_PK_ID is already
composed of the Customer_PK_ID, the Job Number enterd by the user and the Location Number entered by the user.
Code:
insert into tbl_Jobs_JobSteps
(JobNumber_PK_ID,
StepNumber,
StepDescription,
StepCode,
PrevailingWage,
Standard,
GuaranteedStepWage
--StartDate
)
values
(@JobNumber_PK_ID,
@StepNumber,
@StepDescription,
@StepCode,
@PrevailingWage,
@Standard,
@GuaranteedStepWage
)
--assigns new StepNumber_PK_ID to new record just added.
set @StepNumber_PK_ID = scope_identity ()
end
tbl_JobsCustomerBasicInfo Table
The customer must already be set up in the table,
"tbl_JobsCustomerBasicInfo", before their job(s) and the
job steps can be set up in the
tables, "tbl_JobsJobNumber"
and tblJobsJobSteps.
Each customer will have a unique data entry
ID, "CustomerNumber", such as 12, 36,etc. that is
assigned by the data entry person.
This number is entered into
the "tbl_JobsCustomerBasicInfo".
SQL will assign a unique Auto-Incr Identity number in the --column
Customer_PK_ID.
tbl_JobsJobNumber table
The data entry person enters a unique "JobNumber" for the job, such as 0212, 0786, 2633, etc. SQL assigns a unique Auto-Incr Identity number in the column "JobNumber_PK_ID".
A particular Customer/Job combination can exist only once in any one location. Example: Customer 36, Job 0786, Location 1 cannot exist in Location 1 twice, but Customer 36, Job 0786, Location 2 is OK.
Continuing with the Jobs_JobNumber table, upon successful
validation of data entered by the data
entry person, SQL will pull the "Customer_PK_ID" Auto-Incr
Identtity number from the tbl_JobsCustomerBasicInfo table
and place it in the table "tbl_JobsJobNumber" in the column
Customer_PK_ID. THIS INSERT WORKS.
tbl_JobsJobSteps. THIS IS THE INSERT NOT WORKING. CODE ABOVE.
The data entry person enters the Customer, Job, Step
and Location Numbers. The sproc should pull the JobNumber_PK_ID
from the tbl_Jobs_JobNumber. This is determined by the
@CustomerNumber value that selects the Customer_PK_ID from
the CustomerBasicInfo table. This PK_ID is assigned to the variable
Customer_PK_ID in this sproc.
The code then SHOULD select the JobNumber_PK_ID from the JobNumber table based upon the Customer_PK_ID, the @JobNumber and the @LocationNumber. This JobNumber_PK_ID should then be placed into the JobSteps table. The Customer_PK_ID does not need to be in the JobSteps table b/c the JobNumber_PK_ID already reflects this value.
--
--There is currently no error handling.
--There is currently no Transaction, Rollback, Commit code
--
--Right now, trying to get the PK's, unique, and processes to work.