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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get row from settings table

Author  Topic 

trouble2
Constraint Violating Yak Guru

267 Posts

Posted - 2012-11-14 : 05:10:42
Hi, I was wondering about the best way to create a sp for something like this.
There is a settings table which looks like this:
ID SettingName SettingValue
1 "Name 1" "Value 1"
2 "Name 2" "Value 2"
3 "Name 3" "Value 3"
4 "Name 4" "Value 4"
etc.

Now, I want to get one row with the value's of a couple of columns, like:

Value 1 Value 2 Value 4

I know you can do this with multiple selects, but is there a better way?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-14 : 06:46:36
You can use PIVOT operator. The code would be something like this:
SELECT
[Name1],[Name2],[Name3]
FROM
(
SELECT
[Name],
[Value]
FROM
YourTable
WHERE
[Name] IN ([Name1],[Name2],[Name3])
)s
PIVOT (MAX([Value]) FOR [Name] IN ([Name1],[Name2],[Name3]))p
Go to Top of Page
   

- Advertisement -