SQL returns the value of a column and displays a row.
Create Table Tb (COL varchar (20) insert TB values ('A') insert TB values ('B') insert TB values ('C ') insert TB values ('D') insert TB values ('E') Go select * from TB -- Method 1 declare @ SQL varchar (1000) set @ SQL = ''select @ SQL = @ SQL + T. col + ',' from (select Col from TB) as t set @ SQL = 'select result = ''' + Left (@ SQL, Len (@ SQL)-1) + ''' exec (@ SQL)/* result ---------- A, B, C, D, E ,*/
-- Method 2 declare @ output varchar (8000) Select @ output = coalesce (@ output + ',', '') + Col from TB -- Note: the "+" Operation of the null and character in SQL Server. the result is still null print @ output/* a, B, c, d, e */
Drop table TB
Coalesce (TRANSACT-SQL) returns the first non-null expression in its parameters. Returns the Data Type of the expression with the highest data type priority. If all expressions cannot be null, The result type cannot be null. If all parameters are null, coalesce returns NULL. Coalesce (expression1,... n) functions the same as the following case expressions:
Case
When (expression1 is not null) Then expression1
When (expression2 is not null) Then expression2
...
Else expressionn
End
Although isnull is equivalent to coalesce, their behavior is different. Expressions that contain isnull with non-null parameters are considered not null, while expressions that contain coalesce with non-null parameters are considered null. To create an index for a coalesce expression that contains non-null parameters in SQL Server, you can use the persisted column attribute to make the calculation column persistent, as shown in the following statement:
Copy
Create Table # checksumtest (ID int identity, num int default (RAND () * 100), rowchecksum as coalesce (checksum (ID, num), 0) persisted primary key );