• subscribe
August 28, 2008 12:00 AM

Q: How do I retrieve values from a table and store the results in a string?

SQL Server Pro
InstantDoc ID #99384

Q: How do I retrieve values from a table and store the results in a string?

A: The following code provides a simple yet effective method for retrieving values from a table and storing the results in a string, without having to rely on cursors, WHILE loops, or other complex structures:

declare @string varchar(8000)
  select @string = ‘’
  
  select @string = @string + MyColumn
    from MyTable
  
  print @string

You need to initialize your string to a non-NULL value, or the string will end up as NULL, which is the default behavior of NULLs and string concatenation.

—Bill McEvoy



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...