Click here to Skip to main content
15,887,320 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].[ProcedureName] 
as
begin

declare @Course varchar(20),
        @NoofStudents varchar(20),
        @Rowcount int,
        @batchid varchar(20),
        @CourseDate datetime

       set @CourseDate = getdate();
   
create table #TempTable (course varchar(10), Noofstudents varchar(10))

begin tran
declare courses Cursor for
     select cmn_minor_code as Course_Name,cbm_batch_id as Batch_ID from co_batch_master where cbm_active <> 'D' and cbm_batch_start_dt = @CourseDate
open courses
fetch next from courses into @Course,@batchid
while @@Fetch_status = 0 
	begin
      begin tran 
          declare studentcount cursor for
             select   count(*)  from batch_course_registration a,course_registration b
	         where b.cr_bill_no = a.cr_bill_no and a.bcr_batch_id = @batchid and b.cr_active = 'A'
          open studentcount
             fetch next from studentcount into @Rowcount
             while @@Fetch_status = 0
			 begin
                insert into #TempTable values(@Course,@batchid)
		     fetch next from studentcount into @Rowcount
	         end
         close studentcount
		 deallocate studentcount
      commit tran
 fetch next from courses into @Course,@batchid
 end
 close courses
 Deallocate courses
 commit tran
select * from #TempTable
end


When i run the store procedure shows output as follows
exec [ProcedureName]

Output as follows
course   Noofstudents
 AFF      10



From my above output i want to display in single line as follows
Dear Faculty, AFF candidates is 10.

i want to show in single line as senetence.

for that how can i do in sql.

Regards,
Narasiman P.
Posted
Updated 9-May-14 3:27am
v2

1 solution

Little suggestion: use UI instead of SQL

But...
SQL
CREATE PROCEDURE GetInfo(@CourseName VARCHAR(30))
AS
BEGIN

DECLARE @tmp TABLE (CourseName VARCHAR(30), CountOfStudents INT)

INSERT INTO @tmp (CourseName, CountOfStudents)
SELECT C.CourseName, COUNT(S.StudentId) AS CountOfStudents
FROM CourseTable AS C INNER JOIN StudentTable AS S ON C.StuID = S.StuID
WHERE C.CourseName = @CourseName

DECLARE @stu INT
SELECT @stu = CountOfStudents
FROM @tmp

SELECT 'Dear Faculty, ' + @CourseName + ' candidates is: ' + CONVERT(VARCHAR(10), @stu) + '.' AS Info

END
 
Share this answer
 
Comments
CHill60 9-May-14 9:44am    
Beat me to it! Except I used SELECT 'Dear Faculty, ' + course + ' candidates is ' + LTRIM(CAST(SUM(Noofstudents) AS VARCHAR(5))) FROM #TempTable GROUP BY course
Maciej Los 9-May-14 11:03am    
My virtual 5!

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