In
Article #2248, we discussed a method for converting a comma-separated list (e.g. from a set of checkboxes, or a multi-select list) into a column of values. What about the other way around? You could do it from ASP easily enough, as follows:
<% ' assuming valid connection object 'conn' Set rs = conn.execute("SELECT value FROM table") if not rs.eof then Do while not rs.eof list = list & rs(0) & "," rs.movenext loop Response.Write left(list,len(list)-1) end if %>
|
However, what if you wanted to do this totally within the confines of the database (e.g. called from another stored procedure)? You could use the following method, however before proceeding you should read this
note from UJ, as well as
KB #287515:
SET NOCOUNT ON SET CONCAT_NULL_YIELDS_NULL ON DECLARE @list VARCHAR(8000) DECLARE @delm VARCHAR(2) SET @delm = ',' CREATE TABLE #TableToList (value INT) INSERT #TableToList VALUES (14) INSERT #TableToList VALUES (8) INSERT #TableToList VALUES (12) INSERT #TableToList VALUES (7) SELECT @list = ISNULL( @list + @delm + CAST(value AS VARCHAR(8000)), CAST(value AS VARCHAR(8000)) ) FROM #TableToList ORDER BY value DROP TABLE #TableToList SELECT list = @list |