MSSQL

[SQLSTATE 42000] (오류 7347) 예기치 않은 데이터 길이를 반환했습니다. 오류발생시 체크사항

머니루키 2010. 7. 19. 08:49
정상적으로 보이는데 가끔 데이터 작업 도중에 이러한 오류를 내뱉는 경우가 발생한다.

예기치 않은 데이터 길이를 반환했습니다.
예상 데이터 길이는 6인데 반환된 데이터 길이는 1입니다. [SQLSTATE 42000] (오류 7347)

일 단 원인은 데이터의 형식에 관련된 문제인거 같다.
아예 데이터 작업시 이러한 오류가 발생하지 않으려면 해당 키의 오류를 발생할 수 있는
쿼리단의 문제를 사전에 배제해야 하겠다.

그게 어렵다면 char(6) 이런식으로 구성된 데이터를 varchar(6) 이런식으로 동적형변환이
가능하도록 바꾸는 방법도 있다.

--------------------------------------------------------------------------------

Unexpected data length for the fixed-length column returned

--------------------------------------------------------------------------------
 I am getting the following error message. The data type of the field is char.

Msg 7347, Sev 16: OLE DB provider 'SQLOLEDB' returned an unexpected data length for the fixed-length column '[CCNIDCDATALOADS].[oaodata].[dbo].[HCNTRP].PROVIDER_NO'. The expected data length is 6, while the returned data length is 1. [SQLSTATE 42000]

Msg 7300, Sev 16: OLE DB error trace [Non-interface error:  Unexpected data length returned for the column:  ProviderName='SQLOLEDB', TableName='[CCNIDCDATALOADS].[oaodata].[dbo].[HCNTRP]', ColumnName='PROVIDER_NO', ExpectedLength='6', ReturnedLength='1']. [SQLSTATE 01000]




I found one solution to change from char to varchar, but PROVIDER_NO is part of a PRIMARY KEY.

The only way to change the data type of a field that is part of the Primary Key of a table is to delete and recreate the field.

Since you can not delete part of a primary key, the only way to change the primary key is to delete and rebuild the table and we can't do that

is there another resolution?

thank you

--------------------------------------------------------------------------------You should be able to convert the column so that it matches the datatype SQL is expecting.

SELECT   
CONVERT(CHAR(6),PROVIDER_NO) AS PROVIDER_NO  
FROM [CCNIDCDATALOADS].[oaodata].[dbo].[HCNTRP] 

If you want to change the data type, you will need to drop all fk constraints, drop the pk, change data type, recreate primary key and finally recreate the foreign keys.