将数字附加到SQL Server中以逗号分隔的列表中 [英] Append number to a comma separated list in SQL server

查看:113
本文介绍了将数字附加到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.


这篇关于将数字附加到SQL Server中以逗号分隔的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆