Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 belowcat I IIA 1 2B 3 4C 5 6if i query I & A it has to return 1.please help me how do i query the column headeris there any other way of doing so.Kindly help me.Regardspraveen
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;endif @colName = 'v2' begin select v2 from #tmp where @keyId = id;end
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 PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL