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.
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 SettingValue1 "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 4I 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]))sPIVOT (MAX([Value]) FOR [Name] IN ([Name1],[Name2],[Name3]))p