Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in database there are 4 columns .

ProductName Price Specification Value

keyboard 560 circuit yes
keyboard 560 techs no
keyboard 560 multi good
so, i required productName and price only one time ...how to create store procedure for this problem???

ProductName Price Specification Value

keyboard 560 circuit yes
techs no
multi good.
i required that type of output
Posted
Updated 27-Mar-13 0:03am
v2

Write a separate Stored Procedure for each need.
 
Share this answer
 
Test it:
SQL
CREATE TABLE #tbl (ProductName VARCHAR(30), Price MONEY, Specification VARCHAR(30), [Value] VARCHAR(10))

INSERT INTO #tbl (ProductName, Price, Specification, [Value])
VALUES('keyboard', 560, 'circuit', 'yes')
INSERT INTO #tbl (ProductName, Price, Specification, [Value])
VALUES('keyboard', 560, 'techs', 'no')
INSERT INTO #tbl (ProductName, Price, Specification, [Value])
VALUES('keyboard', 560, 'multi', 'good')


DECLARE @cols VARCHAR(300)
DECLARE @dt VARCHAR(2000)
DECLARE @pt VARCHAR(4000)

SET @cols = STUFF((SELECT DISTINCT '],[' + Specification
					FROM #tbl 
					ORDER BY '],[' + Specification
			FOR XML PATH('')),1,2,'') + ']'


SET @dt = 'SELECT ProductName, Price, Specification, ValID = CASE WHEN [Value]=''no'' THEN 0 WHEN [Value]=''yes'' THEN 1 ELSE 2 END ' + 
			'FROM #tbl'
--EXEC(@dt)
SET @pt = 'SELECT ProductName, Price, ' + @cols + ' ' +
			'FROM (' + @dt + ') AS DT ' +
			'PIVOT(MAX(ValID) FOR Specification IN(' + @cols + ')) AS PT' 
EXEC(@pt)

DROP TABLE #tbl


In above example is use simple trick. I "convert" values from [Value] column into numeric values: no->0; yes->1, other->2. Why? To use agregate function and to create pivot table.
Result:
ProductName     Price   cicuit  multi   techs
keyboard	560,00	1	2	0
 
Share this answer
 
SQL
SELECT CASE WHEN y.rowNumber = 1 THEN 'keyboard' ELSE '' END AS ProductName, CASE WHEN y.rowNumber = 1 THEN '560' ELSE '' END AS price, y.Specification, y.Value
FROM (
   SELECT Row_Number() OVER (ORDER BY Specification) AS rowNumber, x.Specication, x.Value
   FROM (
      SELECT Specification, Value
      FROM Table
      WHERE ProductName = 'keyboard' AND price = 560
   ) x
) y
 
Share this answer
 
Comments
Ankit Gajera 28-Mar-13 2:44am    
if you can seprate the store procedure then how can you call tne two store prcedure inside the .cs page
ZurdoDev 28-Mar-13 7:12am    
You can use two different SqlCommand objects. Or execute the first and then modify your SqlCommand and execute the second. It all depends on what your code looks like for calling them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900