我只是想使用以下脚本从一些 XML 中获取 JobID 属性。有一个更好的方法吗?
PROCEDURE [dbo].[spProcessJobXML]
@XMLPACKET as nvarchar(MAX) --<Root><Job JobID='2'></Root>
AS
-- Declare XML doc handle
declare @docHandle int
Declare @jobid nvarchar(3);
Declare @parentid int;
declare @testInt int;
-- Create XML Doc
exec sp_xml_preparedocument @docHandle OUTPUT, @XMLPACKET
if exists (
SELECT * FROM tempdb.sys.tables WHERE [name] like '#tempTable%'
)
DROP TABLE tempdb.#tempTable;
SELECT * INTO #tempTable FROM OPENXML (@docHandle, '/Root/Job')
Select top 1 @parentid = #tempTable.id from #tempTable where #tempTable.localname like 'JobID';
--select * from #tempTable;
Select top 1 @jobid = #tempTable.[text] from #tempTable where #tempTable.parentid = @parentid;
SET @testInt = CAST(@jobid AS int)
谢谢