如何使用bcp命令将文本文件读入@table变量

时间:2018-10-20 15:05:20

标签: sql sql-server tsql bcp

我在下面的文本文件中有不同的单词:

Text file having words

我的目标是使用bcp命令将文本文件中的4个字符单词插入到@temp表变量中。

因此,最后,表变量@temp如下所示:

@temp

1 个答案:

答案 0 :(得分:0)

  1. 创建一个表,您将在其中存储来自文件的数据:

    create table import(WORDS nvarchar(100))
    
  2. 使用bcp将文件中的数据导入第一步中创建的表中:

    bcp [test].[dbo].[import] in d:\test.txt -c -T
    
  3. 声明@table变量:

    declare @table table ([ID] int identity(1,1), WORDS nvarchar(100))
    
  4. 仅将长度为4的单词插入@table变量中:

    insert into @table 
    select WORDS 
    from import
    where len(WORDS) <= 4
    

现在@table变量包含以下数据:

enter image description here

相关问题