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
 General SQL Server Forums
 New to SQL Server Programming
 reading data from sql

Author  Topic 

lamiajoyee
Starting Member

15 Posts

Posted - 2012-01-15 : 10:06:52
hello when we read data from sql we use reader["columnname"].my problem is that when my query is like this "select sum(columnname) from table" what will i specify in the reader["columnname"] field?please specify

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-15 : 10:18:19
you should give it an alias like below

select sum(columnname) as colsum from table


and then use reader["colsum"]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-15 : 10:18:44
You will either need to initialize a variable and then aggregate as you iterate through each row in the reader,

OR

A MUCH better option, let SQL do the summation for you. Change your query string to something like "select sum(columnname) AS SUMCOLUMN from yourTable". If you are only trying to get the sum, you could even use ExecuteScalar in that case.

Edit: misread the question. OP is asking for aliasing, correction in red..
Go to Top of Page

lamiajoyee
Starting Member

15 Posts

Posted - 2012-01-15 : 10:44:37
thank you all
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-15 : 11:41:33
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -