Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 SQL Server 6.5 \ SQL Server 7.0
 Conditional Insert

Author  Topic 

medotnet
Starting Member

14 Posts

Posted - 2007-08-21 : 04:10:58
Hi all,
how can I apply this into SQL stmt:

SELECT a,b, SUM(c)-SUM(d) AS result FROM A# GROUP BY a,b

//now insert the result into another table similar to A#
IF ( result > 0 )

INSERT INTO B# VALUES ( a , b , 0 , result )

ELSE

INSERT INTO B# VALUES ( a , b , result , 0 )

I did it by code (C++ and ADO) but the performance is bad, and I don't feel good using code instead of SQL (it is a SIN).

thanks,

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-21 : 04:47:21
INSERT INTO B#(columns)
SELECT a,b,
case when (coalesce(SUM(c),0)-coalesce(SUM(d),0))>0 then (coalesce(SUM(c),0)-coalesce(SUM(d),0))
else 0 end FROM A# GROUP BY a,b


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

medotnet
Starting Member

14 Posts

Posted - 2007-08-21 : 09:36:46
Well thank you Guru,
I appreciate your help
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-22 : 01:16:56
quote:
Originally posted by medotnet

Well thank you Guru,
I appreciate your help



You are welcome

If you are new to sql, learn it

http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -