简体   繁体   English

在表格中插入以逗号分隔的ID

[英]Insert comma delimited ids into a table

I need to insert records into a SQL Server table. 我需要将记录插入到SQL Server表中。 The client send ids I need to insert into a specific column in the table: 客户端发送我需要插入表中特定列的ID:

(2,4,123,1357,1234,5657,753);

Using this function I'm splitting the comma delimited string, but not sure how to insert it to the table along with the other columns 使用函数,我将分割逗号分隔的字符串,但不确定如何将其与其他列一起插入表中

I need to create something that will generate inserts such as: 我需要创建一些将生成插入的内容,例如:

insert into table_name (id,column_2,column_3) values (2, column_s_some_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (4, column_s_some_other_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (123, column_s_some_value, column_3_some_value);
ETC...

How can I achieve that? 我该如何实现?

the split function is a Table-Valued Function, which means it can be treated as a table, and you can do an INSERT..SELECT split函数是一个表值函数,这意味着可以将其视为表,并且可以执行INSERT..SELECT

insert into table_name (id,column_2,column_3)
SELECT s.item, column_s_some_value, column_3_some_value
FROM Split(@input_string, ',') s
{JOINS if needed to get other column values}

If you do want to split strings, the most efficient way to get that done is through the use of the tally table as outlined here by Jeff Moden . 如果您确实想分割字符串,最有效的方法就是使用Jeff Moden概述的理货表。 I'd strongly suggest you use this method in just about any version of SQL Server. 我强烈建议您在几乎任何版本的SQL Server中都使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM