Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. required help for sql query

required help for sql query

Scheduled Pinned Locked Moved Database
databasehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalyan_vb
    wrote on last edited by
    #1

    I have an problem in sequel query. i.e I have 2 table where master child relationship exits Table1 CorrespondentCodePK, ContactCode,PartNo, the data in Table1 is 1,1,1 2,1,1 3,1,2 Table2 CorrespondentCodeFK,PartNo,PartName, the data in Table2 is 1,1,Part1 1,1,Part2 2,1,Part1 and I want the result in the bellow mentioned format i.e CorrespondentCodePK,ContactCode,PartName (i.e all part names for the CorrespondentCodePK should come in PartName column as a one record) 1,1,Part1Part2 2,1,Part1

    E R 2 Replies Last reply
    0
    • K kalyan_vb

      I have an problem in sequel query. i.e I have 2 table where master child relationship exits Table1 CorrespondentCodePK, ContactCode,PartNo, the data in Table1 is 1,1,1 2,1,1 3,1,2 Table2 CorrespondentCodeFK,PartNo,PartName, the data in Table2 is 1,1,Part1 1,1,Part2 2,1,Part1 and I want the result in the bellow mentioned format i.e CorrespondentCodePK,ContactCode,PartName (i.e all part names for the CorrespondentCodePK should come in PartName column as a one record) 1,1,Part1Part2 2,1,Part1

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      First, make a function: NOTE: The lengths of the varchar variables will depend upon your data.

      CREATE FUNCTION GetPartNames (@nCorrCode int)  
      RETURNS  varchar(1000) AS  
      BEGIN 
      
      DECLARE @cPartNames varchar(1000)
      DECLARE @cPartName varchar(50)
      
      SELECT @cPartNames = ''
      
      DECLARE  partcursor CURSOR FOR 
      SELECT DISTINCT PartName 
      FROM table2
      WHERE correspondentcodefk = @nCorrCode
      
      OPEN partcursor
      FETCH NEXT FROM partcursor INTO @cPartName 
      WHILE @@FETCH_STATUS = 0
      BEGIN
      	select @cPartNames = @cPartNames + @cPartName
      	FETCH NEXT FROM partcursor INTO @cPartName 
      END
      
      CLOSE partcursor
      DEALLOCATE partcursor
      RETURN(@cPartNames)
      END
      

      Next, use this SQL:

      SELECT correspondentcodepk, contactcode, dbo.GetPartNames(correspondentcodepk) AS PartNames FROM table1
      

      ---------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      1 Reply Last reply
      0
      • K kalyan_vb

        I have an problem in sequel query. i.e I have 2 table where master child relationship exits Table1 CorrespondentCodePK, ContactCode,PartNo, the data in Table1 is 1,1,1 2,1,1 3,1,2 Table2 CorrespondentCodeFK,PartNo,PartName, the data in Table2 is 1,1,Part1 1,1,Part2 2,1,Part1 and I want the result in the bellow mentioned format i.e CorrespondentCodePK,ContactCode,PartName (i.e all part names for the CorrespondentCodePK should come in PartName column as a one record) 1,1,Part1Part2 2,1,Part1

        R Offline
        R Offline
        r stropek
        wrote on last edited by
        #3

        If you have SQL 2005 you could also write a short user-defined aggregation function in C# that concats strings. Regard, Rainer. Rainer Stropek Visit my blog at http://www.cubido.at/rainers

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups