繁体   English   中英

如何使用存储过程在SQL Server中拆分字符串并将数据插入表中

[英]How to split a string in sql server using stored procedure and insert the data to table

<pre>update d 
  set d.Price = null 
from dbo.SalDocumentDetail d 
left join dbo.StkWarehouse w on w.WarehouseID = d.WarehouseID 
where DocumentID=" + 1 + " 
and DocumentTypeID=" + 2 + " 
and FiscalYear= " + 2016 + " 
and isnull(isPrescription,0) <>1 
and w.POSType is null 
and ProductName BETWEEN ''C' 'AND' 'M''
and Country LIKE ''%land%'''</pre>

实际上,这个字符串只是一个样本,我的原始字符串非常大。 我没有得到一个要点,如果我打破了这个字符串,那么在分割字符串之后,我还必须获取多少变量来捕获数据,我希望将其插入包含Felid和Value列的数据表中?

I want my result like :
<pre>
Felid                         Value
 DocumentID=                    1 
 DocumentTypeID=                2 
 FiscalYear=                   2016 
 isnull(isPrescription,0) <>=     1 
 w.POSType is=                 null 
 ProductName=                   C
 ProductName=                   M 
 Country=                       land 
</pre>
<pre>I Use this function but this function split 'and' not split like what i want in my result i want split 'and,or,like,is,between ' if function find any this split it to two columns (Felid and Value)</pre>

<pre>ALTER FUNCTION [dbo].[fnSplitString] 
(@List NVARCHAR(MAX),@Delimiter  NVARCHAR(255))
RETURNS @Items TABLE(Felid NVARCHAR(Max),Valu nvarchar(MAx))
WITH SCHEMABINDING
AS BEGIN
DECLARE @ll INT=LEN(@List)+1,@ld INT=LEN(@Delimiter); 
WITH a AS
(SELECT     
[end]=COALESCE(NULLIF(CHARINDEX(@Delimiter,@List,1),0),@ll),                      
[VlaueFelid]=SUBSTRING(@List,(select
CHARINDEX('where',@List)+5),COALESCE(NULLIF(CHARINDEX('=',  @List,0),0),@ll) ) ,
[Value]=SUBSTRING(@List,(select CHARINDEX('="',@List)+2),(select CHARINDEX('and',@List))-(select C`enter code here`HARINDEX('="',@List)+3))              
UNION ALL
SELECT 
[end]=COALESCE(NULLIF(CHARINDEX(@Delimiter,@List,[end]+@ld), 0),@ll),                    
[VlaueFelid]=SUBSTRING(@List,[end]+@ld,   COALESCE(NULLIF(CHARINDEX('=',@List, [end]+@ld),0),@ll)-[end]-@ld),
[Value]=SUBSTRING(@List,[end]+@ld+16,  COALESCE(NULLIF(CHARINDEX('=',@List,[end]+@ld),0),@ll)-[end]-@ld-5)                      
 FROM a WHERE [end]< @ll) INSERT @Items SELECT[VlaueFelid],[Value] FROM a WHERE LEN([VlaueFelid])>0 RETURN;
END</pre>

暂无
暂无

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

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