0

Below query throwing error in some cases

Declare @Col Varchar(100)
Select @ClientDocFormat=NoticeFormat From ClientDocumentFormat Where ClientID=@ClientID And FormatTypeID=@FormatType
Set @mCursor=Cursor For Select Fields,DATA_TYPE From V_EmployeeMasterFields
Open @mCursor
Fetch Next From @mCursor Into @ColumnName,@DATA_TYPE
WHILE @@FETCH_STATUS = 0
Begin
     Set @Col=REPLACE(@ColumnName,'@@','')
     If @DATA_TYPE='date'
        Set @Col='CONVERT(Varchar,'+@Col+',106)'
     Set @ClientDocFormat=REPLACE(@ClientDocFormat,@ColumnName,('!!+IsNull('+@Col+','''')+!!'))

Fetch Next From @mCursor Into @ColumnName,@DATA_TYPE
End
CLOSE @mCursor
DEALLOCATE @mCursor
Set @ClientDocFormat='Select '''+REPLACE( Replace(@ClientDocFormat,'''',''''''),'!!','''')+''' As DocFormat From V_EmployeeDetails Where EmpID='''+CONVERT(Varchar(20), @EmpID)+''''
EXEC(@ClientDocFormat)

Below is Error:

DocFormat
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.

2
  • print @ClientDocFormat before EXEC . this will help you identify the issue Commented May 29, 2015 at 10:08
  • 2
    You missed to provide all variable declarations. The "CONVERT(Varchar,'+@Col+',106)" uses only the default number of characters. When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified when using the CAST and CONVERT functions, the default length is 30. msdn.microsoft.com/en-us/library/ms176089.aspx Commented May 29, 2015 at 10:16

1 Answer 1

1

Here, The field is NOT big enough to hold my data.

Check the table structure of tables. I think you'll find that the length of one or more fields is NOT big enough to hold the data you are trying to insert / select / holding variable . For example, if the Phone field is a varchar(8) field, and you try to put 11 characters in to it, you will get this error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.