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 2005 Forums
 Transact-SQL (2005)
 how to insert a dummy row on top of qry resultset

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2007-07-11 : 11:24:17
I have the following Stored proc, how can i have a dummy row on top with no values. it has to be based on @Emptyrow bit parameter, if @emptyrow has 1 then the resultset has to have a dummyrow on top afterwards the results from teh query.

if the @Emptyrow has "0", then ignore the dummyrow, just bring teh query results. theis stored proc works fine, i want to just plug in the dummy row on top of the resultset based on @emptyrow bit parametrer value.


************************************************
Create PROCEDURE [dbo].[USP_GetUsers_ByLevel]
@ContractID int,
@EmptyRow bit
AS

DECLARE @ErrorCode int

SET NOCOUNT ON
SELECT @ErrorCode = @@Error
IF @ErrorCode = 0
BEGIN
select UA.userID, US.username from tab_netUserAccess UA, Tab_netUsers US
where UA.contractid=@ContractID and UA.UserID =
US.UserId and UA.Deleted = 0 group by UA.userid,UA.username

SELECT @ErrorCode = @@Error
END
SET NOCOUNT OFF
RETURN @ErrorCode
****************************************************

Thank you vfery much fro the information

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-11 : 11:30:26
if it has to be based on the bit then insert your select into a temp table and if your bit is 1
insert another empty row.
then select data from your temp table

but this is a weird requirement. care to explain why you need this?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-11 : 13:48:52
How about this?
************************************************
Create PROCEDURE [dbo].[USP_GetUsers_ByLevel]
@ContractID int,
@EmptyRow bit
AS

DECLARE @ErrorCode int

SET NOCOUNT ON
SELECT @ErrorCode = @@Error
IF @ErrorCode = 0
BEGIN
SELECT null as userid, null as username
WHERE @EmptyRow=1
UNION ALL
select UA.userID, US.username from tab_netUserAccess UA, Tab_netUsers US
where UA.contractid=@ContractID and UA.UserID =
US.UserId and UA.Deleted = 0 group by UA.userid,UA.username

SELECT @ErrorCode = @@Error
END
SET NOCOUNT OFF
RETURN @ErrorCode
****************************************************




Mark
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-07-11 : 14:52:45
PLEASE explain to us why you need the dummy row ... I am looking forward to hearing this. Let me guess -- to help add spacing to a report or web page?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-11 : 15:21:56
Not to speak for cplusplus, but occasionally, I will use this to have a blank first listitem in a bound combobox (or drop-down list). If the data value for the column is optional, it provides a way for the user to select a null value, or allow a null value to be allowed.


Mark
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-07-11 : 15:38:57
quote:
Originally posted by mharr

Not to speak for cplusplus, but occasionally, I will use this to have a blank first listitem in a bound combobox (or drop-down list). If the data value for the column is optional, it provides a way for the user to select a null value, or allow a null value to be allowed.

Mark



That should be handled by the front-end.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-11 : 16:21:42
quote:
Originally posted by jsmith8858

That should be handled by the front-end.


Ah, spoken like a DBA with no development responsibilities.
"It's not my job." [url]http://www.tonyrogers.com/humor/not_my_job.htm[/url]
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-11 : 16:27:35
Jeff is not a DBA. He is a senior or higher developer. You should take this opportunity to learn a lot from him.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-11 : 16:37:19
Then let me put my 29 years of development experience (long before Codd put his 12 rules to paper) to the side and ask another question. WHY does it have to be on front-end job.

This poor guy probably is struggling with an unreasonable DBA that say "all queries have to be stored procedures". Yes, it CAN be done on the front-end. But perhaps the sproc is probably used only to populate combo boxes, the associated data field allows nulls, so I can contend that this properly belongs in the "back-end" to be used/re-used/shared/consistent wherever this list for combo boxes is used.

Mark
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-07-11 : 16:37:21
he he ... Now I've been called everything!

As Tara said, I am not a DBA. And if a DBA insisted on returning blank rows to "help" me write my application code, that would be one really, really bad DBA. As a developer, it is much easier, cleaner, shorter and clearer to do things like add blank rows or "(please select)" to drop-down lists in your application code, where they belong. And, since those blank rows are not data and have nothign to do with the database, it has the added benefit of following best practices and giving you a clear separation between your data layer and your presentation layer.

So, ironically, instead of saying "it's not my job", what I really said was "it's not YOUR job, let me do it, the way it is supposed to be done!"

Classic!



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-11 : 16:40:46
quote:
Originally posted by jsmith8858
... since those blank rows are not data ...



Ah, but the "blank rows" ARE data. I stipulated that the data field allowed nulls, so nulls are a valid data value.

Mark
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-11 : 16:41:03
As a DBA, I agree that all queries needs to be done through stored procedures. But that doesn't mean that a blank row needs to be or should be produced by a query. That just doesn't make sense.

The whole purpose of stored procedures is for data access. A blank row is not data as Jeff pointed out.

I really don't understand why Mark keeps bringing up DBAs. It's got nothing to do with this thread. This is a question of where to put the blank row, in the front-end or the back-end.

I agree with Jeff that this is the front-end's job.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-07-11 : 16:41:17
quote:
Originally posted by mharr

Then let me put my 29 years of development experience (long before Codd put his 12 rules to paper) to the side and ask another question. WHY does it have to be on front-end job.

This poor guy probably is struggling with an unreasonable DBA that say "all queries have to be stored procedures". Yes, it CAN be done on the front-end. But perhaps the sproc is probably used only to populate combo boxes, the associated data field allows nulls, so I can contend that this properly belongs in the "back-end" to be used/re-used/shared/consistent wherever this list for combo boxes is used.

Mark



we posted at the same time, but see my previous post for the answer to your question.

List items like "(please select)" are not data, they are presentation. In your applications, some drop-downs without a value selected will choose to leave it blank, some will default to a certain value, others might display "(none)" or "n/a" or something else entirely. That is all dependent on how your UI chooses to present the situation. This has nothing to do with database code.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 01:29:25
quote:
Originally posted by mharr

Not to speak for cplusplus, but occasionally, I will use this to have a blank first listitem in a bound combobox (or drop-down list). If the data value for the column is optional, it provides a way for the user to select a null value, or allow a null value to be allowed.


Mark


What if I want to show "ALL" as first item in a combo box or list box where DATE values are listed along with? How will you do this at back end? The only way is to make all your DATE values to varchar to compatible to the value "ALL". Now if all the DATE values should be shown in "DD/MM'YYYY" format, how do front end develeopers do other than converting varchars to DATE and use FORMAT function?

If the data should be shown in front end, then the best approach is to send only required data to client and let client deals with the data

The only thing that should be done in front end is

Dim i as integer
set i=1
cbItems.AddItem(0)="ALL"

While not Rs.EOF
cbItems.AddItem(i)=Rs("col")
Rs.moveNext
i=i+1
Loop

Now when adding to combo, you can format Rs("col") in the format you want without changing the source data

Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-12 : 02:18:01
I think he do not know of this Madhi, and instead is using a databound grid with one column to show the data.
Hence the urge for "<all>" or "<please select>" record in the resultset.

You can learn a lot from a guy with 29+ years of experience. Don't mock him...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 02:42:23
quote:
Originally posted by Peso

I think he do not know of this Madhi, and instead is using a databound grid with one column to show the data.
Hence the urge for "<all>" or "<please select>" record in the resultset.

You can learn a lot from a guy with 29+ years of experience. Don't mock him...


Peter Larsson
Helsingborg, Sweden


Peso, My intention is to show how it can be done easily in front end without much complex code at back end. I know that most of DBAs or SQL developers who dont have enough knowledge in Front ends prefer doing everything at back end. I just want to show mharr how it can be done in front end easily(given that adding NULL value to combo box is discussed by mharr)

I wont hesitate to learn from anyone regardless of experience

Becuase mharr has given example on Adding NULL value at the top of combo, I have shown the example of doing the same in an easier way


Madhivanan

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

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-12 : 04:30:46
quote:
Originally posted by Peso

I think he do not know of this Madhi, and instead is using a databound grid with one column to show the data.
Hence the urge for "<all>" or "<please select>" record in the resultset.

You can learn a lot from a guy with 29+ years of experience. Don't mock him...


Peter Larsson
Helsingborg, Sweden



Feel the sarcasm, Maddy?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

mharr
Starting Member

20 Posts

Posted - 2007-07-12 : 07:08:30
One last attempt to describe the scenario where this is appropriate.

The original question asked for a row of "no values" at the beginning of the resultset from a stored procedure. I have provided a way to do so. In response to jsmith asking why this was desired, I explained a couple scenarios this could be useful. Yes, madhivan, I am aware of how to do so in VB apps. In Asp.Net web apps, using a bound dropdown list control for a column that allows null values, the control does not provide for setting an unselected value (equivalant to listindex = -1 in VB). To show a null data value, you need a row in the dropdown list to show an empty or unselected value. You can do it on the front-end by the same "manual" VB method shown by madhivan, or intercept the datasource dataset and add the empty row to the datatable, or add an item to the Items collection and specify .AppendDataBoundItems=True. Or you can add an empty row in the stored procedure (as I showed), and the empty row is there without any client-side code.

Note that this is not the same as "<select all>" or "ALL" or "(please select)". This is appropriate for the instance where a null value is permitted and allowed. You can add those types of items on front-end, and have some postback code to check and handle those values.

But if you want to allow a null value for the data item, and allow the user to select a null value, and have the data item updated to a null value, then a null value ("empty row") in the resultset is appropriate. Adding it to the query to load the dropdown list is the easiest way to do it, and fits all the rules and criteria for good design; the null value is a valid data value.

Mark
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-12 : 07:11:52
well the proper way of doing this is to derive a new control from the dropdownlist that allows an empty value.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 08:06:44
quote:
Originally posted by mharr

One last attempt to describe the scenario where this is appropriate.

The original question asked for a row of "no values" at the beginning of the resultset from a stored procedure. I have provided a way to do so. In response to jsmith asking why this was desired, I explained a couple scenarios this could be useful. Yes, madhivan, I am aware of how to do so in VB apps. In Asp.Net web apps, using a bound dropdown list control for a column that allows null values, the control does not provide for setting an unselected value (equivalant to listindex = -1 in VB). To show a null data value, you need a row in the dropdown list to show an empty or unselected value. You can do it on the front-end by the same "manual" VB method shown by madhivan, or intercept the datasource dataset and add the empty row to the datatable, or add an item to the Items collection and specify .AppendDataBoundItems=True. Or you can add an empty row in the stored procedure (as I showed), and the empty row is there without any client-side code.

Note that this is not the same as "<select all>" or "ALL" or "(please select)". This is appropriate for the instance where a null value is permitted and allowed. You can add those types of items on front-end, and have some postback code to check and handle those values.

But if you want to allow a null value for the data item, and allow the user to select a null value, and have the data item updated to a null value, then a null value ("empty row") in the resultset is appropriate. Adding it to the query to load the dropdown list is the easiest way to do it, and fits all the rules and criteria for good design; the null value is a valid data value.

Mark


mharr, I agree with you on NULL values. Yes it is easy that you can do it at back end without affecting front end. But it is not always the case where NULL value is preferred to say "ALL". In that case I prefer doing it in front end to show "ALL", "(Please select}", etc

Madhivanan

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-07-12 : 08:38:05
quote:
Originally posted by mharr

One last attempt to describe the scenario where this is appropriate.

The original question asked for a row of "no values" at the beginning of the resultset from a stored procedure. I have provided a way to do so. In response to jsmith asking why this was desired, I explained a couple scenarios this could be useful. Yes, madhivan, I am aware of how to do so in VB apps. In Asp.Net web apps, using a bound dropdown list control for a column that allows null values, the control does not provide for setting an unselected value (equivalant to listindex = -1 in VB). To show a null data value, you need a row in the dropdown list to show an empty or unselected value. You can do it on the front-end by the same "manual" VB method shown by madhivan, or intercept the datasource dataset and add the empty row to the datatable, or add an item to the Items collection and specify .AppendDataBoundItems=True. Or you can add an empty row in the stored procedure (as I showed), and the empty row is there without any client-side code.

Note that this is not the same as "<select all>" or "ALL" or "(please select)". This is appropriate for the instance where a null value is permitted and allowed. You can add those types of items on front-end, and have some postback code to check and handle those values.

But if you want to allow a null value for the data item, and allow the user to select a null value, and have the data item updated to a null value, then a null value ("empty row") in the resultset is appropriate. Adding it to the query to load the dropdown list is the easiest way to do it, and fits all the rules and criteria for good design; the null value is a valid data value.

Mark



That is still a presentation issue; NULL is not a valid value, it is the absence of any value.

Why it is presentation? One group of apps might choose to display "N/A", "None", something else. Another app might choose to display a checkbox that says "None" and to enable/disable or show/hide the drop-down list based on the checkbox. Yet another might show nothing but a button that says "Assign XXXX" if there is no value currently there, and that displays the drop-down in a pop-up. the possibilities are endless.

A single app might also want to change how it displays this situation; forcing "empty" values into your stored proc means that two things need to change (SQL code, UI code), letting the UI handle it means only one thing has to change (UI code). Which makes the most sense when only the UI is changing?

In short, your database code simply needs to return the valid values, for a drop-down list or anything else -- it just returns pure DATA. Nothing else. Your user interface accepts that data and decides how it wants to display it and where -- drop down list, data-grid, etc.

Here's another example -- suppose you want to 'space out' a data grid to display every other row as a blank. Is this a presentation issue, or a database issue?

One a side note -- the code you provided needs a sort to even ensure that the NULL row displays first; that will not always be the case without explicitly stating it. or maybe some drop-downs want it at the end, instead of at the beginning. The problem is, the UI cannot decide where and how to display this if you force your database layer to do it.

29 years of experience is great, but the problem with that is people tend to do things "the way they've always done it" and not listen to alternate approaches; I've been programming for about 20 years myself (10 professionally) and I still always listen to other ideas about how to do things and approach techniques with an open mind. However, I always try to follow best practices and short, concise, clear methods of doing things where they make the most sense and are most maintainable and flexible.

Mladen hit it on the head -- if your app has many places where drop-down lists need to display an "n/a" option, you simply inherit from a drop-down list and add this feature as a simple property. Easily done in about 10 lines of code, all in one place, your entire app can use, not a single line of T-SQL code is written, and it is all maintainable in the UI and very flexible. If you force this into your SQL code, your app has no control at all regarding how it will present this situation, and if any other parts of your code would like to use that stored proc, they'd need to deal with the NULL values returned, or you'd have to put this "@returnBlankRow" param into every SQL statement, along with "@ReturnBlankAtEnd" and "@DescriptionForBlankRow" and potentially others to give your UI *some* control over the presentation of these drop-down lists. If you think that's a good programming model, that's your opinion, but I would strongly disagree and suggest that you are making something very simple very complicated and creating more work for both the DBA's and the front-end guys.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
    Next Page

- Advertisement -