SQL问题
对于存储过程输入,例如:
。我需要将值插入到单独的行中,如下所示:
customerID | address city
--------------+------------------
1 | Oregon Portland
2 | Oregon Portland
3 | Oregon Portland
4 | Oregon Portland
我需要有关如何在单个参数中插入CSV值以分隔行的帮助。
答案 0 :(得分:6)
使用Jeff Moden的CSV分离器功能:
declare @CustomerId varchar(8000) = '1,2,3,4,'
, @Address varchar(64) = 'Oregon'
, @City varchar(64) = 'Portland'
select
CustomerId = d.Item
, Address = @Address
, City = @City
from [dbo].[delimitedsplit8K](@CustomerId,',') as d
where d.Item <> ''
测试设置:http://rextester.com/COUJVK17338
返回:
+------------+---------+----------+
| CustomerId | Address | City |
+------------+---------+----------+
| 1 | Oregon | Portland |
| 2 | Oregon | Portland |
| 3 | Oregon | Portland |
| 4 | Oregon | Portland |
+------------+---------+----------+
拆分字符串参考:
答案 1 :(得分:1)
一种方法是递归CTE:
OnClickListener
答案 2 :(得分:0)
使用while循环
WHILE len(@CustomerID) >1
BEGIN
insert into MyTable (customerID, Address, City)
values (left(@CustomerID, charindex(',',@CustomerID)), @Address, @City)
@CustomerID = substring(@CustomerID,
charindex(',',@CustomerID)+1,
len(@CustomerID)-charindex(',',@CustomerID))
END
答案 3 :(得分:0)
使用xQuery的有趣方式
declare @CustomerXML xml = '<i>' + replace(@CustomerID, ',', '</i><i>') + '</i>'
select
i.value('.', 'int') as customerID,
@Address as address,
@City as city
from @CustomerXML.nodes('for $i in /i where $i != "" return $i') as T(i)
答案 4 :(得分:0)
我使用自定义的Table值Split函数,它在自定义指定的分隔符上拆分分隔的字符串,并在表变量中返回结果。
<div class="form-group has-feedback">
<input type="text" class="form-control" placeholder="Phone Number" name="phone" value="{{ old('phone') }}"/>
<span class="glyphicon glyphicon-phone form-control-feedback"></span>
</div>