我正在尝试将许多列组合在一起以创建完整的字符串。
这是我的意思:
我有一个名为QRFormats的表,如下所示:
| Id | Text1 | Variable1 | Text2 | Variable2 | Text3 | Variable3 |
| 1 | The Color | Color | Is designated | Description | *NULL* | *NULL* |
| 2 | The Company | Company| Is Located | Location| In Country | Country|
| 3 | The Part is | PartNumber| Work Order:| WorkOrder| *NULL* | CompanyCode |
.
.
完整的字符串应打印 文本 ,并使用FK / PK在完全不同的表中查找 Variable 相关性。
所有 变量 位于两个可能的表中,分别称为 PartData 和 Company
PartData
| Id | PartNumber | Color | WorkOrder | Description |
| 1 | 123456789 | Blue | 111222333 | Microchip |
| 2 | 101441414 | Silver | 55556666 | Handel |
公司
| Id | Company | Location | CompanyCode | Country |
| 1 | Microsoft | Seattle | 1234 | USA |
| 2 | Apple | California | 1122 | USA |
.
我正在尝试做的一些例子。...
。
如果我的话,完整字符串应如下所示:
-QRFormats FK是1:
-PartDataFK是1:
-公司FK是1:
“蓝色表示为Microchip”
。
如果我的话,完整字符串应如下所示:
-QRFormats FK是2:
-PartDataFK是1:
-公司FK是3:
“苹果公司位于美国国家/地区的加利福尼亚州”
。
如果我的话,完整字符串应如下所示:
-QRFormats FK是3:
-PartDataFK是2:
-公司FK是1:
“零件为101441414,工作订单:555566661234”
。
非常感谢您的帮助,我正在Visual Studio 2015中使用SSIS。
答案 0 :(得分:1)
这是一个非常有趣的请求。首先是DML:
create table #QFR
(
Id int
, Text1 varchar(100)
, Variable1 varchar(100)
, Text2 varchar(100)
, Variable2 varchar(100)
, Text3 varchar(100)
, Variable3 varchar(100)
)
insert into #QFR
values
( 1,' The Color ','Color',' Is designated ','Description ',NULL ,NULL )
,( 2,' The Company ','Company',' Is Located ','Location',' In Country ','Country' )
,( 3,' The Part is ','PartNumber','Work Order:','WorkOrder',NULL ,'CompanyCode' )
create table #parts
(
Id int
, PartNumber varchar(100)
, Color varchar(100)
, WorkOrder varchar(100)
, Description varchar(100)
)
insert into #parts
values
(1,'123456789','Blue ','111222333',' Microchip '),
(2,'101441414','Silver ','55556666',' Handel ')
create table #company
(
Id int
, Company varchar(100),
Location varchar(100)
, CompanyCode varchar(100)
, Country varchar(100)
)
insert into #company
values
(1,' Microsoft ','Seattle ','1234',' USA ')
,(2,' Apple ','California ','1122',' USA ')
我创建了一个CTE来翻转变量逻辑,并多次加入以完成这项工作...
declare @qID int = 1
,@pID int = 1
,@cID int = 1
;with cte as
(
select Label,Value
from #parts
cross apply (values('PartNumber',PartNumber),('Color',Color),('WorkOrder',WorkOrder),('Description',Description)) as a(Label,Value)
where id =@pID
union all
select Label,Value
from #company
cross apply (values('Company',Company),('Location',Location),('CompanyCode',CompanyCode),('Country',Country)) as a(Label,Value)
where id =@cID
)
select Text1 , t1.Value , text2 , t2.Value , text3 , t3.Value
from #QFR q
left join cte t1 on q.Variable1=t1.Label
left join cte t2 on t2.Label = q.Variable2
left join cte t3 on t3.label = q.Variable3
where q.id = @qID
已更改为功能:
create function fn_BuildAsentence (@qID int
,@pID int
,@cID int)
returns varchar(max)
as
BEGIN
declare @v varchar(Max)
;with cte as
(
select Label,Value
from parts
cross apply (values('PartNumber',PartNumber),('Color',Color),('WorkOrder',WorkOrder),('Description',Description)) as a(Label,Value)
where id =@pID
union all
select Label,Value
from company
cross apply (values('Company',Company),('Location',Location),('CompanyCode',CompanyCode),('Country',Country)) as a(Label,Value)
where id =@cID
)
select @v = concat(Text1 , t1.Value , text2 , t2.Value , text3 , t3.Value)
from QFR q
left join cte t1 on q.Variable1=t1.Label
left join cte t2 on t2.Label = q.Variable2
left join cte t3 on t3.label = q.Variable3
where q.id = @qID
return @v
END
最后,我将使用一个SQL Source(我想您是在数据流中执行此操作的地方)
Select dbo.fn_BuildAsentence(QFR,parts,company),QFR,parts,company
from [whereever]