为什么值与select不同,查询输出和插入查询输出

时间:2014-01-17 08:37:56

标签: sql-server xml

我执行了以下查询

DECLARE @xmlfile xml set @xmlfile=' <Services>
<ServiceInfo>
    <ServiceName>AeLookupSvc</ServiceName>
    <ServiceState>1</ServiceState>
    <HardwareProfiles>
      <HardwareProfile>
        <ServiceID>2</ServiceID>
        <Profile>Undocked Profile</Profile>
        <State>1</State>
      </HardwareProfile>
     </HardwareProfiles>

  select 
    1 as assetid,
     (select dbo.ServiceMaster.serviceid from dbo.ServiceMaster where dbo.ServiceMaster.servicename=[profile].query('../../ServiceName').value('.', 'varchar(max)'))as serviceid,
     (select [profile].query('State').value('.','bit'))as state,
     1 as profileid
     into #temp
    from 
    @xmlfile.nodes('Services/ServiceInfo/HardwareProfiles/HardwareProfile')as Profiles([profile])
    select * from #temp;

    drop table #temp;

这里的servicemaster是一个包含serviceid和servicename的表

serviceid servicename
1001       AelookupSvc
1002       ALG

上面的查询输出是

assetid serviceid   state   profileid
1          1001         1     1

当我将所选值插入现有表时,它返回

编辑: 插入dbo.AssetServiceState

 select 
        1 as assetid,
         (select dbo.ServiceMaster.serviceid from dbo.ServiceMaster where dbo.ServiceMaster.servicename=[profile].query('../../ServiceName').value('.', 'varchar(max)'))as serviceid,
         (select [profile].query('State').value('.','bit'))as state,
         1 as profileid
 from 
        @xmlfile.nodes('Services/ServiceInfo/HardwareProfiles/HardwareProfile')as Profiles([profile])

此查询提供

   assetid  serviceid   state   profileid
        1      1            1     1

为什么Serviceid值为1?请帮帮我解决这个问题?

0 个答案:

没有答案