Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on "insert" and then on "On" 1

Status
Not open for further replies.

willydude

Programmer
Oct 24, 2006
123
US
The following bit of code gives me the error message
“Incorrect syntax near the keyword 'Insert'.”
Code:
If @TimeSheet_PK_ID is null or 
	@TimeSheet_PK_ID = 0
Begin  
Select tbl_Clients_ClientBasicInfo.Client_PK_ID,
	tbl_Customers_CustomerBasicInfo.Customer_PK_ID,
	tbl_Jobs_JobNumber.JobNumber_PK_ID,
	tbl_Jobs_JobSteps.StepNumber_PK_ID

--Tables where above PK_ID's will come from

From 
tbl_Clients_ClientBasicInfo 
Join
tbl_Customers_CustomerBasicInfo 
Join
tbl_Jobs_JobNumber 
Join
tbl_Jobs_JobSteps 
ON
@ClientNumber = tbl_Clients_ClientBasicInfo.ClientNumber
and
@CustomerNumber = tbl_Customers_CustomerBasicInfo.CustomerNumber
and
@JobNumber = tbl_Jobs_JobNumber.JobNumber
and
@StepNumber = tbl_Jobs_JobSteps.StepNumber

--error pointing to Insert here
Insert into
tbl_TimeSheetEntry
(BatchNumber,
LocationNumber,
WorkDate,
Client_PK_ID,
Customer_PK_ID,
JobNumber_PK_ID,
StepNumber_PK_ID,
HoursWorked,
Units,
WagePaid,
PieceRate,
PrevailingWage,
FundingCode)

values
(@BatchNumber,
@LocationNumber,
@WorkDate,
@Client_PK_ID,
@Customer_PK_ID,
@JobNumber_PK_ID,
@StepNumber_PK_ID,
@HoursWorked,
@Units,
@WagePaid,
@PieceRate,
@PrevailingWage,
@FundingCode)
--Assign New TimeSheet_PK_ID to record just added
Set @TimeSheet_PK_ID = Scope_Identity ()
end


When I make changes to the code involving the From, Join and On, I get the error “Incorrect syntax near the keyword 'On'.”
Code:
--same as above
If @TimeSheet_PK_ID is null or 
	@TimeSheet_PK_ID = 0
Begin  --page 274
Select tbl_Clients_ClientBasicInfo.Client_PK_ID,
	tbl_Customers_CustomerBasicInfo.Customer_PK_ID,
	tbl_Jobs_JobNumber.JobNumber_PK_ID,
	tbl_Jobs_JobSteps.StepNumber_PK_ID

--this is where this bit of code is different from the first bit
Code:
From 
tbl_Clients_ClientBasicInfo ON  --error here
	@ClientNumber = tbl_Clients_ClientBasicInfo.ClientNumber
Join
tbl_Customers_CustomerBasicInfo ON
	@CustomerNumber = tbl_Customers_CustomerBasicInfo.CustomerNumber
Join
tbl_Jobs_JobNumber ON
	@JobNumber = tbl_Jobs_JobNumber.JobNumber
Join
tbl_Jobs_JobSteps ON
	@StepNumber = tbl_Jobs_JobSteps.StepNumber

Insert into
tbl_TimeSheetEntry
(BatchNumber,
LocationNumber,
WorkDate,
Client_PK_ID,
Customer_PK_ID,
JobNumber_PK_ID,
StepNumber_PK_ID,
HoursWorked,
Units,
WagePaid,
PieceRate,
PrevailingWage,
FundingCode)

values
(@BatchNumber,
@LocationNumber,
@WorkDate,
@Client_PK_ID,
@Customer_PK_ID,
@JobNumber_PK_ID,
@StepNumber_PK_ID,
@HoursWorked,
@Units,
@WagePaid,
@PieceRate,
@PrevailingWage,
@FundingCode)
--Assign New TimeSheet_PK_ID to record just added
Set @TimeSheet_PK_ID = Scope_Identity ()
end

Can anyone see from this why I am getting error messages and what I can do to stop them?

TIA,

Bill
 
First you have to understand that Query Analyzer isn't exact. The error may be in the code near that line.

The problem is really in your JOINS. JOINs work this way:

FROM Table1 t1
JOIN Table2 t2
ON t1.somecolumn = t2.somecolumn
JOIN Table3 t3
ON t2.somecolumn = t3.somecolumn

You must CONNECT two tables by a common column. You can use almost any type of operator (=, <>, >, <), but it MUST be between columns in the two tables at a minimum. You can also add other requirements....

FROM Table1 t1
JOIN Table2 t2
ON t1.somecolumn = t2.somecolumn
AND t1.somecolumn = 1
AND t2.somecolumn = 'April'

On this line, you have an ON but no JOIN...remember it's FROM .... JOIN .... ON ..... <repeat JOIN .... and ON ... as necessary)

From
tbl_Clients_ClientBasicInfo ON --error here
@ClientNumber = tbl_Clients_ClientBasicInfo.ClientNumber
Join



-SQLBill

Posting advice: FAQ481-4875
 
I’m still having a problem. It is not pulling the Client_PK_ID from the table “tbl_Clients_ClientBasicInfo”.

I’m thinking that I do not want to join “tbl_Clients_ClientBasicInfo” with the others, but I do not know how to accomplish my goal. Here is what I have just tried:

Code:
If @TimeSheet_PK_ID is null or 
	@TimeSheet_PK_ID = 0
Begin  
Select tbl_Clients_ClientBasicInfo.Client_PK_ID,
	tbl_Customers_CustomerBasicInfo.Customer_PK_ID,
	tbl_Jobs_JobNumber.JobNumber_PK_ID,
	tbl_Jobs_JobSteps.StepNumber_PK_ID

--Tables where above PK_ID's will come from

From
tbl_Clients_ClientBasicInfo as ClientInfo
join
tbl_Customers_CustomerBasicInfo ON
	@CustomerNumber = tbl_Customers_CustomerBasicInfo.CustomerNumber
join
tbl_Jobs_JobNumber ON
	@JobNumber = tbl_Jobs_JobNumber.JobNumber
join
tbl_Jobs_JobSteps ON
	@StepNumber = tbl_Jobs_JobSteps.StepNumber
join
tbl_Clients_ClientBasicInfo  ON
	@ClientNumber = tbl_Clients_ClientBasicInfo.ClientNumber

The table “tbl_Clients_ClientBasicInfo” does NOT have a field in common with the other three tables. But I still need to pull the field “Client_PK_ID” from this table.

The table “tbl_Jobs_JobSteps” (its PK is “StepNumber_PK_ID”) is in common with the table “tbl_Customers_CustomerBasicInfo” on the field “Customer_PK_ID”.

The table “tbl_Jobs_JobSteps” is ALSO in common with the table “tbl_Jobs_JobNumber” on the field “JobNumber_PK_ID”.

The table “tbl_Jobs_JobNumber” (PK is “JobNumber_PK_ID”)
is in common with the table “tbl_Customers_CustomerBasicInfo” on the field “Customer_PK_ID”.

 
The table “tbl_Clients_ClientBasicInfo” does NOT have a field in common with the other three tables. But I still need to pull the field “Client_PK_ID” from this table.

If it does not have a field in common, how were you planning on doing this?

You are still not joining your columns to other columns. If you want column to equal your variable, that should be handled in a WHERE clause. Joins should be done like this (as SQLBill pointed out)

Code:
select a.ID, b.ID
from table1 a 
join table2 b
on a.ID = b.ID

If you want the ID to equal a variable, do it like this:

Code:
select a.ID, b.ID
from table1 a 
join table2 b
on a.ID = b.ID
where a.ID = @ID

This is an oversimplified example, but I think it should be sufficient.

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
I think at this point we need some sample data. Please provide us your schema (tables and columns), some sample data, and the results you would like to see.

-SQLBill

Posting advice: FAQ481-4875
 
I have the following tables with the PK_ID’s, for the clients, job and step numbers and names and descriptions. The data entry person does NOT enter the PK_ID numbers, only the client, customer, job, step number and the hours worked.

Example: For Bob working for customer Pete’s Pets on job Sealing and step Folding, the data entry person would enter (into the proper fields): 1000, 36, 0786, 12 and for his hours, 5.50.

The SProc would look up the proper PK_ID’s for Bob’s number, the customer number, the job number and the step number and along with the number of hours, place these PK_ID’s in the Timesheet table. This is the first line in the timesheet table with TS_PK_ID of “1”.

I’m sure that if I place the client’s number (1000 for Bob) instead of their PK_ID (1 for Bob) that it will work, but from what I have read, I don’t believe that that is the “proper” way to store data.


Tbl_ClientInfo
Ct_PK_ID Number Name
1 1000 Bob
2 1234 Joe
5 1789 Helen
Etc…

Tbl_Customers
Cu_PK_ID Number Name
1 36 Pet’s Pets
4 38 Millie’s Mops
5 52 Randy’s
8 88 Candy’s Candy

Tbl_Jobs
Jo_PK_ID Cu_PK_ID Number Description
1 1 0786 sealing
5 1 3552 assembly
6 4 2296 boxing
7 5 5555 mailing

Tbl_StepsForJobs
St_PK_ID Jo_PK_ID Cu_PK_ID Number Description
2 1 1 12 fold
3 5 1 24 box
4 7 5 65 average
5 6 4 44 sorting

Tbl_TimeSheets
TS_PK_ID Ct_PK_ID Cu_PK_ID Jo_PK_ID St_PK_ID Hours
1 1 1 1 2 5.5
2 1 1 1 2 1.5
3 2 5 7 4 3.0
4 2 4 6 5 2.0


I had some issues trying to get the info to appear properly on the screen. Hope it's workable.
Thanks.
 
I think you need to put your stuff with the variables into a where clause. You should join on Cu_PK_ID between your tables, although I don't think you will be able to join to 'Tbl_ClientInfo'.

Is there a criteria for getting info from this table, based on one of your input parameters or other variables? You could possibly use a subquery to include the right info from there.

Good Luck,

Alex

Ignorance of certain subjects is a great part of wisdom
 
The only criteria for getting the Ct_PK_ID from the tablle tbl_ClientInfo is the client's number, ie. 1000.

I did not think of a sub query. will give it a try.

thanks.

 
After spending the day reading and typing, I'm not sure if I am any closer, or possibly farther, from wher I want to be. If anyone can help with the following code, I would be greatly appreciative. I have gotten the error messages down to one: --"Incorrect syntax near "ON"" for following line.. I have notes within the code below.

Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[TestingforPullingPK_IDDataFromMultipleTables]
(@BatchNumber varchar (2),
@LocationNumber varchar (2), --<<does not need a PK_ID
@WorkDate datetime,
@ClientNumber varchar (4),
@CustomerNumber varchar (3),
@JobNumber varchar (4),
@StepNumber varchar (2),
@Client_PK_ID int output,
@Customer_PK_ID int output,
@JobNumber_PK_ID int output,
@StepNumber_PK_ID int output,
@HoursWorked numeric (5,2),
@Units numeric (5,1),
@WagePaid numeric (4,2) output,
@PieceRate numeric (10, 5) output,
@PrevailingWage numeric (4, 2) output,
@FundingCode varchar (2) output,
@Timesheet_PK_ID int output,
@Message varchar (30))
as
declare
@Continue bit

set @Continue = 1
set @Message = ''
set @Client_PK_ID = 0
set @Customer_PK_ID = 0
set @JobNumber_PK_ID = 0
set @StepNumber_PK_ID = 0
set @Timesheet_PK_ID = 0

if @Timesheet_PK_ID is null or
	@TimeSheet_PK_ID = 0

--following is the most OUTER part of the query
  --the Client table is not related to the 
	--Customer, Job or Step tables. It IS related to the
	--timesheet table
begin
--getting Client_PK_ID from Client info table
select
tbl_Clients_ClientBasicInfo.Client_PK_ID
from
tbl_Clients_ClientBasicInfo
where @ClientNumber = tbl_Clients_ClientBasicInfo.ClientNumber
--(  --<<<<parenthisis for sub query. don't seem to need it.

--trying to get the Customer and Job PK_ID's. 
--At this point, the SProc does not know the Customer_PK_ID
--or the JobNumber_PK_ID because the @CustomerNumber 
--and @JobNumber parameters have not been processed.
--They are entered below.

begin  --<<<Needed?
select
tbl_Customers_CustomersBasicInfo.Customer_PK_ID,
	tbl_Jobs_JobNumber.JobNumber_PK_ID
from
tbl_Customers_CustomersBasicInfo
	
--"Incorrect syntax near "ON"" for following line.
    on @CustomerNumber =  
	tbl_Customers_CustomersBasicInfo.CustomerNumber
	
	join
	tbl_Jobs_JobNumber
	on
	@JobNumber = tbl_Jobs_JobNumber.JobNumber_PK_ID
	end
--)	--<<<parenthesis ending subquery

   --must use Customer_PK_ID and JobNumber_PK_ID
   --along with the StepNumber (NOT StepNumber_PK_ID) and
	--LocationNumber to obtaind the StepNumber_PK_ID
	--from the Steps table. All four are needed for
	--entry into the timeSheet table

select
@StepNumber_PK_ID 
from
tbl_Jobs_JobSteps 
--comparing the Cust and Job PK_ID's obtained above
to those that are in the step table

where @Customer_PK_ID = tbl_Jobs_JobSteps.Customer_PK_ID 
AND @JobNumber_PK_ID = tbl_Jobs_JobSteps.JobNumber_PK_ID 
AND @StepNumber = tbl_Jobs_JobSteps.StepNumber 
AND @LocationNumber = tbl_Jobs_JobSteps.LocationNumber

--for inserting data 
Insert into tbl_TimeSheetEntry
(BatchNumber,
LocationNumber,
WorkDate,
Client_PK_ID,  --obtained from ClientBasicInfo table
Customer_PK_ID, --obtained from Customer table
JobNumber_PK_ID,  --obtained from Job table
StepNumber_PK_ID,  --obtainded from Steps table
HoursWorked,
Units,
WagePaid,
PieceRate,
PrevailingWage,
FundingCode)

values
(@BatchNumber,
@LocationNumber,
@WorkDate,
@Client_PK_ID,
@Customer_PK_ID,
@JobNumber_PK_ID,
@StepNumber_PK_ID,
@HoursWorked,
@Units,
@WagePaid,
@PieceRate,
@PrevailingWage,
@FundingCode)

--Assign New TimeSheet_PK_ID to record just added
Set @TimeSheet_PK_ID = Scope_Identity ()
end

 
Problem is the order in which your ON statement appears (and you are also still trying to join to a variable). Join syntax needs to be like this:

Code:
select zeTable.ID, zeOtherTable.SomeField
from zeTable
[b]JOIN
zeOtherTable
ON
zeTable.ID = zeOtherTable.ID[/b]

Note how the 'ON' portion includes one field from each table. This is what the join is built on. You CANNOT join to a variable. You can use a variable within your where clause. Using the same query as above, that could be done like this:

Code:
select zeTable.ID, zeOtherTable.SomeField
from zeTable
[b]JOIN
zeOtherTable
ON
zeTable.ID = zeOtherTable.ID
[b]WHERE zeOtherTable.SomeField = @zeVariable[/b]

Hope this helps,

Alex



Ignorance of certain subjects is a great part of wisdom
 
Alex,

The Clients table does not have anything in common with the Customer, Jobs, and JobSteps tables to build the Join UNTIL AFTER the new timesheet record is “assembled” and entered into the timesheet table, then they do have something in common, the new TimeSheet_PK_ID.

I need to assign to a variable the Customer_PK_ID from the Customer table based upon the @CustomerNumber parameter.

I need to assign to a variable the Job_PK_ID from the Jobs table based upon the @JobNumber parameter.

With the two PK_ID values from above AND with the @StepNumber value (NOT a PK_ID) and the @LocationNumber value (also not a PK_ID value) I need to pull the STEP_PK_ID from the JobSteps table based upon the four values. If the JobStep has been properly set up, all field values will exist. If not, I need an error message.

I know that its basically saying “When I have a JobStep table record that has all of the requested field values (Customer_PK_ID, Job_PK_ID, @StepNumber value and the @LocationNumber value) then give me the STEP_PK_ID from the JobSteps table for the record that has all four values.”

After getting the above, I then need to place into the TimeSheet table: the Customer_PK_ID, Job_PK_ID, STEP_PK_ID and the @LocationNumber value ALONG with the Client_PK_ID from the Client table based on the @ClientNumber parameter.

If all goes well, then a new TimeSheet_PK_ID is assigned to the new record.

All six values will then make up the timesheet record.

Note:
Getting the Client_PK_ID from the Client table based on the @ClientNumber parameter is not a problem.

Getting the Customer_PK_ID, Job_PK_ID, STEP_PK_ID and the @LocationNumber from the JobStep table is not much of a problem.

The problem is getting all five values by using only one query.

TIA.
Bill
 
Dude, I think you are drifting off course.

Here is what I would do.

1. Get CLIENT_PK_ID (based on your criteria) and store it in a variable.
2. Rather than joining to the Client Info table, you will select this value in your select. So it goes something like this (based on your original query - I am assuming all the table names in your original query are correct, and correspond to *similar* table names in your sample data):

Code:
--store PK_ID in a variable
declare @myPKid varchar(1000)
select @myPKid = Client_PK_ID from tbl_ClientInfo where Number = @ClientNumber

--run your original query with modifications
If @TimeSheet_PK_ID is null or 
    @TimeSheet_PK_ID = 0
Begin  
Select @myPKid,
    tbl_Customers_CustomerBasicInfo.Customer_PK_ID,
    tbl_Jobs_JobNumber.JobNumber_PK_ID,
    tbl_Jobs_JobSteps.StepNumber_PK_ID

From

tbl_Customers_CustomerBasicInfo 
join
tbl_Jobs_JobNumber 
ON
tbl_Customers_CustomerBasicInfo.CU_PK_ID = tbl_Jobs_JobNumber.CU_PK_ID
join
tbl_Jobs_JobSteps ON
    tbl_Customers_CustomerBasicInfo.CU_PK_ID = tbl_Jobs_JobSteps.CU_PK_ID


WHERE

    tbl_Customers_CustomerBasicInfo.CustomerNumber = @CustomerNumber 
AND
    tbl_Jobs_JobNumber.JobNumber = @JobNumber 
AND
    tbl_Jobs_JobSteps.StepNumber = @StepNumber 


END

Hope it helps,

Alex


Ignorance of certain subjects is a great part of wisdom
 
Thanks. I'll work on it tomorrow. My brain is a bit fried now.

Thanks again.

Bill
 
I have made the adjustments to my code, which is below. When I run the SProc, I get some partial results, but I also get the error message: “The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_TimesheetEntry_tbl_Jobs_JobSteps". The conflict occurred in database "ClientPayroll", table "dbo.tbl_Jobs_JobSteps".

I have checked my spelling of the column names, checked that there is a relationship between tbl_TimeSheets and tbl_Jobs_JobSteps, checked the data type and size of the StepNumber_PK_ID field and can see nothing wrong. (But I’ve been looking at this for quite a while and might not be seeing the obvious.)

In the Results pane, there are three rows with column names. The first row is showing the Client_PK_ID, the Customer_PK_ID, the JobNumber_PK_ID and the StepNumber_PK_ID. All four columns have the correct PK_ID’s based upon the Client, Customer, Job and Step Numbers. This tells me that the PK_ID’s are being accessed and called properly.

The second row in the results pane is showing the column names for the @Client_PK_ID, the @Customer_PK_ID, the @JobNumber_PK_ID, the @StepNumber_PK_ID and some other information that the user would enter, such as the piece rate, PrevailingWage, etc. There is also a column name for the @TimeSheet-PK_ID.

The @Client_PK_ID has the proper PK_ID based upon the value of the ClientNumber that was entered. That’s good. But the @Customer_PK_ID, the @JobNumber_PK_ID and the @StepNumber_PK_ID have the value “0”.

The third row has the Ruturn column name with the value “-6”.

Just to see what would happen, I set the @Customer_PK_ID, the @JobNumber_PK_ID, the @StepNumber_PK_ID and the @TimeSheet_PK_ID variables in my code to “1”, instead of “0”. The SProc ran without errors and only two rows where returned in the Results pane.

The first row has the @Client_PK_ID with the proper value. The @Customer_PK_ID, the @JobNumber_PK_ID, @StepNumber_PK_ID all have the value of “1”. This tells me that I am not selecting the PK_ID’s for these fields. @TimeSheet_PK_ID is also “1”. I checked and refreshed my Timesheet table and NO records had been entered. The Return value is “0”.

Also NOT apearing in the Results are the column names and related data for the @BatchNumber, the @LocationNumber, the @WorkDate, the @Hours, and the @Units. But there ARE column names for the @WagePaid, @PieceRate, @PrevailingWage and @FundingCode variables.

TIA,

Bill
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[TestingforPullingPK_IDDataFromMultipleTables2]
(@BatchNumber varchar (2),
@LocationNumber varchar (2), --<<does not need a PK_ID
@WorkDate datetime,
@ClientNumber varchar (4),
@CustomerNumber varchar (3),
@JobNumber varchar (4),
@StepNumber varchar (2),
@Client_PK_ID int output,
@Customer_PK_ID int output,
@JobNumber_PK_ID int output,
@StepNumber_PK_ID int output,
--@ClientTimeStudy_PK_ID int output,
@HoursWorked numeric (5,2),
@Units numeric (5,1),
@WagePaid numeric (4,2) output,
@PieceRate numeric (10, 5) output,
@PrevailingWage numeric (4, 2) output,
@FundingCode varchar (2) output,
@Timesheet_PK_ID int output,
@Message varchar (30))
as
declare
@Continue bit

set @Continue = 1
set @Message = ''
set @Client_PK_ID = 0
set @Customer_PK_ID = 0
set @JobNumber_PK_ID = 0
set @StepNumber_PK_ID = 0
--set @ClientTimeStudy_PK_ID = 0
set @Timesheet_PK_ID = 0

--store Client_PK_ID in a variable BEFORE selecting other data
select @Client_PK_ID = Client_PK_ID 
from tbl_Clients_ClientBasicInfo 
where ClientNumber = @ClientNumber

if @Timesheet_PK_ID is null or
	@TimeSheet_PK_ID = 0
begin
Select @Client_PK_ID AS Client_PK_ID,
    tbl_Customers_CustomerBasicInfo.Customer_PK_ID,
    tbl_Jobs_JobNumber.JobNumber_PK_ID,
    tbl_Jobs_JobSteps.StepNumber_PK_ID
From
tbl_Customers_CustomerBasicInfo 
join
tbl_Jobs_JobNumber 
ON
tbl_Customers_CustomerBasicInfo.Customer_PK_ID = 
		tbl_Jobs_JobNumber.Customer_PK_ID
join
tbl_Jobs_JobSteps 
ON
tbl_Customers_CustomerBasicInfo.Customer_PK_ID = 
	tbl_Jobs_JobSteps.Customer_PK_ID
where  tbl_Customers_CustomerBasicInfo.CustomerNumber = 
		@CustomerNumber 
AND
    tbl_Jobs_JobNumber.JobNumber = @JobNumber 
AND
    tbl_Jobs_JobSteps.StepNumber = @StepNumber 
--need ClientTimeStudy_PK_ID value pulled, if it exists
--for inserting data 

Insert into tbl_TimeSheetEntry
(BatchNumber,
LocationNumber,
WorkDate,

Client_PK_ID,
Customer_PK_ID,
JobNumber_PK_ID,
StepNumber_PK_ID,
--ClientTimeStudy_PK_ID,
HoursWorked,
Units,
WagePaid,
PieceRate,
PrevailingWage,
FundingCode)

values
(@BatchNumber,
@LocationNumber,
@WorkDate,

@Client_PK_ID,
@Customer_PK_ID,
@JobNumber_PK_ID,
@StepNumber_PK_ID,
--@ClientTimeStudy_PK_ID,

@HoursWorked,
@Units,
@WagePaid,
@PieceRate,
@PrevailingWage,
@FundingCode)

--Assign New TimeSheet_PK_ID to record just added
Set @TimeSheet_PK_ID = Scope_Identity ()
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top