Solved

Force "empty" row return

Posted on 2009-04-15
5
1,149 Views
Last Modified: 2012-05-06
I need to return 5 rows - if there is data corresponding to all 5 great - would like to return that - if not, say - only data in the table for 2 out of 5 rows - then show the first two populated with data and then 3 empty rows, etc.

Is this even possible in SQL?
0
Comment
Question by:tbaseflug
5 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24149524
sure
you can return all nulls using a left outer join
but for me to provide you with the syntax i need your table structure
0
 

Author Comment

by:tbaseflug
ID: 24149690
Here it is
CREATE TABLE [dbo].[CM_tblContractStopLoss](

	[stopLossID] [int] IDENTITY(1,1) NOT NULL,

	[contractID] [int] NOT NULL,

	[startDate] [datetime] NULL,

	[endDate] [datetime] NULL,

	[dollarThreshold] [money] NULL,

	[billedChargesPercentage] [decimal](4, 2) NULL,

	[dollarType] [tinyint] NULL,

	[dollarNotToEceed] [money] NULL,

	[patientType] [tinyint] NULL,

Open in new window

0
 
LVL 11

Expert Comment

by:N R
ID: 24149785
You mean like:

select *
from dbo.cm_tblcontractstoploss
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24149815
you can do something like this



declare @sql nvarchar(max)

declare @param varchar(10)

declare @rows int

set @param='mis'

select @rows=count(id) from emps where dept=@param

set @sql='select name from emps where dept='+char(39)+@param+char(39)+ ' union all'

--exec sp_executesql @sql

if @rows<5

begin

declare @i int

set @i=5-@rows

while @i>0

begin

set @sql=@sql+ ' select char(13) as name  union all'

set @i=@i-1

end

end

set @sql=left(@sql,len(@sql)-9)

exec sp_executesql @sql

Open in new window

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24149913
here is bit modified version for your own table. however you have to include remaining column, I have used only two column

declare @sql nvarchar(max)

declare @param varchar(10)

declare @rows int

select @rows=count(stopLossID) from CM_tblContractStopLoss

set @sql='select stopLossID,contractid from CM_tblContractStopLoss union all'

--exec sp_executesql @sql

if @rows<5

begin

declare @i int

set @i=5-@rows

while @i>0

begin

set @sql=@sql+ ' select NULL as stopLossID,NULL as contractid  union all'

set @i=@i-1

end

end

set @sql=left(@sql,len(@sql)-9)

print @sql

exec sp_executesql @sql

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Optimizing a query 3 33
ASP.NET 5 Templates 2 65
the term "Features Not Supported in the Next Version of SQL Server" 12 27
recover sqlserver db 8 45
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now