0

I have below query in which i am inserting into the temporary table and when i try to run the query sql is throwing binary or string data would be truncated. But actually my data is in the range of defined nvarchar(400) for column PhoneModel.

CREATE TABLE #orders
(
    quoteHeaderId INT,
    PhoneModel nvarchar(400)
)

INSERT INTO #orders(quoteHeaderId, PhoneModel)
SELECT qh.QuoteHeaderId, 
   (Select ph.ModelName + ','  
    FROM t_handsetQuote h2
       INNER JOIN t_PhoneAudit t1 on t1.PhoneAuditId = h2.QuotePhoneAuditId
       INNER JOIN t_phoneModel ph on t1.phoneModelid = ph.phoneModelid                                                  
    where qh.QuoteHeaderId = h2.QuoteHeaderId
    FOR XML PATH('')
   ) AS PhoneModel
FROM t_QuoteHeader qh
INNER JOIN t_handsetQuote h on qh.QuoteHeaderId = h.QuoteHeaderId
INNER JOIN t_CustomerAdditionalInformation ch on qh.CustomerId = ch.CustomerId 
and keyName ='SendUpdatesByEmail' AND ch.value = 'yes'
japzdivino
  • 1,668
  • 2
  • 13
  • 24
HarshSharma
  • 590
  • 3
  • 6
  • 30
  • 1
    Possible duplicate of [SQL Server String or binary data would be truncated](http://stackoverflow.com/questions/6388756/sql-server-string-or-binary-data-would-be-truncated) – Evaldas Buinauskas Oct 07 '15 at 09:17

3 Answers3

1

I think the nvarchar(400) cannot handle the length of string you inserted to it.

How about changing your PhoneModel nvarchar(400) into PhoneModel varchar(MAX)

japzdivino
  • 1,668
  • 2
  • 13
  • 24
0

run your select query without insert statement, and check the len of ph.ModelName + ','. it could be differ record by record.

Run your query and provide feedback.

0

Easiest thing to see what's causing the problem is to use select ... into:

SELECT qh.QuoteHeaderId, 
   (Select ph.ModelName + ','  
    FROM t_handsetQuote h2
       INNER JOIN t_PhoneAudit t1 on t1.PhoneAuditId = h2.QuotePhoneAuditId
       INNER JOIN t_phoneModel ph on t1.phoneModelid = ph.phoneModelid                                                  
    where qh.QuoteHeaderId = h2.QuoteHeaderId
    FOR XML PATH('')
   ) AS PhoneModel
into #orders
FROM t_QuoteHeader qh
INNER JOIN t_handsetQuote h on qh.QuoteHeaderId = h.QuoteHeaderId
INNER JOIN t_CustomerAdditionalInformation ch on qh.CustomerId = ch.CustomerId 
and keyName ='SendUpdatesByEmail' AND ch.value = 'yes'

and then check the created table by ordering len(PhoneModel) desc

James Z
  • 11,838
  • 10
  • 25
  • 41