Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In the database , there is a table with three columns, First column is an autogenerated id field, second one is named as 'Resources' and third is 'Classes'. the 'Resources' can have many 'Classes'. For example suppose the 'Resource' field value of one row is 'Room'. The 'Classes' Value in the same row will be bed room, dining room, kitchen..etc.. I would like to insert bed room, dining room, kitchen, in the 'Classes' column with a comma seperation. How can i write an insert query for this?
Posted
Comments
PJ003 14-May-14 2:07am    
use stored procedure to insert comma seprated values in database.

Basically, don't - it's very easy to do, but it's a total nightmare to work with whenever you have to make any changes. SQL string handling is pretty poor and you have to really mess about in order to do anything.
Instead, create a second table which links a Class value to a Resource ID, and have multiple entries in that.
In practice, what I would do is have three tables:
Resources:
Id
...

Classes:
Id
Room

ClassesInResource
Id
ResourceId
ClassId

And fill them:
Resources:
1      John     Smith
2      Mary     Jones

Classes:
1      Bedroom
2      Kitchen
3      Dining room
4      Lounge

ClassesInResource:
1      1      1
2      1      2
3      1      3
4      2      1
5      2      3
6      2      4
It may seem more complicated - and it is - but it's really very simple to use, and it makes later coding a lot, lot easier!
 
Share this answer
 
Comments
Aarti Meswania 14-May-14 2:33am    
nice answer
5+! :)
Santosh K. Tripathi 14-May-14 4:39am    
good answer :) 5
george4986 15-May-14 3:29am    
+5v niceExplanation & Example ;-)
create a user-defined function that will convert the comma separated value into a table. user-defined function that takes a comma-delimited value and returns a table containing these values.
See this post
sql-server-comma-separated-string-to-table.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900