当前位置: 动力学知识库 > 问答 > 编程问答 >

sql server - Write the content of sql row into varchar variable

问题描述:

Does anybody know how can I fill in the varchar variable with the content of any row in T-SQL table ?

For example, I have next table:

id name Age city

1 Alice 10 London

When we read the current value of column we need to check if it is NOT NULL we write it to the @description variable.

I need to get the result in a next maner:

@description = "id: 1; name: Alice; Age: 10; city: London";

I will be very glad if someone could tell me how to solve this problem.

Thanks in advance.

网友答案:

Just concatenate the columns (you'll have to convert the numeric columns first):

select 
  'id: ' + convert(varchar(10), id) + '; name: ' + name + 
  '; Age: ' + convert(varchar(10), age) + '; city: ' + city 
from test 

SQL fiddle

分享给朋友:
您可能感兴趣的文章:
随机阅读: