如標題所述,詳情可看程式中的註解~
DECLARE @pi_sXMLData AS NVARCHAR(MAX)
DECLARE @objXML AS XML
DECLARE @bIsAGoodCompany AS BIT
DECLARE @bIsASmallCompany AS BIT
SET @pi_sXMLData=N'
<Root>
<Lavel1>
<BasicInfo EmpCHName="王大維"></BasicInfo>
<DetailInfo CompanyCHName="XX公司中文名稱">
<IsAGoodCompany>Y</IsAGoodCompany>
<IsASmallCompany>N</IsASmallCompany>
</DetailInfo>
</Lavel1>
</Root>'
SET @objXML = CAST(@pi_sXMLData as xml)
SELECT
@bIsAGoodCompany =
CASE DetailInfoXML.Col.value('.', 'varchar')
When 'Y' Then 1 When 'N' Then 0 Else null End
FROM @objXML.nodes('/Root/Lavel1/DetailInfo/IsAGoodCompany') AS DetailInfoXML(Col)
-- 這邊要進行兩次的 Select 去賦予兩個變數值
-- 有沒有辦法讓他在一個 Select 中就做完呢?
SELECT
@bIsASmallCompany =
CASE DetailInfoXML.Col.value('.', 'varchar')
When 'Y' Then 1 When 'N' Then 0 Else null End
FROM @objXML.nodes('/Root/FormInfo/DetailInfo/IsASmallCompany') AS DetailInfoXML(Col)
SELECT
@bIsAGoodCompany as AGoodCompany,
@bIsASmallCompany as ASmallCompany
這樣呢
DECLARE @pi_sXMLData AS NVARCHAR(MAX)
DECLARE @objXML AS XML
DECLARE @bIsAGoodCompany AS BIT
DECLARE @bIsASmallCompany AS BIT
SET @pi_sXMLData=N'
<Root>
<Lavel1>
<BasicInfo EmpCHName="王大維"></BasicInfo>
<DetailInfo CompanyCHName="XX公司中文名稱">
<IsAGoodCompany>Y</IsAGoodCompany>
<IsASmallCompany>N</IsASmallCompany>
</DetailInfo>
</Lavel1>
</Root>'
SET @objXML = CAST(@pi_sXMLData as xml)
select @bIsAGoodCompany = case DetailInfoXML.Col.query('./IsAGoodCompany').value('.', 'varchar')
When 'Y' Then 1 When 'N' Then 0 Else null End,
@bIsASmallCompany = case DetailInfoXML.Col.query('./IsASmallCompany').value('.', 'varchar')
When 'Y' Then 1 When 'N' Then 0 Else null End
FROM @objXML.nodes('/Root/Lavel1/DetailInfo') AS DetailInfoXML(Col)
SELECT
@bIsAGoodCompany as AGoodCompany,
@bIsASmallCompany as ASmallCompany
w4560000
不好意思,方便請教這樣的語法( .col.query
)我可以上那裡找到說明呢?
我找了好久都沒有找到 @@
google "sql xml query" 就有資料了
https://docs.microsoft.com/zh-tw/sql/t-sql/xml/query-method-xml-data-type?view=sql-server-ver15
謝謝!