Invalid column name - or so it says

I wrote a query but management studio says
Msg 207, Level 16, State 1, Line 13
Invalid column name 'JobID'.
Msg 207, Level 16, State 1, Line 13
Invalid column name 'Rig'.
Msg 207, Level 16, State 1, Line 13
Invalid column name 'SF_Rental_Days'.
Msg 207, Level 16, State 1, Line 13
Invalid column name 'WF_Rental_Days'.

and the error is on the insert into(JobID,Company,Rig,SF_Rental_Days,WF_Rental_Days,Total)  - I dont get it?

IF Object_id('Temp_x', 'U') IS NOT NULL
 DROP Table Temp_x;  
 CREATE Table Temp_x 
 JobID int,
 Company varchar(60),
 Rig varchar(60),
 SF_Rental_Days int,
 WF_Rental_Days int,
 Total int
  INSERT INTO Temp_x(JobID,Company,Rig,SF_Rental_Days,WF_Rental_Days,Total)
            SELECT Job.JobID AS JobID,
   As Company, 
            (''+Job.rig+' '+Job.rignumber) AS Rig, 
            sfrentaldays as SF_Rental_Days,
            wfrentaldays as WF_Rental_Days,
            (sfrentaldays * sfrentalrate + wfrentaldays * wfrentalrate + rigupcharge + rigdowncharge + numberoftools * toolredresscharge + sfinsurancedays * sfinsurancerate + wfinsurancedays * wfinsurancerate + servicemandays * servicemanrate - creditdays * creditrate + othercharges + SFStandByDays * SFStandByRate + WFStandByDays * WFStandByRate) AS Total 
            FROM   Job 
            INNER JOIN JobRentalTicket 
            ON Job.Jobid = JobRentalTicket.Jobid 
            WHERE  Job.Jobcomplete  = 1  
            AND JobRentalTicket.invictussentinvoicetoagent  = 0  
            AND Job.offlocationdatetime BETWEEN 
            '2012-01-01 00:00:00' AND '2015-12-31 00:00:00'  
            ORDER  BY Job.JobID; 
            Select * From Temp_x

Open in new window

Who is Participating?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
It is the INSERT. Add a GO after the CREATE TABLE, so the parser restarts checking for valid columns after each GO.
Dale FyeCommented:
I've found that the line number in the error messages can be mis-leading.

Have you tried just running the SELECT portion of the SQL?

You might want to explicitly identify where each of the fields is coming from (Job or JobRentalTicket).  

you also might want to use the ISNULL( ) function on line 18 to ensure that none of the values is NULL resulting in an overall NULL  value
r3nderAuthor Commented:
I found the issue - it seems the table is not actually being dropped - so when I used it last it had different columns - now I have to figure out why
r3nderAuthor Commented:
Thanks for the help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.