[英]How I insert the name of a text file in a Column Table in SQL Server
I'm reading data from a text file and I want to write a query to insert the name of the text file in a column called FileName
.我正在从文本文件中读取数据,并且我想编写一个查询以在名为FileName
的列中插入文本文件的名称。
Example:例子:
CREATE TABLE ##textFileData(rowData Nvarchar(max))
DECLARE @fiLeName varchar(max)
DECLARE @fiLePath varchar(max) = '\\PC\Folder\TextFile.TXT'
SET @fiLeName = 'BULK INSERT ##textFileData FROM ''' + @fiLePath + '''
with (
codepage = 65001
)';
Exec (@fiLeName)
Expected Output:预期输出:
TableName
----------
ID FileName
1 File_Name.TXT
2 File_Name.TXT
3 File_Name.TXT
Is there a simple/fun way to do this?有没有简单/有趣的方法来做到这一点?
Thank you谢谢
Would this be something you are looking for:这会是你正在寻找的东西吗:
If this is your full path:如果这是您的完整路径:
DECLARE @fiLePath VARCHAR(1000)
SET @fiLePath = '\\Location\DISK\YOURFOLDER\File_Name.txt'
And this is your table:这是你的桌子:
create table TableName (id int IDENTITY(1,1) PRIMARY KEY
, FileName varchar(500))
Then you can insert it like this:然后你可以像这样插入它:
insert into TableName
select RIGHT(@fiLePath, CHARINDEX('\', REVERSE(@fiLePath)) -1) file_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.