竞争无处不在,青春永不言败!专业撸代码,副业修bug

Talk is cheap , show me the code!



读取 xml 文件 获取其中保存的数据信息

 

建立一个存储过程来返回要读取的数据形成结果集:

CREATE PROC dbo.getValuesFromXmlByPath
@fileName NVARCHAR(128)
as
DECLARE @T XML,
@idoc INT,
@existFlag int
EXEC xp_fileexist @fileName,@existFlag OUTPUT
IF @existFlag=1
BEGIN

DECLARE @xmltab TABLE
(
[Alias] VARCHAR(15),
[ServerName] VARCHAR(15),
[DatabaseName] VARCHAR(63)
)

DECLARE @sqlCmd NVARCHAR(max)='SELECT @T=BULKCOLUMN FROM OPENROWSET(BULK '''+@fileName+''',SINGLE_BLOB) AS BULKCOLUMN'
EXECUTE SP_EXECUTESQL @sqlCmd,N'@T XML OUTPUT',@T=@T OUTPUT
EXECUTE SP_XML_PREPAREDOCUMENT @idoc OUTPUT,@T

INSERT INTO @xmltab
( Alias, ServerName, DatabaseName )
SELECT [Alias],
[location],
[name]
FROM OPENXML(@idoc,'/databases/database',1)
with(
[alias] VARCHAR(15),
[location] VARCHAR(15),
[name] VARCHAR(63)
)

EXECUTE SP_XML_REMOVEDOCUMENT @idoc
SELECT * FROM @xmltab

END

posted @ 2016-09-16 23:41  云雾散人  阅读(435)  评论(0编辑  收藏  举报

Your attitude not your aptitude will determine your altitude!

如果有来生,一个人去远行,看不同的风景,感受生命的活力!