|
我有以下表格:5 ^( t1 Y7 z( s1 H
tblengineeringlookupcolumnmaster
4 a8 `; Y3 w: Y! o7 Ielccolumnid | elclookupcode | elccolumnname | elcisrequired | 64 | FirstName | | 64 | LastName | | 5555 | abc | 14 | 5555 | xyz | 1tblengineeringlookupdetail8 }7 [" y" f9 L9 ^, ?
eldrecordId | eldlookupcode |eldlookupsequence |eldlookupvalue | eldlookupvaluedescription245 | | | Red | Aravinth,Arumugam246 | | 1 | Blue | Santhosh,Chandran247 | | 2 | Green | Karthik,Balasubramanian当我把 64作为参数传递给过程时。我得到的输出是:2 n( h, l/ \7 W" y6 w; }! {
FirstName | LastName | eldRecordId -------------------------------------Aravinth | Arumugam | Santhosh | Chandran | 246Karthik | Balasubramanian| 247使用的存储过程是
; R) X& B% G$ {4 g) [9 _3 _) G* Q0 S// SP% w8 y9 s2 _( g# v$ ?
-- Select the columns to be usedDECLARE @tcol TABLE( ID INT IDENTITY ,elclookupcode INT ,elccolumnname VARCHAR(100))-- Insert the records into the tableINSERT INTO @tcol (elclookupcode,elccolumnname)SELECT elclookupcode,elccolumnname FROM tblEngineeringLookupColumnMaster WHERE elclookupcode=@LookupCode-- Select the columns which should be as output as a tableDECLARE @temp TABLE( elcLookupCode INT RecordId INT txt VARCHAR(8000))-- Select the records from the table and insertINSERT INTO @temp (elcLookupCode,RecordId,txt)SELECT eldLookupCode,eldRecordId,eldLookupValueDescription FROM tblEngineeringLookupDetail WHERE eldLookupCode=@LookupCodeDECLARE @SQL NVARCHAR(MAX)-- Have a table for the selected values;WITH cte AS SELECT token = ,[ d2.elccolumnname = d.token '''' d.RecordId FROM ( SELECT token = t.c.value('.','VARCHAR a.RecordId a.elcLookupCode rn = ROW_NUMBER() OVER (PARTITION BY a.RecordId ORDER BY a.RecordId) FROM ( SELECT RecordId elcLookupCode txml = CAST('' REPLACE(txt,',',' ' AS XML) FROM @temp a CROSS APPLY txml.nodes('/t') t(c) ) d -- Select the columns to be mapped JOIN ( SELECT elclookupcode elccolumnname rn = ROW_NUMBER() OVER (PARTITION BY elclookupcode ORDER BY elclookupcode) FROM @tcol ) d2 ON d.elcLookupCode = d2.elclookupcode AND d2.rn = d.rn)-- Join all the records takenSELECT @SQL = STUFF(( SELECT CHAR(13) UNION ALL SELECT ' STUFF(( SELECT t2.token FROM cte t2 WHERE t2.RecordId = t.RecordId FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX),[RecordId] = ' CAST(RecordId AS VARCHAR(10) FROM @temp t FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)1、11、')PRINT @SQLEXEC sp_executesql @SQLEND现在的问题是:
7 h; f' r9 \6 ~如果我将‘65’作为参数传递给这个过程,在第一个表中(tblengineeringlookupcolumnmaster)在第二个表中有值(tblengineeringlookupdetail)在没有相应值的情况下,我没有结果(显示消息3rowsaffected)。
* j7 Y0 A9 t9 ?: z/ n" s; |但我必须一个人去做columns标题。0 a" p& V% |. j) N8 ?. q
‘样本:+ {1 K. n& G) J+ F, ]
xyz | abc | eldrecordIdNULL |NULL | NULL为什么我没有得到这个?我哪里错了?
5 v' U: g2 `% r8 i6 S. f$ W. g
; O `; _+ B, { 解决方案: |
|