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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select statement to only insert into certain rows

Author  Topic 

yoggi123
Starting Member

29 Posts

Posted - 2005-01-14 : 08:00:45
Hey,

I am not sure how to really explain this, but I'll give it a try.

I am looking to use a select statement in a way that I can tell it which rows to insert in depending on when only one result is returned. For example, if I run this statement:

SELECT Column1, Column2, Column3
FROM #Temp1

The result set is:

Column1---Column2---Column3
99--------6756756---55555
44--------55---------NULL

Column3 as only the one returned value, so I do not want it associated with any of the other rows, so I need this:

Column1---Column2---Column3
NULL------NULL------55555
99--------6756756---NULL
44--------55---------NULL

Another example:

The returned result now is:

Column1---Column2---Column3---Column4
99---------6756756---55555-----NULL
42---------55--------NULL------12345

So I need:

Column1---Column2----Column3----Column4
NULL-------NULL-------55555------NULL
NULL-------NULL-------NULL-------12345
99---------6756756----NULL-------NULL
44---------55---------NULL-------NULL


Does this make sense, and/or is it even possible?

I know it could be more of a presentation thing, but I would like to know how to do it in the code behind.

Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-01-17 : 00:42:35
Something like this

Select case when((select column3 from table where column1=T.Column1)<>"") then
null else column1 end,
case when((select column3 from table where column2=T.Column2)<>"") then
null else column2,
column3, column4
from Table T

Madhivanan
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-01-17 : 01:22:59
You could use the union operator to construct the resultset

select col1, col2, null as col3 from tab where ...
union all
select null as col1, null as col2, col3 from tab where ...

rockmoose
Go to Top of Page
   

- Advertisement -