SQL returns the value of a column and displays a row.

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.