2011-02-14 4 views
4

T-SQL 블록을 사용하여 데이터베이스 열에 저장된 ASCII 문자 덤프를 가져옵니다. Oracle에서 DUMP() 함수를 사용하여 쉽게 수행 할 수 있습니다. SQL Server sytax에 익숙하지 않지만 이와 비슷한 것을 사용하고 있습니다. 내가 @ 출력 2 관련 코드의 주석을 해제하는 경우 어떤 이유 T-SQL 저장된 문자의 ASCII 값을 가져 오는 함수

SET NOCOUNT ON 
-- Create the variables for the current character string position 
-- and for the character string. 
DECLARE @position int, @string char(15), @output char(1000), @output2 char(2000) 
-- Initialize the variables. 
SET @position = 1 
SET @output2 = 'Start:' 
SELECT @string = name from 
location where location_type = 4405 and owner_id = 362 
and location_id = 53183 
WHILE @position <= DATALENGTH(@string) 

     BEGIN 
     SELECT @output = CAST(ASCII(SUBSTRING(@string, @position, 1)) AS CHAR) 
     + ' ' +  CHAR(ASCII(SUBSTRING(@string, @position, 1))) 
     PRINT @output 
     --SET @output2 = @output2 + '=' + @output 
     SET @position = @position + 1 
     END 
     --PRINT @output2 
    SET NOCOUNT OFF 
    GO 

, 그것은 제대로 출력 2 @ 인쇄되지 않습니다. 아이디어는 각 문자에 대해 줄을 가져 오는 대신 모든 ASCII 값을 단일 줄로 가져 오는 것입니다. 내가 뭔가 잘못하고 있는거야?

+0

이 제대로 출력 2 @ 인쇄되지 않습니다에 의해 의미합니까 (Cyberkiwi의 대답에 건물) 갈 수있는 easiset 방법은 아마? 어떤 행동을 취하고 있습니까? – CarneyCode

+0

Output2에 쓰여지고 인쇄 할 수없는 일부 제어 문자가 있습니까? – Karl

+0

@ output2는 항상 "시작 :"을 인쇄합니다.이 항목은 내가 초기화 할 대상입니다. 컨트롤 문자에 대해 잘 모르겠습니다. 컨트롤 문자를 찾고 싶습니다. – abhi

답변

2

@output 및 @ output2의 데이터 형식을 varchar로 변경하십시오. DataLength를 Len으로 바꿉니다. Char는 고정 길이이며 문자열 끝에 문자열을 추가하면 커지지 않습니다.

+0

굉장합니다. 감사합니다 – abhi

5

단일 행을 찾고 있다면이

DECLARE @string char(15), 
@output1 varchar(1000), 
@output2 varchar(1000) 

SELECT @string = name 
from location 
where location_type = 4405 and owner_id = 362 
and location_id = 53183 

SET @output1 = '' 
SET @output2 = '' 

select 
    @output1 = @output1 + SUBSTRING(@string, number, 1) + ', ', 
    @output2 = @output2 + cast(ASCII(SUBSTRING(@string, number, 1)) as varchar) + ', ' 
from master..spt_values 
where type='p' and number between 1 and LEN(@string) 
order by number 

PRINT @output1 
PRINT @output2 
+0

감사. 지금은 오라클의 DUMP() 함수를 모방 할 수 있도록이 함수를 구현하려고합니다. – abhi