列值在sql存储过程中的一个字符串中

时间:2011-08-15 02:58:27

标签: sql sql-server tsql sql-server-2008 concatenation

我有以下列的地址表:

  • addressId
  • buildingName
  • streetNo
  • streetName
  • streetType
  • subAddressNo
  • subAddressType
  • 郊区
  • 邮编
  • 状态

我想要的是SELECT * from address where addressId = @addressId,并且返回将是连接成一个字符串的所有列。类似的东西:

set @addressString = (SELECT * from address where addressId = @addressId)

2 个答案:

答案 0 :(得分:4)

select coalesce(cast(addressId as varchar)+ ',', '')  + coalesce(buildingName+ ',', '') 
coalesce(cast(streetNo as varchar)+ ',', '') -- + and so on 
from address

请记住将数值类型转换为varchar

答案 1 :(得分:0)

试试这个:

SELECT buldingName + ' ' + streetNo + ' ' + streetName + ' ' + theRestOfYourColumns AS ClientAddress
FROM YourAddressTable
WHERE addressId = @addressIdPreviouslyAssignedVariable