How to read text file into @table variable using bcp command

瘦欲@ 提交于 2019-12-25 02:26:47

问题


I have below text file having different words inside it:

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

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))
    
  2. Import data from file with bcp into the table created in the first step:

    bcp [test].[dbo].[import] in d:\test.txt -c -T
    
  3. Declare @table variable:

    declare @table table ([ID] int identity(1,1), WORDS nvarchar(100))
    
  4. Insert into @table variable only words with length = 4:

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

Now @table variable contains this data:



来源:https://stackoverflow.com/questions/52907024/how-to-read-text-file-into-table-variable-using-bcp-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!