473,654 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function to create comma separated list from any given column/table.

Hi,

I'm sure this is a common problem.. to create a single field from a
whole column, where each row would be separated by a comma.

I can do this for a specified table, and column.. and I've created a
function using VBA to achieve a more dynamic (and very slow) solution..
so I would like to implement it using a user defined function in sql server.

The problems I'm facing are, that I can't use dynamic sql in a
function.. and I also can't use temporary tables which could build up a
'standard' table from parameters given to then perform the function on.

So, with these limitations, what other options do I have?

Cheers,
Chris
Jul 20 '05 #1
1 9891
Dynamic SQL is so so passe'

Firstly it is a security risk , as you can't lock down rights to procedures
that use it.

secondly it is inefficient as it has to be re-generated and compiled each
time you use it

Code Generation is the all the rage now.

And how often will you be adding new code tables to do this with?

Here's how to roll your own code generator in SQL:

in Query Analyzer (aka ISQL)

write a template code block to get it to work (I've done this below)

wrap that in print statements.

create a cursor to loop thru sysobjects and/or syscolumns (or the SCHEMA
Views) to locate all your tables with the columns you need.

loop thru and generate all the script you need.

you can create a separate function/procedure for each or create a single one
with a large case in it (your call).

run the output to create the function(s).

save the script that generates the stuff and hold on to it.

every time a change is made to a code table (i.e. add new table, change
column names, etc..) rerun the script to regenerated (make sure there is a
drop in there somewhere as well)

Tally Ho! - (which is a liquor store her in Delaware, and not a Fox hunting
term, which I understand is now outlawed in the UK).

-----------------------------------------------------------
-- declare an empty string (not null)
declare @list varchar 4000
set @list = ''

-- select multiple rows into a single variable
select @list = @list + ',' + ColumnName from TableName

-- get rid of the last comma
select @list = substring( @list , 1, len( @list ) -1 )
-----------------------------------------------------------

"Not Me" <No***********@ here.com> wrote in message
news:cl******** **@ucsnew1.ncl. ac.uk...
Hi,

I'm sure this is a common problem.. to create a single field from a whole
column, where each row would be separated by a comma.

I can do this for a specified table, and column.. and I've created a
function using VBA to achieve a more dynamic (and very slow) solution.. so
I would like to implement it using a user defined function in sql server.

The problems I'm facing are, that I can't use dynamic sql in a function..
and I also can't use temporary tables which could build up a 'standard'
table from parameters given to then perform the function on.

So, with these limitations, what other options do I have?

Cheers,
Chris

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2218
by: Craig Keightley | last post by:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both eg: List 1 => 1,2,3,4,5,6,7,8,11 List 2 => 1,3,4,5,6,7,10,23 Therefore
26
32111
by: Christina | last post by:
I have a post-form that holds a listbox with mulitple selected items. When the form is posted to the server ASP file, I want to loop through the selected items, to insert each of them into a table. How do I do that? If I execute the line: Response.Write(Request.Form("ListBox")), I get the list of selected values, separated by commas. Christina
12
29063
by: insomniux | last post by:
Hi, I'n in an environment where I cannot make stored procedures. Now I need to make a query with a subquery in the SELECT part which gives a comma separated list of results: SELECT p.id, listFunction(SELECT name FROM names WHERE name_parent=p.id) AS 'nameList' FROM projects AS p
5
10109
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first letter of each token (abc) must not be a number. I am simply trying to parse it to get an array of tokens: abc
3
6114
by: starman7 | last post by:
I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the designers in the designers table (which has fields like: id, fname, lname). How can my select return the possibly several designers for a product, with the rest of the row data, like price, name, etc.?
5
4254
by: nikoromano | last post by:
Can anyone think of a changing these XPath expressions to return comma separated lists? /Library/Branch/book/(if(author="John") then position() else "") output: 1 3 5 desired output: 1, 3, 5 /Library/Branch/book/(if(author="Mary") then position() else "") output: 2 desired output: 2
7
3029
by: Hermann | last post by:
I run a server with apache 1.3 and php 5. Yesterday I notice that sometimes the HTTP_HOST server variable has a comma separated list in it. Let's say my domain name is: www.mydomain.com Usually the HTTP_HOST value is of course "www.mydomain.com". But sometimes it is: "www.mydomain.com, www.mydomain.com". Is that supposed to be a correct behabior??
2
3084
Haitashi
by: Haitashi | last post by:
I have a comma delimited list of the following elemente: sites.app.enroll.page5.degrees.gfdg, sites.app.enroll.page5.degrees.aeetr, sites.app.enroll.page5.degrees.sfdsd, sites.app.enroll.page5.degrees.ur4gf, I'd like to use the "listDeleteAt" function to take that list and remove everything after "degrees". Any ideas? I that this but it doesn't seem to work correctly. I still get all the text after the "degree." <cfset temp =...
3
2589
by: anil2083 | last post by:
How to migrate the comma separated values from one table to another table? suppose we have table i.e XYZ and we have comma separated values in few columns i.e( column_name and values are A,B,C,D).Now i have to migrate these comma separated values into new table i.e PQR. Example: Table1 column_name1 complexion values : VF,F,AVG etc.. column_name2 profession values : TEA,LECT etc...
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.