473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

insert column in union select statement

hi all,

i am currently using the following union select statement to select records from two of my tables to a gridview in asp.net:
Expand|Select|Wrap|Line Numbers
  1. SELECT ConcertName AS Name, ConcertDate AS Date, ConcertTime AS Time, ConcertPlace AS Place FROM Concerts UNION SELECT EventName AS Name, EventDate AS Date, EventTime AS Time, EventPlace AS Place FROM Events ORDER BY Date, Time
now i'd like to add two things to this statement:

1. a column that indicates whether the row is taken from the Concerts table or not (i imagine a boolean column with value True if it is, and Null or False if not). I tried with just inserting "True AS InConcerts" after "ConcertPlace AS Place", but of course i got an error.

2. a column to use as "id" column - since ConcertID and EventID (not currently selected) can be the same, i can't use them, but i'd rather need a column containing the row number or something of the sort. i'm not going to use it for anything but selecting, but i would like to be able to choose one row in my gridview and view the details of that record only in a detailsview.

can these two columns be added "on the fly" in sql, or do i have to (i really don't want to!) redesign my database?

thanks!

and by the way, i'm using sql server 2005 express with .net framework 2.0 (maybe soon migrating to 3.5).
Dec 7 '07 #1
1 4192
Jim Doherty
897 Expert 512MB
hi all,

i am currently using the following union select statement to select records from two of my tables to a gridview in asp.net:
Expand|Select|Wrap|Line Numbers
  1. SELECT ConcertName AS Name, ConcertDate AS Date, ConcertTime AS Time, ConcertPlace AS Place FROM Concerts UNION SELECT EventName AS Name, EventDate AS Date, EventTime AS Time, EventPlace AS Place FROM Events ORDER BY Date, Time
now i'd like to add two things to this statement:

1. a column that indicates whether the row is taken from the Concerts table or not (i imagine a boolean column with value True if it is, and Null or False if not). I tried with just inserting "True AS InConcerts" after "ConcertPlace AS Place", but of course i got an error.

2. a column to use as "id" column - since ConcertID and EventID (not currently selected) can be the same, i can't use them, but i'd rather need a column containing the row number or something of the sort. i'm not going to use it for anything but selecting, but i would like to be able to choose one row in my gridview and view the details of that record only in a detailsview.

can these two columns be added "on the fly" in sql, or do i have to (i really don't want to!) redesign my database?

thanks!

and by the way, i'm using sql server 2005 express with .net framework 2.0 (maybe soon migrating to 3.5).


Hi ya Black Mustard,

I think this is what you require isnt it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT     ConcertName AS Name, ConcertDate AS Date, ConcertTime AS Time, ConcertPlace AS Place, 'Concerts' AS Source
  3. FROM         Concerts
  4. UNION
  5. SELECT     EventName AS Name, EventDate AS Date, EventTime AS Time, EventPlace AS Place, 'Events' AS Source
  6. FROM         Events
  7. ORDER BY Date, Time
  8.  
As for your not being able to use your id numbers I fail to see why? (if I understand you correctly that is) you have a flag marker there telling you the source, so IF logic could be applied to retrieve your records from Concerts if the source field states 'Concerts' conversely selecting records from 'Events' if source states 'Events' in either case using the ID number to identify the relevant table row as part of any WHERE clause in an SQL statement

Regards

Jim :)
Dec 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
5
by: ratu | last post by:
I'd like to use a stored procedure to insert large amounts of records into a table. My field A should be filled with a given range of numbers. I do the following ... but I'm sure there is a better...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
12
by: Martin_Hurst | last post by:
Has some one come up with a similar type script that could be used in a Postgresql database? The script below was created for a SQLServer database. Thx, -Martin ...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
0
by: gpspocket | last post by:
help me -CURSOR backward insert from End Date > to Start Date how to insert dates from end to start like this SELECT 111111,1,CONVERT(DATETIME, '17/03/2008', 103), CONVERT(DATETIME,...
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.