![](/img/trans.png)
[英]Insert data in to table using xml and stored procedure in SQL Server 2008
[英]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.