将数字附加到SQL Server中以逗号分隔的列表中
问题描述:
我在JS中有一个代码,如果列表中不存在该值,则将值推送到逗号分隔的字符串。
我也希望类似的东西进入SQL Server。
我尝试过:
我可以将逗号分隔的列表放入列中使用值,我将列表转换为临时表,并检查是否存在新数字。
有没有更好的方法来执行此操作?请帮助我找到。
I have a code in JS which is pushing value to a comma separated string if that value do not exist in the list.
I want the similar thing into SQL Server also.
What I have tried:
I am able to make comma separated list into column with values, i am converting list into a temp table and checking one by if new numbers exists.
Is there any better way to perform this? Please help me finding.
答
在SQL中存储逗号分隔列表通常是一个坏主意,yopu woudl可能会更好地用第二个表和一个外国人密钥。
但是......
It's generally a bad idea to store comma separated lists in SQL at all, yopu woudl probably be better off with a second table and a foreign key.
But...
UPDATE MyTable SET MyColumn = MyColumn + ',' + 'My new item' WHERE MyIDColumn=MyIDValue
Takign再次退出是一个非常困难的过程 - 这也是为什么第二个表是一个更好的设计的原因之一。
Takign one back out again is a much, much harder process - which is one of the reasons why the second table is a much better design.