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,
            Job.company 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

LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCCommented:
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
0
Qlemo"Batchelor", 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
0
r3nderAuthor Commented:
Thanks for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.