I am facing a strange SQL exception:-
The code flow is like this:
.Net 4.0 --> Entity Framework --> SQL 2008 ( StoredProc --> Function {Exception})
In the SQL Table-Valued Function, I am selecting a column (nvarchar(50)) from an existing table and (after some filtration using inner joins and where clauses) inserting the values in a Table Type Object having a column (nvarchar(50))
This flow was working fine in SQL 2008 but now all of sudden the Insert into @TableType is throwing "string or binary data would be truncated" exception.
Insert Into @ObjTableTypeSelect * From dbo.Table
The max length of data in the source column is 24 but even then the insert statement into nvarchar temp column is failing.
Moreover, the same issue started coming up few weeks back and I was unable to find the root cause, but back then it started working properly after few hours(issue reported at 10 AM EST and was automatically resolved post 8 PM EST). No refresh activity was performed on the database.
This time however the issue is still coming up (even after 2 days) but is not coming up in every scenario. The data set, for which the error is thrown, is valid and every value in the function is fetched from existing tables.
Due to its sporadic nature, I am unable to recreate it now :( , but still unable to determine why it started coming up or how can i prevent such things to happen again.
It is difficult to even explain the weirdness of this bug but any help or guidance in finding the root cause will be very helpful.
I also Tried by using nvarchar(max) in the table type object but it didn't work.
Here is a code similar to the function which I am using:
BEGINTRAN
DECLARE @PIDint= 483
DECLARE @retExcludablesTABLE
(
PIDintNOT NULL,
ENumbernvarchar(50)NOTNULL,
CNumbernvarchar(50)NOTNULL,
AIduniqueidentifierNOTNULL
)
declare @PSCount int;
select @PSCount = count('x')
from tblProjSur ps
where ps.PID = @PID;
if (@PSCount = 0)
begin
return;
end;
declare @ExcludableTempValue table (
PIDint,
ENumbernvarchar(max),
CNumbernvarchar(max),
AIduniqueidentifier,
SIdsint,
SCSymbnvarchar(10),
SurCSymbnvarchar(10)
);
with SurCSymbs as (
select ps.PID,
ps.SIds,
csl.CSymb
from tblProjSur ps
rightouterjoin tblProjSurCSymb pscs
on pscs.tblProjSurId= ps.tblProjSurId
innerjoin CSymbLookup csl
on csl.CSymbId = pscs.CSymbId
where ps.PID = @PID
),
AssignedValuesas (
select psr.PID,
psr.ENumber,
psr.CNumber,
psmd.MetaDataValueas ClaimSymbol,
psau.UserIdas AId,
psus.SIds
from PSRow psr
innerjoin PSMetadata psmd
on psmd.PSRowId = psr.SampleRowId
innerjoin MetaDataLookup mdl
on mdl.MetaDataId= psmd.MetaDataId
innerjoin PSAUser psau
on psau.PSRowId = psr.SampleRowId
innerjoin PSUserSur psus
on psus.SampleAssignedUserId= psau.ProjectSampleUserId
where psr.PID = @PID
and mdl.MetaDataCommonName='CorrectValue'
and psus.SIds in(selectdistinct SIds from SurCSymbs)
),
FullDetailsas (
select asurv.PID,
Convert(NVarchar(50),asurv.ENumber)as ENumber,
Convert(NVarchar(50),asurv.CNumber)as CNumber,
asurv.AId,
asurv.SIds,
asurv.CSymbas SCSymb,
scs.CSymbas SurCSymb
from AssignedValues asurv
leftouter join SurCSymbs scs
on scs.PID= asurv.PID
and scs.SIds = asurv.SIds
and scs.CSymb = asurv.CSymb
)
--Error is thrown at this statement
insertinto @ExcludableTempValue
select* from FullDetails;
with SurHavingSym as (
selectdistinct est.PID,
est.ENumber,
est.CNumber,
est.AId
from @ExcludableTempValue est
where est.SurCSymbisnot null
)
delete @ExcludableTempValue
from @ExcludableTempValue est
innerjoin SurHavingSym shs
on shs.PID= est.PID
and shs.ENumber = est.ENumber
and shs.CNumber = est.CNumber
and shs.AId = est.AId;
insert @retExcludables(PID, ENumber, CNumber, AId)
selectdistinct est.PID,
Convert(nvarchar(50),est.ENumber) ENumber,
Convert(nvarchar(50),est.CNumber) CNumber,
est.AId
from @ExcludableTempValue est
RETURN
ROLLBACKTRAN
I have tried by converting the columns and also validated the input data set for any white spaces or special characters.
For the same input data, it was working fine till yesterday but suddenly it started throwing the exception.