Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Stored procedure to retreive data in MS Sql Server

i have a table in below mentioned format:

StateID   ColorID     ColorDesc
21           5             Green
21           18           Red
21            11          A
22           5          Green
22         19          B
23        55          M
27       100       Cofee
27       99        Blue
etc..


I want to stored procedure in MS Sql Server 2005  which should return the output in below format:
sateid   RequiredColorDetails
21         5|Green|18|Red|11|A
22       5|Green|19|B
23      55|M
27      100|Cofee|99|Blue
etc..

Please help me.
0
ram27
Asked:
ram27
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you need the "group_concat" emulation in sql server, with the XML trick:
http://blog.shlomoid.com/2008/11/emulating-mysqls-groupconcat-function.html
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT DISTINCT StateID, (SELECT CAST(ColorID as varchar)+'|' +ColorDesc+'|' FROM urTable n WHERE a.stateid = n.stateid FOR XML PATH('') )
from urTable a
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now