473,657 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

phantom records

TB
Hi all:

I realize that this is strictly speaking not an ASP.NET question, but
since most of you people out there SQL gurus as well, I hope you will
bear with me on this occasion:

I would like to know how to create a "phantom" record when running a
SELECT query.

For example:

Running "Select ID, Firstname, Lastname from Customers order by
Lastname" would produce a list all the records in the Customers table
such as:

1 John Brown
2 Anne Johnsen
3 Tom Smith
But I would like the first the result of such a query to contain a
"virtual record", i.e information that I have manually inserted into
the query and does not correspond to any record.

So, by way of an example how do I ensure a return like this:

0 PETER JENSEN
1 John Brown
2 Anne Johnsen
3 Tom Smith

when there is no record containing the information "0 Peter Jensen"?

Thanks.

TB

Mar 29 '06 #1
9 1591
I answered it in

microsoft . public . sqlserver . programming

newsgroup.

Search by subject for "
Re: phantom records"

"TB" <tr********@gma il.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
Hi all:

I realize that this is strictly speaking not an ASP.NET question, but
since most of you people out there SQL gurus as well, I hope you will
bear with me on this occasion:

I would like to know how to create a "phantom" record when running a
SELECT query.

For example:

Running "Select ID, Firstname, Lastname from Customers order by
Lastname" would produce a list all the records in the Customers table
such as:

1 John Brown
2 Anne Johnsen
3 Tom Smith
But I would like the first the result of such a query to contain a
"virtual record", i.e information that I have manually inserted into
the query and does not correspond to any record.

So, by way of an example how do I ensure a return like this:

0 PETER JENSEN
1 John Brown
2 Anne Johnsen
3 Tom Smith

when there is no record containing the information "0 Peter Jensen"?

Thanks.

TB

Mar 29 '06 #2
SELECT 0 as ID,
'Peter' AS FirstName,
'Jensen' AS Surname
UNION ALL
SELECT Id,
FirstName,
Surname
FROM...

But that will insert the record and then sort. What are you doing with
the result of the query?

I suppose you could do

SELECT 0 AS ID, 'Peter' AS FirstName, 'Jensen' AS Surname, 0 AS DUMMY
UNION ALL
SELECT ID, FirstName, Surname, 1
FROM ...
ORDER BY Dummy, Surname

But that's getting uglier and uglier, why on earth would you want to do
this?

Mar 29 '06 #4
TB
Thanks a lot for your reply. Your last proposal works.

Now why would I want to do this? Well You see, I have this drop-down
list which is populated from a datebase. However, in addition to the
list items suppled by a table, I need some kind of default "all of the
above" (or below in case of af DDL) choice.

I have discovered that neither of the below statements:
ddlChoices.item s.add(new listitem("Defau lt",0)) 'This one adds at the
end of the list

OR

ddlchoice.items .insert(0,New listitem("Defau lt",9)) 'This one adds at
the beginning of the list

seem to be compatible with databinding from a data source, such as

ddlchoice.DataS ource = mydatareader
ddlchoice.DataV alueField = "ID"
ddlchoice.DataT extField = "Lastname"
ddlchoice.DataB ind()

If I databind after adding either of the two statements above, then
only the results from the database appear. If I add either of the two
statements after databinding then ddl will only contain one list item,
namely the manual insert.

So I came up with the idea of this phantom record.

What do you think?

TB

Mar 29 '06 #5
TB
One more thing:

I discovered that the columns get truncated according the length of the
dummy column. For example, the firstname column width is 5, because the
dummy record contains the 5 letter word Peter. Why is that?

Another thing is that the statement after the UNION ALL should contain
a CONCAT function.

However, if I do this:

SELECT 0 AS IDuser, '(All employees)' AS Lastname, 0 AS dummy
UNION ALL
SELECT Iduser, concat( FirstName, ' ', Lastname) AS name, 1
FROM tblUsers
ORDER BY dummy, lastname

then the secondary sort (after dummy) is done on the result of the
CONCAT operation. In other words, the (undesired) result will be

0 (All Employees) 0
2 Anne Johnsen 1
1 John Brown 1
3 Tom Smith 1
If I shorten to
SELECT 0 AS IDuser, '(All)' AS Lastname, 0 AS dummy
UNION ALL
SELECT Iduser, concat( FirstName, ' ', Lastname) AS name, 1
FROM tblUsers
ORDER BY dummy, lastname

then the result is

0 (All) 0
2 Anne 1
1 John 1
3 Tom 1

Cheers

TB

Mar 29 '06 #6
Your DDL only contains one item if you insert after databinding? That's
.... odd; I've got manifold examples right here in front of me where I
do something like

locations.DataS ource = Data.GetCarLoca tions(CurrentDe alership);
locations.DataT extField = "LocationId ";
locations.DataV alueField = "LocationNa me";
locations.DataB ind();

ListItem item = new ListItem("Selec t a location", "");
locations.Items .Insert(0, item);

Without a problem.

What is mydatareader exactly?

Mar 30 '06 #7
Oh, and the column length - This is off-topic, so I'll be brief. SQL
Server is guessing the data type of your columns from the first set in
the union operation. 'Peter' fits nicely as a VARCHAR(5) and the second
set is cast to that type. If you wanted to keep the columns separate
without truncating data, try CAST('PETER' as VARCHAR(xxx)) AS FirstName
where xxx is (1 + FirstName.Lengt h + Surname.Length)

Mar 30 '06 #8
TB
I discovered the reason, and I am the one to blame: My dataloading
routine in the page_load sub was not surrounded by "If Not
Page.IsPostBack Then [...] End if" condition. So, the whole shop was of
course reset every time I hit something.

Mydatareader is the data I pick from a MySQL database:

Imports MySql.Data.MySq lClient
Public Class searchpanel
[.....]

Dim myConnection As MySqlConnection
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
myConnection = New MySqlConnection (connstring)
If Not Page.IsPostBack Then
[...]
inisearch()
End If
End sub
sub inisearch()
Dim objCommand As MySqlCommand
Dim mydatareader As MySqlDataReader
strSQL = "Select ID, Concat(firstnam e,' ',lastname) as name from
tblUsers order by lastname"
objCommand = New MySqlCommand(st rSQL, myConnection)
myConnection.Op en()
mydatareader =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
ddlIDuser.DataS ource = mydatareader
ddlIDuser.DataV alueField = "ID"
ddlIDuser.DataT extField = "name"
ddlIDuser.DataB ind()
mydatareader.Cl ose()
end sub
As you can see, I use CONCAT to join firstname and lastname into one
column, but if I do that with the UNION ALLl statement, then the
sorting will be from left to right in the combined field, and not on
the lastname.

Anyway, I will now try to go back to my original idea of inserting a
listitem after databinding as the error in the page_load sub has been
corrected.

Mar 30 '06 #9
TB
Interesting. Thanks for the info.
I have replied separately to your other posting.

TB

Mar 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2617
by: Andrew | last post by:
Hi All, Have come across something weird and am after some help. Say i run this query where rec_id is a column of table arlhrl, select * from arlhrl where rec_id >= 14260 This returns to me 2 records with rec_id's of 14260 and 14261
2
4135
by: Max | last post by:
Hi. I really hope someone can help me. Going slowly insane with this problem. I have a two Access 2000 databases. One is the backend containing tables and some admin queries. The other is the front end with forms / queries and links to the tables in the back end. From the Relationships window I selected File / Print Relationships. The resulting report shows relationships that are not displayed in the relationships window. Some of...
3
2352
by: memememe | last post by:
I see weak reference on the .net api, but I do not see soft or phantom, are they supported on .net?
0
1237
by: Rod Billett | last post by:
The included html contains 3 divs. One primary Div, with 2 nested divs. the second nested DIV contains an empty table. Problem 1: Phantom Space. When viewed within the browser, the div 'action panel' is spaced a picel or two lower than the div 'ItemsPanel' even though both are the same height. Oddly enough, if you remove the table (ID Removemetowork) this phantom space dissappears and everything is fine with the world Problem 2:...
9
1264
by: Dave | last post by:
Apologies if this has come up before, but I can't find it if it has. I am fairly new to .Net and am having problems with ghosts in the datagrid. Basically I have a find screen that accepts search criteria, then interrogates a database to find all matching records. These are put into a dataset, which has a dataview that is used as the datasource of my datagrid, which is read only. The idea is that the user selects which record they...
4
1443
by: CCHDGeek | last post by:
I am using Access 2003 on a Windows XP Platform. I am using DAO to open up a table and export the records to PowerPoint. I'm having an issue where deleted records are still showing up in the table's memory somehow. Even if I delete the table and start fresh, the records still show up. Set db = CurrentDb Set rs = db.OpenRecordset("WEEKLY_UPDATE_TABLE", dbOpenSnapshot) rs.Requery ' Open up an instance of Powerpoint. Set ppt = New...
3
1602
by: CCHDGeek | last post by:
I have a client-server DB, main tables in back-end, forms in front-end. The front-end is on each person's individual PC. I'm using the following code to declare a recordset and an instance of Powerpoint. Set db = CurrentDb Set rs = db.OpenRecordset("SELECT * From WEEKLY_UPDATE_TABLE ORDER BY WEEKLY_UPDATE_TABLE.ENTRY_DATE DESC , WEEKLY_UPDATE_TABLE.ENTRY_TIME", dbOpenDynaset) rs.Requery Set ppt = New PowerPoint.Application ppt.Activate...
0
1390
by: Rebles | last post by:
I'm writing a PERL script to access and insert rows into a Microsoft SQL. i'm using MS SQL Server Management Studio Express (2005) to architect tables and queries. I've inserted two records into my "Pools" table One from MS SQL Manager as a test, and another from my PERL script. The one inserted by MS SQL Manager shows up everytime. The row from my PERL script doesn't show up on MS SQL Manager and ONLY shows up on my PERL script and...
1
2275
by: Rebles | last post by:
Hi, I just posted this in the MS SQL Section, but maybe my problem is rooted in Perl, so it's more appropriate to post here instead (sorry for the double post) I'm writing a PERL script to access and insert rows into a Microsoft SQL. i'm using MS SQL Server Management Studio Express (2005) to architect tables and queries. I've inserted two records into my "Pools" table One from MS SQL Manager as a test, and another from my PERL script....
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.