Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[Shortcode_Accommodation] (@Keyword varchar(50))
as

declare @Hotel varchar(25),
@Phone varchar(25),
@final varchar(max)
 
create table #TempTable(res varchar(max))  
begin
insert into #Temptable(res) values('Dear Students, Hotel Accommodation is');
insert into #Temptable(res)
select CONVERT(varchar, ROW_NUMBER() over(order by Hotel_Name))+'. '+Hotel_Name+','+Phoneno+''
from Tb_Accommodation where Active <> 'D' and Description = @Keyword
insert into #Temptable(res) values('By marine');

select * from #Temptable 
end

when I execute the above store procedure output as follows
SQL
exec [Shortcode_Accommodation] 'Accommodation'

Quote:
Dear Students, Hotel Accommodation is
1. HOTEL RAAJ BHAAVAN,26404621/22
2. HOTEL SHRI VALLI RESIDENCY(NEW RAJ BHAVAN),26422297/42668009
3. SATKAR LODGE,9380197278/26425881/33570477
By marine
But i want the output to be needed in single line as follows
Quote:
Dear Students,Hotel Accommodation is 1.HOTEL RAAJ BHAAVAN,26404621/22 2.HOTEL SHRI VALLI RESIDENCY(NEW RAJBHAVAN),26422297/42668009 3.SATKAR LODGE,9380197278/26425881/33570477 By marine

for that in my above store procedure what is the mistake I made.

please help me.

regards,
narasiman P.
Posted
Updated 1-Mar-14 0:37am
v2
Comments
Maciej Los 1-Mar-14 6:38am    
Do not repost: i want the output in row not in column using store procedure[^]!
Reposting is threated as abuse.
At this moment you have posted 631 questions!
Tom Marvolo Riddle 1-Mar-14 7:01am    
Hi Maciej Los.Did you see his profile?Out of 631 question ,he is not accepting a single answer(AFAIK) and even didn't upvote/saying Thanks.Nothing from him.What do you think?
Maciej Los 1-Mar-14 7:06am    
My opinion about OP changes nothing. My advice for Him: buy a book and start with basics ;)
Cheers
Maciej
Tom Marvolo Riddle 1-Mar-14 7:10am    
Thank you for the opinion.Cheers :)

1 solution

you can use cursor:

SQL
declare @m1 as varchar(max);
declare @m2 as varchar(max);
set @m1 = '';
set @m2 = '';

declare dircursor cursor for select * from #Temptable;
open dircursor;
fetch next from dircursor into @m2;
while @@FETCH_STATUS=0
begin
    set @m1 = @m1 +' '+ @m2;
    fetch next from dircursor into @m2;
end
close dircursor;
deallocate dircursor

print @m1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900