簡體   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