在SQL中检索上传文件的文件名
我一直在寻找有关如何使用 SQL Server 获取文件名的解决方案。我知道如果您使用 C#,这是可能的。但在 SQL 中它是如何完成的呢?
例如,我有一个位于 C:\ 的文件(例如:uploadfile.txt)即将上传。我有一个表,其中有一个字段“文件名”。如何获取该文件的文件名?
这是我目前拥有的脚本。
-- Insert to table
BULK INSERT Price_Template_Host
FROM 'C:\uploadfile.txt'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
-- Insert into transaction log table filename and datetime()
I've been searching for a solution on how to get the filename of using SQL Server. I know that it's possible if you're using C#. But how is it done in SQL?
For example, I have a file (example: uploadfile.txt) located in C:\ that is about to be uploaded. I have a table which has a field "filename". How do I get the filename of this file?
This is the script that I have as of the moment.
-- Insert to table
BULK INSERT Price_Template_Host
FROM 'C:\uploadfile.txt'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
-- Insert into transaction log table filename and datetime()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,T-SQL 中没有直接的方法来定位文件系统上的文件。毕竟这不是该语言的用途。从您提供的脚本中,BULK INSERT 要求完全限定的文件名称在语句调用时已经已知。
当然,您可以通过多种方式识别/定位文件,除了使用 T-SQL(例如使用 SSIS)之外,也许您可以使用 xp_cmdshell(有安全警告),或在 SQL Server 中创建托管代码模块来执行此任务。
为了向您提供具体的指导,如果您可以向我们所有人提供您尝试实施的业务流程的详细信息,可能会有所帮助。
To the best of my knowledge, there is no direct method in T-SQL to locate a file on the file system. After all this is not what the language is intended to be used for. From the script you have provided, BULK INSERT requires that the fully qualified file name already be known at the time of the statement call.
There are of course a whole variety of ways you could identify/locate a file, outside of using T-SQL for example using SSIS, perhaps you could use xp_cmdshell (has security caveats), or create a managed code module within SQL Server to perform this task.
To provide you with specific guidence, it may help if you could provide us all with details of the business process that you are trying to implement.
我个人会将此问题附加到 SSI 包中,这将为您在加载和后续日志记录方面提供更大的灵活性。但是,如果您打算通过 T-SQL 执行此操作,请考虑执行动态构造的 SQL:
如果我正确理解您的问题,这将参数化文件名,以便您可以在脚本中进一步捕获它。
I would personally attach this problem with an SSIs package, which would give you much more flexibility in terms of load and subsequent logging. However, if you're set on doing this through T-SQL, consider exec'ing dynamically-constructed SQL:
If I understand your question correctly, this paramaterizes the filename so that you can capture it further down in the script.