简体   繁体   English

使用MS SQL查找和读取XML文件

[英]Finding and Reading a XML file using MS SQL

I have an SQL script which I want to run on multiple databases. 我有一个要在多个数据库上运行的SQL脚本。 The script runs a couple of update and insert statements and I also want to open and parse an xml file on different paths. 该脚本运行几个update和insert语句,我也想在不同的路径上打开和解析xml文件。
The issue I have is that I know where the file I want to open is (the directory) but I don't know its name (the name is random) but the extension is always .profile (which is a xml file) and there is only one file in each directory. 我遇到的问题是,我知道要打开的文件在哪里(目录),但我不知道它的名称(名字是随机的),但是扩展名始终是.profile (这是一个xml文件),在那里每个目录中只有一个文件。
I wonder how I can open a XML/profile file without knowing its exact name using MS SQL. 我想知道如何在不使用MS SQL知道确切名称的情况下打开XML /配置文件。

As far as I understand your question correctly: 据我正确理解您的问题:

declare @files table (ID int IDENTITY, fileName varchar(max))
insert into @files execute xp_cmdshell 'dir <yourdirectoryhere>\*.profile /b'
declare @fileName varchar(max)
select top 1 @fineName = fileName * from @files

does what you want but is based on calling xp_cmdshell and it's usually a very bad idea to use it . 做你想要的,但是基于调用xp_cmdshell ,使用它通常是一个非常糟糕的主意

Try something along the lines of this: 尝试一些类似的方法:

DECLARE @output NVARCHAR(MAX) 

CREATE TABLE #OUTPUT 
  ( 
     OUTPUT VARCHAR(255) NULL 
  ) 

INSERT #OUTPUT 
EXEC @output = XP_CMDSHELL 
  'DIR "C:\temp\*.profile" /B ' 

SELECT * 
FROM   #OUTPUT 

DROP TABLE #OUTPUT 

As explained here (and that's just one way), you can access disk contents from SQL Server, provided your permissions are working fine. 正如解释这里 (这只是单程),您可以访问从SQL Server磁盘内容,提供您的权限工作正常。

IIRC, the following options need to be enabled. IIRC,需要启用以下选项。 However, you need them anyway to access files from SQL Server. 但是,无论如何,您都需要它们才能从SQL Server访问文件。

EXEC sp_configure 'show advanced options', 1
GO

RECONFIGURE
GO

EXEC sp_configure 'xp_cmdshell', 1
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM