Efficient SQL procedure to get data from a table of value pairs of values in a DataSet?
Using SQL Server, enter a table of name value pairs. Each line is basically a userid, contentid, sectionid, parameter, value. Therefore, there is data that I want to display in the table, for example user information. Each bit of information is on its own line, sows, how do I get it into a DataSet for use in a relay? Can I somehow concatenate the strings into one? So I can get multiple parameter / value pairs in one line?
so that...
two lines for user 32:
(param / value)
fname / Bob
lname / Smith
displayed on one line in the repeater as follows:
Bob Smith
Any ideas? Oh yes, and the reason it is in name / value pair format is to adhere to the required standard.
a source to share
Another alternative is PIVOT .
Something like this (untested because I don't have SQL Server right now)
SELECT UserID, [fname] AS firstname, [lname] AS lastname
FROM
(SELECT UserID, value, name from PairTable WHERE UserID=32 ) p
PIVOT
(
value
FOR name IN
( [fname], [lname] )
) AS pvt
a source to share
This table is terrible. No offense; -)
Relational databases just don't do EAV tables very well.
You can also group and execute conditional CASE statements - for example:
SELECT UserID,
MAX(CASE WHEN param = 'fname' THEN value ELSE '' END) AS fname,
MAX(CASE WHEN param = 'lname' THEN value ELSE '' END) AS lname
FROM MyEAVTable
GROUP BY UserID
The PIVOT syntax is great - the only benefit of this solution is that it will work with SQL 2000 as well.
a source to share