Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2009, 12:26 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Question Insert and Select using where clause

I have following code that I need to expand. I need to select only records from t2 where field t2.jde_equipment_hours > 0. I tried to do this but does not seem to work:

Code:
 
insert into #Combined_LaborHrsDetailTable (jobdate, costcode, employeename, equipmentid, equipmentname, jdeequipmenthours) 
select t2.jde_job_date, t2.jde_cost_code, t2.jde_employee_name, t2.jde_equipment_id, t2.jde_equipment_name, 
t2.jde_equipment_hours 
from #JDE_LaborHrsDetailTable AS t2 
where not exists (select * from #Combined_LaborHrsDetailTable AS t1 where t1.jobdate = t2.jde_job_date and
t1.equipmentid = t2.jde_equipment_id and 
t1.employeename = t2.jde_employee_name and
t1.eqequipmenthours = t2.jde_equipment_hours and
t2.jde_equipment_hours > 0)
 
Old March 19th, 2009, 12:49 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

Please post structures of your table and define your requirement properly
__________________
urt

Help yourself by helping someone.
 
Old March 19th, 2009, 12:01 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Your logic in your query may be off. You're saying WHERE NOT EXISTS and then asking the query to select t2.jde_equipment_ > 0. Maybe if you said WHERE t2.jde_equipment_hours = 0 ... so that everything you get from the outer query will be > 0.

Just a shot in the dark.
 
Old March 22nd, 2009, 04:50 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I answered this but, once again, this forum is throwing away my posts!

Grumble!

I'm not positive, but I *THINK* you just have a misplaced right paren.

Try this:
Code:
insert into #Combined_LaborHrsDetailTable 
    (jobdate, costcode, employeename, equipmentid, equipmentname, jdeequipmenthours) 
select t2.jde_job_date, t2.jde_cost_code, t2.jde_employee_name, 
       t2.jde_equipment_id, t2.jde_equipment_name, t2.jde_equipment_hours 
from #JDE_LaborHrsDetailTable AS t2 
where not exists (
      select * from #Combined_LaborHrsDetailTable AS t1 
      where t1.jobdate = t2.jde_job_date 
      and t1.equipmentid = t2.jde_equipment_id 
      and t1.employeename = t2.jde_employee_name 
      and t1.eqequipmenthours = t2.jde_equipment_hours 
      )
and t2.jde_equipment_hours > 0

.





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert into....select problem timbal25 SQL Server 2005 1 October 24th, 2007 08:28 PM
Select statement with where clause with 2 conditon Yasho VB.NET 2002/2003 Basics 7 May 16th, 2007 01:40 PM
Select statement with where clause for visual basi Yasho SQL Language 6 May 16th, 2007 01:00 AM
How grap the passed value in select where clause method ASP.NET 1.x and 2.0 Application Design 1 June 16th, 2005 10:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.