我有示例数据:
表:tblsampledata
create table tblsampledata
(
column1 varchar(50),
column2 varchar(50)
);
insert into tblsampledata values('Bob Frapples','Gail Forcewind');
insert into tblsampledata values('Paul Molive','Mario Speedwagon');
我有带有表名的列映射表:
表:tblmapping
create table tblmapping
(
tblname varchar(100),
columnmap varchar(max)
);
insert into tblmapping values('tblsampledata','[column1]|[column2]');
注意:我想在表tblmapping
的表名tblname
中拆分存在的列数据,并将其存储到临时表中。
预期结果:#TempTable
column1 column2
---------------------
Bob Gail
Frapples Forcewind
Paul Mario
Molive Speedwagon
答案 0 :(得分:3)
您需要使用动态查询来实现这一目标。
您可以尝试以下操作。
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "Error trying login and get user Context. Error: Error trying to enroll user. Error: Enrollment failed with errors [[{\"code\":20,\"message\":\"Authorization failure\"}]]",
"stack": "Error: Error trying login and get user Context. Error: Error trying to enroll user. Error: Enrollment failed with errors [[{\"code\":20,\"message\":\"Authorization failure\"}]]\n at HLFConnection.login (/home/composer/.npm-global/lib/node_modules/composer-rest-server/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:477:30)\n at <anonymous>\n at process._tickCallback (internal/process/next_tick.js:189:7)"
}
}
要拆分第1列和第2列,可以使用如下查询。
select @xml = Cast(( '<X>' + Replace(columnmap, '|', '</X><X>') + '</X>' ) AS XML)
from tblmapping where tblname =@tablename
DECLARE @query AS NVARCHAR(max) = 'select ' + Stuff((SELECT DISTINCT ', ' + value
FROM (
SELECT n.value('.', 'varchar(100)') AS value
FROM @xml.nodes('X') AS T(n)
)t
FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
+ ' from ' + @tablename;
exec sp_executesql @query
Full Demo使用动态查询