there is a table having two columns no and name
and the data is
1 A
2 B
3 C

write a query that will result a horizontal output
A,B,C

Answer Posted / kumar.t

1. First To create the following table.

Create table NewNew(sno int identity(1,1) primary key,
names varchar(100))

2. To INsert the record one by one

Insert Into NewNew Values ('A')
Insert Into NewNew Values ('B')
Insert Into NewNew Values ('C')
Insert Into NewNew Values ('D')
Insert Into NewNew Values ('F')

3. To view the records first

Select * From NEwNew

4. To create the following procedure

Create Proc New1
As
Begin
Declare @names varchar(100)
Declare @sQuery Varchar(7000)
Declare @sQuery1 Varchar(1000)
Declare @ans varchar(8000)
Declare @i int
Declare cur cursor for select names from newnew
Open cur
Set @i = 1
Set @sQuery1 = ''
Set @sQuery = ''
fetch next from cur into @names
While @@fetch_status=0
Begin
Set @sQuery1 = '(Select Top 1 names
from newnew where names in (select top '+ convert(varchar
(3),@i) +' names From newnew order by 1 Asc) order by 1
Desc) As names,'
Set @sQuery = @squery + @squery1
Set @i = @i + 1
Fetch next from cur into @names
End
Set @ans = Convert(varchar(6),'Select ') +
Left(@sQuery,Len(@sQuery)-1)
Exec (@ans)
Close cur
deallocate cur
End

5. To execute the procedure
now to view the output Ok

Exec New1


By
Kumar.T

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many types of the database links?

591


How can you check the version of sql server?

551


What is transact-sql language?

570


What are cursors stored procedures and triggers?

506


What is the fastest way to permanently delete a 1 million row table named customers?

586






How to create a new schema in a database?

544


What is a covered index?

511


What is a document index?

579


What is difference between getdate and sysdatetime in sql server 2008?

592


Explain what is meant by replication of database?

522


What is a fill factor?

612


What do you understand by triggers?

512


Explain about service Broker functions?

606


How to list all stored procedures in the current database using ms sql server?

628


What is difference between stored procedure and user defined function?

541