Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

Import text file using xp_cmdshell in SQL Server

$
0
0


There are several technique to import file in SQL Server from external source like SSIS, BULK INSERT command, Import/Export wizard, OPENROWSET etc. one of them xp_cmdshell is use to import text file to SQL Server.

Below script can be use to import text file to SQL Server.
Note: Before using below script we have to enable xp_cmdshell once.


CREATETABLE#TEMP
(
    Result  VARCHAR(MAX)
)

DECLARE@pathVARCHAR(8000)
DECLARE@QueryINT

-- read from text file
SET@path='TYPE D:\Test.txt'

INSERTINTO#TEMP
EXEC@Query=master.dbo.xp_cmdshell@path


SELECT  *
FROM    #TEMP
GO

DROPTABLE#TEMP



Viewing all articles
Browse latest Browse all 265

Trending Articles