2012-06-27 18 views
0

我需要使用SQL Server存儲過程從文本文件中提取十六進制值。如何從使用SQL Server的文本文件中提取十六進制值?

我編寫了手動給出輸入(十六進制值 - )的程序,但我需要傳入一個文件名作爲輸入。從那個文件我必須提取內容

我的文件內容看起來是這樣的:

V(068F 015A 0000 01A7 69 019A 6B 00F1 6A) 
V(0665 0158 0000 01A8 68 0186 6B 00EE 6A) 
V(0687 017A 0000 01C3 67 018A 69 00F9 69) 
V(067F 0171 0000 01AF 66 01A4 68 00F6 67) 
V(06C2 0162 0000 01AA 64 0191 66 0150 65) 
V(07E7 0163 0000 01B3 62 0195 64 0213 64) 
V(0876 0166 0000 01CA 60 0214 62 01EF 62) 
V(0BA1 015F 0000 021B 5E 039C 60 024B 60) 
V(0DC9 014D 0007 01A2 5B 0426 5C 0407 5D) 
V(0E30 0140 000A 013E 5B 04A2 5B 043A 5C) 
V(0E6B 0130 000B 013C 5A 04B4 5A 046D 5B) 
V(0DDC 0150 0011 015A 58 052A 59 0399 5A) 
V(0C1C 0164 0001 013E 55 0456 56 03A4 56) 
V(0CF0 02EA 0000 01B0 55 0534 56 0338 57) 
V(0B86 03A1 0000 01D3 57 0461 58 02D6 59) 
V(0950 0236 0000 013F 59 03A1 5A 0226 5A) 
V(0847 0279 0000 00CC 59 03A7 59 01E1 5B) 
V(0734 0203 0000 0078 5B 037D 5B 0156 5D) 
V(075D 038B 0000 00DD 5E 0306 5E 01E3 60) 
V(073E 02C6 0000 0117 61 028A 62 0191 63) 
V(0606 0183 0000 0095 62 01C8 63 01A7 64) 
V(0793 0310 0000 00D4 5F 02CF 5F 020A 61) 
V(07C2 03C0 0000 011D 5D 0301 5D 0211 5E) 
V(080A 043B 0000 0170 5E 031C 5E 01E6 60) 
V(06FD 041C 0000 0129 60 02B1 60 01D5 61) 
V(05D2 03A3 0000 0139 62 014E 62 0238 63) 
V(06CC 046E 0000 0153 62 0205 62 0240 63) 

請幫助我..

我想喜歡本作的單十六進制數值

set @sting='068F 015A 0000 01A7 69 019A 6B 00F1 6A' 
    set @string=replace(@string,' ',',') 
    SET @Delimiter = ',' 
    SET @string = @string + @Delimiter 
    SET @Pos = charindex(@Delimiter,@string) 
    while(@Pos <> 0) 
    begin 
    select @mid ='0x'+substring(@string,1,@Pos - 1) 
    set @query='select convert(int,'[email protected]+')' 
    insert @word execute (@query) 
    select @value=value from @word 
    SET @string = substring(@string,@pos+1,len(@string)) 
    SET @pos = charindex(@Delimiter,@string) 
    end 

謝謝

+0

**向我們展示**您到目前爲止嘗試過的東西! –

+0

你期待輸出是什麼?一列「二進制(15)」的N行? –

+0

我需要知道如何從文件中直接獲取這些值作爲068F 015A 0000 01A7 69 019A 6B 00F1 6A –

回答

0

我找到了從文本文件中讀取內容的方法。

alter PROCEDURE ReadFromTextFile 

@FileName VARCHAR (1024) 

AS 
DECLARE @OLEResult INT 
DECLARE @FS INT 
DECLARE @FileID INT 
DECLARE @Message VARCHAR (8000) 
drop table hex_temp 
create table hex_temp (id int identity(1,1),value varchar(max)) 
-- Create an instance of the file system object 
EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT 
IF @OLEResult <> 0 
BEGIN 
PRINT 'Scripting.FileSystemObject' 
PRINT 'Error code: ' + CONVERT (VARCHAR, @OLEResult) 
END 

-- Open the text file for reading 
EXEC @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 1, 1 
IF @OLEResult <> 0 
BEGIN 
PRINT 'OpenTextFile' 
PRINT 'Error code: ' + CONVERT (VARCHAR, @OLEResult) 
END 

-- Read the first line into the @Message variable 
EXECUTE @OLEResult = sp_OAMethod @FileID, 'ReadLine', @Message OUT 

-- Keep looping through until the @OLEResult variable is < 0; this indicates that the end of the file has been reached. 
WHILE @OLEResult >= 0 
BEGIN 
insert into hex_temp(value) values (@Message) 
EXECUTE @OLEResult = sp_OAMethod @FileID, 'ReadLine', @Message OUT 
END 

EXECUTE @OLEResult = sp_OADestroy @FileID 
EXECUTE @OLEResult = sp_OADestroy @FS 
相關問題