如何使用bcp命令将文本文件读取到@table变量中 [英] How to read text file into @table variable using bcp command

查看:86
本文介绍了如何使用bcp命令将文本文件读取到@table变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have below text file having different words inside it:

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

My aim is to insert only 4 character words from the text file into a table variable which is @temp, using bcp command.

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

So, at the end, the table variable @temp will look like below:

推荐答案

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

  1. Create a table where you will store the data coming from your file:

create table import(WORDS nvarchar(100))

  • 使用bcp将文件中的数据导入第一步中创建的表中:

  • Import data from file with bcp into the table created in the first step:

    bcp [test].[dbo].[import] in d:\test.txt -c -T
    

  • 声明@table变量:

    declare @table table ([ID] int identity(1,1), WORDS nvarchar(100))
    

  • 仅将长度为4的单词插入@table变量:

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

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

    Now @table variable contains this data:

    这篇关于如何使用bcp命令将文本文件读取到@table变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆