简体   繁体   English

如何在 SQL Server 的列表中插入文本文件的名称

[英]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

Here is a demo 这是一个演示

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

相关问题 如何将SQL Server表数据作为插入命令导出到文本文件? - How to export SQL Server table data into text file as Insert command? 如何将文本文件的内容插入SQL Server中的表中 - How to insert the contents of a text file into a table in SQL Server 如何从不同的表中获取更新的列名,并将其数据与列名一起插入SQL Server的单个历史表中? - How can I get updated column names from different tables and insert their data with column name in a single history table in SQL Server? 如何在列名称为SQL Server的SQL Server中返回数据透视表? - How can i return Pivot table in sql server with column name? 如何通过从 SQL Server 中的另一个表中选择列名和值将行插入表中 - How to insert row into a table by select column name and value from another table in SQL Server 将ID,列名和值插入表SQL Server - Insert id, name of column and value into a Table SQL Server 如何在列名是变量的SQL表中插入列? - How do I insert columns in my SQL table, where column name is a variable? 如何从SQL Server中的当前表中获取表名作为列 - How can I get table name as column from the current table in SQL Server 如何在SQL中将文件插入表中? - How can I insert a file into a table in SQL? 如何使用冒号添加列:在使用 SQL 服务器的表中的列名中 - How to add column with Colon : in column name in table using SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM