在SQL中检索上传文件的文件名

发布于 2024-10-09 12:50:51 字数 404 浏览 1 评论 0原文

我一直在寻找有关如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-10-16 12:50:51

据我所知,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.

眼泪也成诗 2024-10-16 12:50:51

我个人会将此问题附加到 SSI 包中,这将为您在加载和后续日志记录方面提供更大的灵活性。但是,如果您打算通过 T-SQL 执行此操作,请考虑执行动态构造的 SQL:

declare @cmd nvarchar(max), @filename nvarchar(255)
set @filename = 'C:\uploadfile.txt'

set @cmd = 
'BULK INSERT Price_Template_Host
FROM '''+@filename+'''
WITH
(
    FIELDTERMINATOR = ''\t'',
    ROWTERMINATOR = ''\n''
)'

-- Debug only
print @cmd
-- Insert to table
exec(@cmd)

-- Insert into transaction log table filename and datetime()
insert into dbo.LoadLog (filename, TheTime)
values (@filename, getdate())

如果我正确理解您的问题,这将参数化文件名,以便您可以在脚本中进一步捕获它。

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:

declare @cmd nvarchar(max), @filename nvarchar(255)
set @filename = 'C:\uploadfile.txt'

set @cmd = 
'BULK INSERT Price_Template_Host
FROM '''+@filename+'''
WITH
(
    FIELDTERMINATOR = ''\t'',
    ROWTERMINATOR = ''\n''
)'

-- Debug only
print @cmd
-- Insert to table
exec(@cmd)

-- Insert into transaction log table filename and datetime()
insert into dbo.LoadLog (filename, TheTime)
values (@filename, getdate())

If I understand your question correctly, this paramaterizes the filename so that you can capture it further down in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文