I have the following statement which works without error: DECLARE @FormattedLicense NVARCHAR(255) SELECT @FormattedLicense = CAST ( CustomerID + '|' + AppName + '|' + MachineName + '|' + CAST (ExpirationDate AS VarChar(10)) + '|' + LicenseType AS NVARCHAR(255)) FROM RegisteredMachines INNER JOIN Licenses ON Licenses.LicenseID = RegisteredMachines.LicenseID WHERE RegistrationID = 1 SELECT @FormattedLicense I would like to use this as a user-defined function, so I created one like this: ALTER FUNCTION "DBA"."fFormatLicense" (@RegistrationID Unsigned BigInt) RETURNS NVARCHAR(255) AS BEGIN DECLARE @FormattedLicense NVARCHAR(255) SELECT @FormattedLicense = CAST ( CustomerID + '|' + AppName + '|' + MachineName + '|' + CAST (ExpirationDate AS VarChar(10)) + '|' + LicenseType AS NVARCHAR(255)) FROM RegisteredMachines INNER JOIN Licenses ON Licenses.LicenseID = RegisteredMachines.LicenseID WHERE RegistrationID = @RegistrationID RETURN @FormattedLicense END Unfortunately, I get errors when I try to call this function. DECLARE @Requested Unsigned BigInt SET @Requested = 1 SELECT fFormatLicense (@Requested)
I don't understand why my UDF doesn't work. Can anyone show me what to fix and how to fix it? asked 19 Feb '13, 18:48 Jason 'Bug' ... Mark Culp |
Right truncation of string data is an indicator that the server thinks you push a long string in a too small variable. I thought explict CAST prevents this. Try to set the connection level option string_rtruncation option [compatibility] to off to verify if this is the cause of the problem. answered 20 Feb '13, 04:29 Thomas Dueme...
No, AFAIK you will have to use LEFT() or something alike to prevent that error if string_rtruncation is on.
(20 Feb '13, 04:48)
Volker Barth
Every Day you can learn something in this Forum ;-)
(20 Feb '13, 04:55)
Thomas Dueme...
Replies hidden
Sure - I think it's really helpful to share one's own
(20 Feb '13, 05:50)
Volker Barth
I tried the LEFT() trick, but that didn't work. Changing the string_rtruncation option fixed it though. Thanks!
(20 Feb '13, 10:56)
Jason 'Bug' ...
Replies hidden
Where did you apply the Left() in your script?
(20 Feb '13, 11:20)
Thomas Dueme...
|
You Should you ether SELECT CAST ( CustomerID + '|' + AppName + '|' + MachineName + '|' + CAST (ExpirationDate AS VarChar(10)) + '|' + LicenseType AS NVARCHAR(255)) INTO @FormattedLicense FROM RegisteredMachines INNER JOIN Licenses ON Licenses.LicenseID = RegisteredMachines.LicenseID WHERE RegistrationID = @RegistrationID; or set @FormattedLicense = ( select ... ) HTH answered 20 Feb '13, 03:26 Thomas Dueme... Oh, the joys of "Transact-SQL compatibility"... though "SELECT @localVar = ..." seems to be valid in SA, too, otherwise Jason's original statement batch should not work, either, methinks.
(20 Feb '13, 03:41)
Volker Barth
Sadly, I tried both suggestions and neither work.
(20 Feb '13, 10:53)
Jason 'Bug' ...
Replies hidden
Have you tried to turn off the string_rtruncation option? (BTW, what SA version and build no. are you using?)
(20 Feb '13, 10:57)
Volker Barth
|