declare @x xml;
declare @DocHandle int;
select @x=(select EmployeeAllwDedDetail1.EmpAllowanceDeduction from EmployeeAllwDedDetail1 where EmployeeKey in(select EmployeeKey from EmployeeMaster where EmployeeMaster.EmployeeID in(SELECT * FROM string_split('136,137', ',')) )
--and MONTH(EmployeeAllwDedDetail1.EffectiveDate) = 2
--and YEAR(EmployeeAllwDedDetail1.EffectiveDate) = 2018)
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @x
-- Execute a SELECT statement using OPENXML rowset provider.
declare @temptbl1 table(Name1 varchar(20),Amount decimal(10,2))
declare @temptbl2 table(Name1 varchar(20),Amount decimal(10,2))
insert into @temptbl1
SELECT *
FROM OPENXML (@DocHandle, '/AllowanceDeduction/Allowance',1)
WITH (Name varchar(10),
Amount decimal(10,2))
EXEC sp_xml_removedocument @DocHandle
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @x
insert into @temptbl2
SELECT *
FROM OPENXML (@DocHandle, '/AllowanceDeduction/Deduction',1)
WITH (Name varchar(10),
Amount decimal(10,2))
EXEC sp_xml_removedocument @DocHandle
;with cteTemp as(
select * from @temptbl1 union all
select * from @temptbl2)
select * from cteTemp
我正在尝试运行此查询以返回多个值,但是当我将cindu = ition完全放入一个记录时它返回单个值,否则它会显示子查询错误..请您转发给我的解决方案< / p>
答案 0 :(得分:0)
问题是当您在string_split
函数中提供多个逗号分隔值时。您正在尝试为@x
变量分配多个值。尝试下面的代码(这里我们使用变量表来保存多个值并使用while循环) -
declare @tmp_tbl table (id int identity(1,1), xml_data xml);
declare @x xml;
declare @DocHandle int;
declare @max int, @count int=1;
declare @temptbl1 table(Name1 varchar(20),Amount decimal(10,2))
declare @temptbl2 table(Name1 varchar(20),Amount decimal(10,2))
insert into @tmp_tbl (xml_data)
select EmployeeAllwDedDetail1.EmpAllowanceDeduction from EmployeeAllwDedDetail1
where EmployeeKey in(select EmployeeKey from EmployeeMaster where EmployeeMaster.EmployeeID in(SELECT * FROM string_split('136,137', ',')))
--and MONTH(EmployeeAllwDedDetail1.EffectiveDate) = 2
--and YEAR(EmployeeAllwDedDetail1.EffectiveDate) = 2018)
select @max = max(id) from @tmp_tbl
while @count <= @max
begin
select @x = xml_data from @tmp_tbl where id = @count
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @x
-- Execute a SELECT statement using OPENXML rowset provider.
insert into @temptbl1
SELECT *
FROM OPENXML (@DocHandle, '/AllowanceDeduction/Allowance',1)
WITH (Name varchar(10),
Amount decimal(10,2))
EXEC sp_xml_removedocument @DocHandle
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @x
insert into @temptbl2
SELECT *
FROM OPENXML (@DocHandle, '/AllowanceDeduction/Deduction',1)
WITH (Name varchar(10),
Amount decimal(10,2))
EXEC sp_xml_removedocument @DocHandle
set @count = @count+1
end
;with cteTemp as(
select * from @temptbl1 union all
select * from @temptbl2)
select * from cteTemp