Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to query colum header insql

Author  Topic 

cycleinmars
Starting Member

1 Post

Posted - 2011-03-31 : 12:21:15
Hello,
i am pretty new to sql,
i have a table with 3 columns and 3 rows,
the table is like match the rows and columns type.

if i give the coloum name and the primary key in row it should match and give me the corresponding value. the example is given below

cat I II
A 1 2
B 3 4
C 5 6


if i query I & A it has to return 1.
please help me how do i query the column header

is there any other way of doing so.
Kindly help me.

Regards

praveen

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-31 : 15:49:03
You can do something like this if you know the column names in advance.
create  table #tmp (id int, v1 varchar(32), v2 int);
insert into #tmp values (1,'a','8'),(2,'b','9');

declare @keyId int; set @keyid = 2;
declare @colName varchar(32); set @colName = 'v2';

if @colName = 'v1'
begin
select v1 from #tmp where @keyId = id;
end
if @colName = 'v2'
begin
select v2 from #tmp where @keyId = id;
end
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-03-31 : 16:56:25
The "column header" is the name of an attribute; That is meta-data and SQL programers never use meta data in a query. You are confusing the PHYSICAL output with the ABSTRACT data model. Your approach to RDBMS is completely wrong. The best, simple introduction to RDBMS I know is THE MANGA GUIDE TO DATABASE.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -