| Author |
Topic |
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 02:28:30
|
| Hi allI've got some code (that works) with the following line in the WHERE clause:-Call_Fact.ReceivedDatetime between CAST(getdate()-4 as DATE) and CAST(getdate() as DATE)If I run the procedure as-is it runs OK.What I'm trying to do is change it so that I can use variables at the top to save wading through to the WHERE clause if I need to change the time-frame.I declared @start and @end both as datetimeI then set @start=CAST(getdate()-4 as DATE) and @end=CAST(getdate() as DATE)and changed the line in the WHERE clause to the following:-Call_Fact.ReceivedDatetime between @start and @endWhen I run the updated code, I get the following error:-Msg 8152, Level 16, State 14, Line 93String or binary data would be truncated.I've checked @start and @end are giving the correct values so I'm now stumped.Anyone any ideas?If it helps, we're running SQL Server 2008 R2 (SP1) on a Windows Server 2003 machine. |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2012-07-31 : 02:49:55
|
| Nothing wrong with your WHERE cluase (even with parameters). Possibly you are trying to insert data, through this query and resulting string of a column is larger then target table column length.--------------------------http://connectsql.blogspot.com/ |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 04:23:50
|
| The query does a select and then inserts it into a temporary table.The only thing to make the query fail is putting the variables into the where clause. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-07-31 : 04:52:57
|
Is your insert query in reality a dynamic built query for which you use EXEC to run? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 05:32:38
|
No, it's a fairly straight-forward insert into/select query.I'll post a snipped version of the query below:-insert into #original_crystal ( CallId ,CallOrigin ,ReceivedDatetime (and some more fields))SELECT Call_Fact.CallId ,Call_Fact.CallOrigin ,Call_Fact.ReceivedDatetime(rest of select)FROM { oj ((((((((((NHSD_Datamart.dbo.Call_Fact Call_Fact INNER JOIN NHSD_Datamart.dbo.CallEvent_Fact CallEvent_Fact ON Call_Fact.CallId = CallEvent_Fact.CallId) LEFT OUTER JOIN NHSD_Datamart.dbo.DispositionFlag_Dim DispositionFlag_Dim ON Call_Fact.FinalDispositionId = DispositionFlag_Dim.DispositionID) LEFT OUTER JOIN NHSD_Datamart.dbo.Patient_Fact Patient_Fact ON Call_Fact.PatientId = Patient_Fact.PatientId) LEFT OUTER JOIN NHSD_Datamart.dbo.Protocol_Fact Protocol_Fact ON Call_Fact.CallId = Protocol_Fact.CallId) LEFT OUTER JOIN NHSD_Datamart.dbo.CallEventPppEnd_Fact CallEventPppEnd_Fact ON Call_Fact.CallId = CallEventPppEnd_Fact.CallId) LEFT OUTER JOIN NHSD_Datamart.dbo.v_EATHandlingTimesByCall v_EATHandlingTimesByCall ON Call_Fact.CallId = v_EATHandlingTimesByCall.CallId) LEFT OUTER JOIN NHSD_Datamart.dbo.v_TimeToStart v_TimeToStart ON Call_Fact.CallId = v_TimeToStart.CallId) LEFT OUTER JOIN NHSD_Datamart.dbo.Disposition_Dim Disposition_Dim ON Call_Fact.FinalDispositionId = Disposition_Dim.DispositionID) LEFT OUTER JOIN NHSD_Datamart.dbo.CallPriority_Dim CallPriority_Dim ON Call_Fact.CallPriorityId = CallPriority_Dim.CallPriorityId) LEFT OUTER JOIN NHSD_Datamart.dbo.CallEventType_Dim CallEventType_Dim ON CallEvent_Fact.CallEventTypeId = CallEventType_Dim.CallEventTypeId) LEFT OUTER JOIN NHSD_Datamart.dbo.CasUser_Dim CasUser_Dim ON CallEvent_Fact.UserId = CasUser_Dim.UserId} WHERE CasUser_Dim.RemodifiedAt >= '3000-01-01 00:00:00.00' AND Call_Fact.SiteId not in (401,19) AND CallEvent_Fact.CallEventTypeId IN (8,36,27,26,24,23,14,11) AND CallEventType_Dim.CallTypeId = 1 AND CallPriority_Dim.RemodifiedAt >= '3000-01-01 00:00:00.00' AND Call_Fact.RemodifiedAt >= '3000-01-01 00:00:00.00' and ((COALESCE(Patient_Fact.GivenName, 'no link')) NOT LIKE '%test%' AND (COALESCE(Patient_Fact.GivenName, 'no link')) NOT LIKE '%protocol%') AND CallEventType_Dim.RemodifiedAt >= '3000-01-01 00:00:00.00' AND (COALESCE(Disposition_Dim.RemodifiedAt, '01-01-3000')) >= '3000-01-01 00:00:00.00' AND (COALESCE(Patient_Fact.RemodifiedAt, '01-01-3000')) >= '3000-01-01 00:00:00.00' AND Call_Fact.ReceivedDatetime between CAST(getdate()-3 as DATE) and CAST(getdate() as DATE)-- AND Call_Fact.ReceivedDatetime between @start and @endThe line that is commented out is the one causing me the issues. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-31 : 06:04:25
|
| One difference is that @start and @end are datetimnes rather than dates but I don't think that is the issue.I suspect there is some data in a character column included when you have this line in that is too big. Odd as adding that line shoould only exclude rows - unless you are replacing the line before rather than just adding this one.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 06:20:12
|
| All I'm doing is trying to update the WHERE clause.I'm adding the line that is currently commented out to replace the one above it.I've also just tried using dateadd/datediff to replace the CAST and it's made no difference. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-07-31 : 06:27:39
|
The difference is that @start is getdate()-4 and the orig statement is getdate()-3.So the range to retrieve data is bigger with that variable and there can be a row which has a too big character column... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 06:40:48
|
| But if I change the original timeframe on this line:-AND Call_Fact.ReceivedDatetime between CAST(getdate()-3 as DATE) and CAST(getdate() as DATE)to this:-AND Call_Fact.ReceivedDatetime between CAST(getdate()-4 as DATE) and CAST(getdate() as DATE)and it runs fine. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-31 : 06:55:19
|
| I would check the values you are setting for @start and @end - and make sure you don't have both lines commented out.Coukd also be that using the variables changes the order of execution so some data is being tested that wasn't before and there is an issue with that.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-31 : 06:55:26
|
| Is it doing an implicict cast to a VARCHAR instead of a date and overflowing because of the name of the day? What type is Call_Fact.ReceivedDatetime really? |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 07:01:32
|
| Call_Fact.ReceivedDatetime is datetime format.How do I check for implicit casts? |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-31 : 07:40:16
|
| Check the type using the tool of your choice. SQL Management Studio for example. Anyway, I now suspect that using temporary variables is changing the query plan so it's now reading in more/different rows and applying additional filters. Some of these rows are now overflowing. Pretty much what Nigel said. Maybe just simply try selecting the rows narrowing down by some other criteria until you make it break then home in on the duff rows.Just a thought. |
 |
|
|
rmg1
Constraint Violating Yak Guru
256 Posts |
Posted - 2012-07-31 : 08:25:12
|
| OK, I've got the execution plans (trying to do things in some semblance of order), but they mean nothing to me unfortunately.Is it possible to post them on here for someone to look at? |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-31 : 09:20:21
|
| You can but I personally cannot guarantee to look at them due to work commitments and site blocks :) Someone might help though.I think what you're looking for is the difference in plans between both scenarios but basically if you remove the predicate you are concerned with you will probably get the error. Then you need to narrow it down by reducing rows until you get there (use an ORDER BY for consistency) |
 |
|
|
|