DBAs - Do you avoid collation errors?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> You don't want this error: <asideEmbed variant="greybox" body={<> "120_ClientInvoice_ClientIDRequired.sql...Column 'dbo.Client.ClientID' is not of same collation as referencing column 'ClientInvoice.ClientID' in foreig..." </>} figureEmbed={{ preset: "default", figure: 'XXX', shouldDisplay: false }} /> When you write a stored proc - it must work regardless of the users collation. When you are joining to a temp table - meaning you are joining 2 different databases (eg. Northwind and TempDB) they won't always have the same collation. The reality is that you can't tell a user what collation to run their TempDB - we can only specify the collation Northwind should be (we don't even want to specify that - we want that to be their default (as per their server)). </>} />

Here is what you need to do:

SELECT
#ClientSummary.ClientID,
DateOfLastReminder = MAX(ClientDiary.DateCreated),
DaysSinceLastReminder = DATEDIFF(day,MAX(ClientDiary.DateCreated),getdate())
INTO #RecentReminderList
FROM
ClientDiary INNER JOIN #ClientSummary
ON ClientDiary.ClientID = #ClientSummary.ClientID COLLATE
database_default
WHERE
ClientDiary.CategoryID LIKE 'DEBT-%'
GROUP BY
#ClientSummary.ClientID

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star