tbl_claimsSQL:分割單元成行
ClaimNo|StatusDate | FromDate
----------------------------------
1076| 2014-02-21 | 2014-02-10
1029| 2014-02-22 | 2014-01-10
tbl_settledagainst
Claimno | Amount | VerificationTeam_ClaimNo
--------------------------------------------------
AC1006 | 1000 |, 1076, 1029
AC1034 | 0.00 |, 1064, 1065, 1066
AC1035 | 0.00 |, 1086
第一表用於存儲A型聲稱第二表用於存儲乙類型的權利要求。 column tbl_settledagainst.VerificationTeam_ClaimNo
has tbl_claims.claims
ie。 索賠1076,1029已經結案agaist AC1006(B類)索賠。 現在的問題是,我想用這種方式來顯示數據:
Type_A_claim | Type_B_Claim | Amt | StatusDate
-------------------------------------------------
1076 | AC1006 | 1000 | 2014-02-21
1029 | AC1006 | 1000 | 2014-02-22
預先感謝您。
我有這個sql函數,我通常用它來分割基於分隔符的字符串。
如:SELECT * FROM dbo.split( '100,200,ABC', '')
**Create FUNCTION [dbo].[Split](@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z
-- ERO FIRST TIME IN LOOP
SELECT @INDEX = 1
-- following line added 10/06/04 as null
-- values cause issues
IF @String IS NULL RETURN
WHILE @INDEX !=0
BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS SET
INSERT INTO @Results(Items) VALUES(@SLICE)
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0 BREAK
END
RETURN
END**
您應該妥善存放您的數據第一個地方。這個問題是找到一個不存在的問題的解決方案。 – podiluska