Reputation: 55
Accidental DBA, here. I'm looking at a query that was written by a past DBA who is no longer with the company and I noticed something that I consider to be odd; maybe I'm just ignorant. The SQL is used to gather information about the DBs and provide hyperlinks and file locations. It uses one variable. Below is the bit of code in question and it is the very beginning of the query.
declare @crlf varchar(2)
set @crlf = char(13)+char(10)
So, to me, it looks like the variable is declared as varchar, but is then set as a concatenation of two char data types? This is confusing to me because:
A) why would he not just set the variable as the data type he wants to begin with? B) I didn't realize you could SET a variable to a data type and not a value C) I also didn't know you could concatenate (or add in this case, maybe?) data types together. This just makes no sense to me at all.
Any insight on this?
Upvotes: 1
Views: 48
Reputation: 25112
He is simply setting this to a carriage return (CR) and new line feed (LF) hence the variable name crlf
. You could use the same method to set it to two letters...
declare @crlf varchar(2)
set @crlf = char(88)+char(89)
select @crlf
RESULTS
XY
As pointed out in the comments, this is calling the CHAR()
function versus assigning a data type which would be a string literal. The opposite would be the ASCII()
function.
Upvotes: 3