C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Eureka!!! Find ways to extract a particular column value


Posted Date: 18 Jul 2006    Resource Type: Articles    Category: Databases
Author: prabhakarMember Level: Silver    
Rating: Points: 8



Introduction



SQLSERVER is a vast ocean where i found out nothing is impossible.

Paragraph Heading 1



What i Found out:

In here, i found out how to select a particular column from a row and then get the max from a ROW. All we have heard till now is how to select single column and then complex queries pursuit. The table which was used in the code had one row and three columns.



declare @coun int
declare @str varchar(5)
declare @a int
create table #temptable(val int)
select @coun = count(*) from syscolumns where id = object_id('tablename')
while(@coun > 0)
begin
select @str = name from syscolumns where colid = @coun and id = object_id('tablename')
insert into #temptable exec ('select '+ @str + ' from tablename')
set @coun = @coun - 1
end
select max(val) from #temptable


Paragraph Heading N



This kept me busy for about a day, but then things unfolded very interestingly. Here i dealt with something called dynamic query.

A dynamic query gets in a variable and generates the result set based on the current value.

This sql statement exec ('select '+ @str + ' from tablename') is a dynamic query. i used this query because passing a varchar as such to the table fetched me what i passed. i was really puzzled but later found out that this could help me, by and large.

To get hold of the column name, just use the syscolumns object and get the column id. That's it. Rest is what u c.

Summary



This article briefed about the usage of a dynamic query by implementing a select query which brings us the value of a particular column or to select the max of a row.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Differnt Data Types in Sql2000 Sever
Previous Resource: TimpStamp
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use