DECLARE @USERNAME VARCHAR(MAX) = 'test1,test2';
DECLARE @EMAILID VARCHAR(MAX) = 'test6,testr3' ;
DECLARE @lenuser INT;
DECLARE @lenemailid INT;
DECLARE @startIndex INT = 0;
declare @indexemail INT = 0;
DECLARE @endIndex INT;
DECLARE @startEmailIndex INT = 0;
DECLARE @endEmailIndex INT;
WHILE @startIndex <= LEN(@USERNAME) AND @startEmailIndex <= LEN(@EMAILID)
BEGIN
SELECT @endIndex = charindex(',',@USERNAME,@startIndex);
SELECT @endEmailIndex = charindex(',',@EMAILID,@startEmailIndex);
if @endIndex = 0
begin
SELECT @endIndex = LEN(@USERNAME);
end
if @endEmailIndex = 0
begin
SELECT @endEmailIndex = LEN(@EMAILID);
end
INSERT INTO dbo.TABLE_1 VALUES(SUBSTRING(@USERNAME,@startIndex,@endIndex), SUBSTRING(@EMAILID,@startEmailIndex,@endEmailIndex));
SELECT @startIndex = @endIndex +1;
SELECT @startEmailIndex = @endEmailIndex +1;
END
這應該work.But可以通過提取上述的功能分割操作除去上面的冗餘。只是你的表名替換TABLE_1並在需要的INSERT語句添加其他列
能否請你告訴它的數據庫和語言。如果你希望做這個代碼端或SQL腳本? – Swetha