Example:
(SELECT Description = 'Changed PhoneNumber from ' + @.old_PhoneNumber + ' to ' + @.PhoneNumber
WHERE @.PhoneNumber <> @.old_PhoneNumber UNION ALL
SELECT Description = 'Changed FaxNumber from ' + @.old_FaxNumber + ' to ' + @.FaxNumber
WHERE @.FaxNumber <> @.old_FaxNumber UNION ALL
SELECT Description = 'Changed EmailAddress from ' + @.old_EmailAddress + ' to ' + @.EmailAddress
WHERE @.EmailAddress <> @.old_EmailAddress)
The problem here is that SQL Server thinks "Description" is an int (by default probably) and gives me an error when I try to assign a string to it.
I'm taking that information and using it as a field in a INSERT INTO ... SELECT statement, so I don't think I am able to use a DECLARE statement or if that would even work.
Does anyone know how I can make it so that Description is always a varchar?
Maybe?
SELECT 'Changed PhoneNumber from ' + @.old_PhoneNumber + ' to ' + @.PhoneNumber AS Description
You could also do this:
SELECT CAST('Changed PhoneNumber from ' + @.old_PhoneNumber + ' to ' + @.PhoneNumber AS varchar) AS Description
OR:
SELECT 'Changed PhoneNumber from ' + CAST(@.old_PhoneNumber AS varchar) + ' to ' + cast(@.PhoneNumber AS varchar) AS Description
|||The third option worked, but I only needed to do it with the Integers. Since there were integers in the string SQL Server tried to convert the entire string into an integer across every SELECT command in the union.
So since I had an integer many SELECTs down it was telling me "can't convert name to integer" even though there was no integer in sight of that particular SELECT statement. Pretty confusing if you ask me.
No comments:
Post a Comment