SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQLSERVER 2000 SQL UNION


SQLSERVER 2000 SQL UNION

Author
Message
Lennie
Lennie
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 50
Hullo Friend, :-P

I am encounter problem trying to create a SQL string with UNION in order to display --- Select Customer --- as text on the C#NET combobox but it's not working.

Please help me, I need your help. :-)


Error message: w00t
All queries n an SQL statement containing a UNION operator must have
equal number of expressions in their target lists

Here are the coding:
{
string strsql = "";
strsql += " Select ' ---Select Customer --- ' 'CompanyName' ";
strsql += " Union Select CustomerId, CompanyName ";
strsql += " From testCustomers ";
strsql += " Order by CompanyName ";

try
{
sqlconn = new SqlConnection(connstr);
sqlconn.Open();
DA = new SqlDataAdapter(strsql, sqlconn);
DS = new System.Data.DataSet("DS");
DA.Fill(DS, "cust");

//---fill combobox
this.comboBoxInvoice.DisplayMember = "CompanyName";
this.comboBoxInvoice.ValueMember = "CustomerID";
this.comboBoxInvoice.DataSource = DS.Tables["cust"];

}
Catch (exception ex)
{
messagebox.show(ex.message);
}
}

Thank you for your help.
Ian Scarlett
Ian Scarlett
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2777 Visits: 7050
We need a lot more information to stand much chance of helping.
When you say it doesn't work, exactly what do you mean? Are you getting an error message?

Just looking at the SQL you are generating, I can see a problem... the first select should have a comma between the two columns you are selecting:-

Select ' ---Select Customer --- ', 'CompanyName' 

There are other potential problems e.g. I suspect CustomerId will be an integer. If it is you will probably get another error when SQL Server tries to convert ' ---Select Customer --- ' to an integer.



Lennie
Lennie
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 50
Hullo Ten Centuries,
This is one decade Lennie.

Your suggestion and statement Quotaton
Select ' ---Select Customer --- ', 'CompanyName'

There are other potential problems e.g. I suspect CustomerId will be an integer. If it is you will probably get another error when SQL Server tries to convert ' ---Select Customer --- ' to an integer.

Regarding your statement the CustomerID is integer I am sorry to say that it's wrong. I view the SQL SERVER table TestCustomer the datatype is Varchar.

This sample coding from you:
Select ' ---Select Customer --- ', 'CompanyName'

it create 76 rows of System.Data.DataRowView
in the COMBOXBOX.
Dave Ballantyne
Dave Ballantyne
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3750 Visits: 8370
Since your last thread went a little awry,

http://www.sqlservercentral.com/Forums/Topic984249-9-1.aspx

personally, im not inclined to help here.

Karma my friend.



Clear Sky SQL
My Blog
Lennie
Lennie
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 50
Hullo Dave Ballantyne
Thank you for not helping
That proved that you don't have the knowledge or skill set.
You should post your problem here to receive help.
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12556 Visits: 13351
Guys, behave yourselves, please.
Don't let this thread go sideways.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Lennie
Lennie
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 50
Gianluca Sartori

The posting I did not go sideway as I am a good driver.
I responded to the helper with the testing result based on their generous sharing of sample coding. It's an honest to tell them the truth of whether it's working or not so that other newbies who may have similar problem will be guide and not misguide and get frustrated.

And you have to be gentleman to face the fact and stop blaming. w00t
ALZDBA
ALZDBA
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15705 Visits: 8967
Of course Ian is correct !

The problems lies with the dev who tries to fit header rows into his row set and doesn't know how to make his combobox show headers.
If the dev knew, (s)he would have just queried the table without the union and altered the column names s(he) received in the set or directly selected by providing column aliases that would fit her/his needs.

Have a nice weekend.

Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Lennie
Lennie
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 50
ALZDBA

Ian is correct to himself not me because both of us are not from the same company and same department and differnt country.
My company SQL SERVER table TestCustomers DataType for CustomerID is VarChar so it's not Integer. Unless you don't understand english as I did mentioned that CustomerID datatype is Varchar in respond to IAN suggestion.

So, you better be careful with your driving and be sensitive to your environment and do not take event for granted.


As per your statement I do not understand what you are suggesting as there are no sample coding to provide logical explanation like the other helpers did. They are good providing sample coding which they think is correct logical. They are good driver.
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12556 Visits: 13351
I suggest everybody to stop posting to this thread.
Lennie seems to know everything he needs, and much more.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search