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.
| Author |
Topic |
|
Sql_forum
Yak Posting Veteran
50 Posts |
Posted - 2011-11-09 : 03:57:58
|
| I have select queery where it should return columns based on WHERE condition..If suppose there are no rows , I want NULL to be populated in that columns.How can i achieve this?? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 04:05:46
|
| [code]SELECT columns....FROM(SELECT columns,Count(column) OVER() AS CntFROM tableUNION ALLSELECT NULL,NULL,...)tWHERE Cnt =0OR Column IS NOT NULL[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-09 : 04:06:00
|
can you show us the query ? It will be easier for us to understand your requirement and provide a solution accordingly KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Sql_forum
Yak Posting Veteran
50 Posts |
Posted - 2011-11-09 : 04:17:04
|
| SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNTFROM(SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNT,Count(Column) OVER() AS CntFROM dbo.dbo_TH609F WHERE VALIDFLAG=1 AND ITEMITEM=@ProductCdUNION ALLSELECT NULL,NULL,NULL,NULL)tWHERE Cnt =0OR Column IS NOT NULLHi Visakhi am getting the error as mentione below when i execute the above qry..Msg 102, Level 15, State 1, Line 4Incorrect syntax near ')'.Msg 102, Level 15, State 1, Line 8Incorrect syntax near ')'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 04:29:24
|
quote: Originally posted by Sql_forum SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNTFROM(SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNT,Count(ColumnITEMITEM) OVER() AS CntFROM dbo.dbo_TH609F WHERE VALIDFLAG=1 AND ITEMITEM=@ProductCdUNION ALLSELECT NULL,NULL,NULL,NULL,NULL)tWHERE Cnt =0OR ColumnITEMITEM IS NOT NULLHi Visakhi am getting the error as mentione below when i execute the above qry..Msg 102, Level 15, State 1, Line 4Incorrect syntax near ')'.Msg 102, Level 15, State 1, Line 8Incorrect syntax near ')'.
you need to replace Column with actual columnname------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sql_forum
Yak Posting Veteran
50 Posts |
Posted - 2011-11-09 : 04:38:13
|
| Hi VisakhAfter executing the query , i m not getting NULL values in columnsI want output as follows---------------------------------------ITEM VALIDFLAG INDIC PRCNTNULL NULL NULL NULL---------------------------------------currently it is not returning anyrws |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 04:42:22
|
| [code]SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNTFROM(SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNT,Count(1) OVER() AS CntFROM(SELECT ITEMITEM, VALIDFLAG,INDIC,PRCNTFROM dbo.dbo_TH609F WHERE VALIDFLAG=1 ANDITEMITEM=@ProductCdUNION ALLSELECT NULL,NULL,NULL,NULL)t)rWHERE Cnt =1OR ITEMITEM IS NOT NULL[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sql_forum
Yak Posting Veteran
50 Posts |
Posted - 2011-11-09 : 04:45:38
|
| grt......Thnx alot Visakh.. it is working as i need |
 |
|
|
|
|
|
|
|