且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server的单个列中插入逗号值

更新时间:2021-11-11 01:05:04

不要。它是一个使用的PITA,虽然插入值很简单,但它很快就会变成肮脏的。

相反,有一个链接表:

Don't. It's a PITA to use, and though it's simple to insert the values to start with, it rapidly descends into nastiness.
Instead, have a "linking" table:
ID Name Dept   Sal      
 1  Abc  Mech  20000    


Techid  Techname
   1       Abc
   2       def
   3       ghi


DeptTechs
ID   DeptId   TechId
1      1        1
2      1        2
3      1        3

然后使用当你想要检索它时,JOIN组合信息。

You then use a JOIN to combine information when you want to retrieve it.

SELECT d.*, t.TechName FROM Depts d
JOIN DeptTech dt ON d.ID = dt.DeptId
JOIN Techs t ON d.TechID = t.ID

这样,添加和删除技术是一项微不足道的工作:只需删除链接行。

That way, adding and removing techs is a trivial job: just delete the linking row.