Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear Friends,

Here i have a Problem in my database sqlserver my table design is like this

Table:

txtAge Total
Below6 3
6 7
7 11
8 18
9 19


Now i want to Display my table like this

txage:Below6 6 7 8 9
Total: 3 7 11 18 19

How can change to horizontal

Sample iam giving my query:



SELECT ntblAge.txtAge, tblPrimaryStandard1RegistrationByAge.intMale + tblPrimaryStandard1RegistrationByAge.intFemale AS Total
FROM tblPrimaryStandard1RegistrationByAge INNER JOIN
ntblAge ON tblPrimaryStandard1RegistrationByAge.intAgeId = ntblAge.intAgeId INNER JOIN
tblSchool ON tblPrimaryStandard1RegistrationByAge.intSchoolID = tblSchool.intSchoolID
WHERE (tblPrimaryStandard1RegistrationByAge.intSchoolID = 58)


Please solve this .


Regards,

Anilkumar.D
Posted
Comments
Sergey Alexandrovich Kryukov 11-Nov-11 0:52am    
How "display" is related to SQL?
--SA
Anil Honey 206 11-Nov-11 1:04am    
Actually i Dont know the Depth Concept Regarding this.So now i want to show the table ion Horizontal Fomat.In Reports i want to Display
txage:Below6 6 7 8 9
Total: 3 7 11 18 19
in Horizontal Manner.

1 solution

It is not possible with the query for this u have to write your own store procedure.

Example
Create Table of name tlbTextData
SQL
create table tlbTextData (fldTextKey varchar(10), fldTextVal varchar(10))
insert into tlbTextData values('One','1')
insert into tlbTextData values('Two','2')
insert into tlbTextData values('Three','3')
insert into tlbTextData values('Four','4')
insert into tlbTextData values('Five','5')


Strored Procedure
SQL
DECLARE @ColNameAS VARCHAR(10),
		@RowElement AS VARCHAR(10),
		@SqlQuery AS NVARCHAR(300),
		@iColumnName AS INT,
		@iFirstTime AS INT,
		@AddColName AS NVARCHAR(10)

SELECT @iFirstTime=0

CREATE TABLE #tlbMemTable (fldColName VARCHAR(10))
   
DECLARE GetColumnName CURSOR FOR      
           SELECT column_name FROM Information_Schema.Columns WHERE table_name='tlbTextData'
OPEN GetColumnName      
FETCH NEXT FROM GetColumnName INTO @ColName     
WHILE(@@FETCH_STATUS = 0)    
BEGIN--1 
	INSERT INTO #tlbMemTable(fldColName) VALUES(@ColName)
	SET @iColumnName = 0 
	SELECT @SqlQuery = 'DECLARE GetRow CURSOR FOR SELECT '+ @ColName +' FROM tlbTextData' 
	EXEC sp_executesql @SqlQuery   
	OPEN GetRow
	FETCH NEXT FROM GetRow INTO @RowElement 
	WHILE(@@FETCH_STATUS = 0)    
	BEGIN--2 
		
		SELECT @AddColName = 'VAL'+CAST(@iColumnName AS VARCHAR(10))
		print @AddColName
		IF(@iFirstTime=0)
		BEGIN
			SELECT @SqlQuery = 'ALTER TABLE #tlbMemTable ADD '+@AddColName+' NVARCHAR(10)'
			EXEC(@SqlQuery)
		END
		
		SELECT @SqlQuery = 'UPDATE #tlbMemTable SET '+@AddColName+'='''+@RowElement+''''+' WHERE fldColName='''+@ColName +''''
		EXEC(@SqlQuery)		
		SET @iColumnName = @iColumnName + 1
		FETCH NEXT FROM GetRow INTO @RowElement 
	END--2
	CLOSE GetRow      
	DEALLOCATE GetRow
	SET @iFirstTime = 1
	FETCH NEXT FROM GetColumnName INTO @ColName   
END--1      
CLOSE GetColumnName      
DEALLOCATE GetColumnName   

SELECT * FROM #tlbMemTable
DROP TABLE #tlbMemTable


Note : Please optimize above sp if ur going to use it :)
 
Share this answer
 
v2
Comments
Anil Honey 206 11-Nov-11 0:50am    
Can u give me any other Article.Due to security reasons i cant open this link

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