SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 cross apply
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aakcse
Aged Yak Warrior

India
517 Posts

Posted - 06/29/2012 :  08:34:11  Show Profile  Reply with Quote
In a table column I have data as below
'A2','act1'
'+','act2'
's9','act4,act8,act12'

I want to insert into other table as below

A2 act1
+ act2
s9 act4
s9 act8
s9 act12

I have a tabled value function which takes string & delimiter and returns results


CREATE FUNCTION StringToTable (@Delimiter VARCHAR(5), @String VARCHAR(max))

RETURNS @RtnValue TABLE ([ID] INT IDENTITY(1, 1),[Value] VARCHAR(max))
AS 
BEGIN 
DECLARE @Cnt INT
SET @Cnt=1

WHILE (CHARINDEX(@Delimiter, @String)>0) --charindex(','    'actuser1')
BEGIN
INSERT INTO @RtnValue ([Value])
SELECT [Value] = LTRIM(RTRIM(SUBSTRING(@String, 1, CHARINDEX(@Delimiter, @String)-1)))

SET @String=SUBSTRING(@String, CHARINDEX(@Delimiter, @String)+1, LEN(@String))
SET @Cnt=@Cnt+1
END

INSERT INTO @RtnValue ([Value])
SELECT [Value] = LTRIM(RTRIM(@String))

RETURN
END


-Neil

Edited by - aakcse on 06/29/2012 09:17:12

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/29/2012 :  09:18:30  Show Profile  Reply with Quote
Assuming the columns are Col1 and Col2:
INSERT INTO YourNewTable
(Col1, Col2)
SELECT
   a.Col1,
   b.[Value]
FROM
   YourTable a
   CROSS APPLY
   ( SELECT [Value] FROM dbo.StringToTable(',',a.Col2)) b

Go to Top of Page

aakcse
Aged Yak Warrior

India
517 Posts

Posted - 06/29/2012 :  10:29:22  Show Profile  Reply with Quote
Thanks, I need help to develop the else part of the below code in the SP, The IF part is working fine & Giving the desired result
I am trying handle this kind of data 'a','p,q,r' in the else part

rest all explained in comment




@LC = 1
set @LT= (SELECT TOP 1 ID FROM #L ORDER BY ID DESC) --#L is the table which contains data
UPDATE #L SET L=''''+REPLACE(lump,@Delim,''',''')+''''  -- now the delimter is ',' --after this L= 'a','p,q,r' type of data for all rec
--ufn_CountChar this function returns number of delimiter
WHILE @LC<=@LT 
  BEGIN   -- here I inserted all the normal values data e.g. 'a','b'  else part should take care of kind of 'a','p,q,r'  
  SELECT @No_Of_Delim = ufn_CountChar(L,',') FROM #L WHERE ID = @LC 
 
  --@No_Of_Col is the total number of columns in the table
  IF @No_Of_Col = @No_Of_Delim+1 -- +1 coz say e.g. no of col=2 then delimiter will 1 
	 BEGIN   
		SELECT @SQLString = 
		'INSERT INTO '+@v_ToTableName+'	VALUES(' + L + ')'
		FROM #L WHERE ID = @LC  
		EXEC (@sqlstring)
	 END
  ELSE  --'a','b,c,d,e,f' like wise data in L column of #L
     
     BEGIN
		
		INSERT INTO @v_ToTableName -- YourNewTable
		  @header  --(Col1, Col2) --@header is passed as a parameter to this SP it contails values like 'col1 varchar(10), col2 varchar(20)'
		SELECT
		a.Col1,
		b.[Value]
		FROM
		YourTable a
		CROSS APPLY
		( SELECT [Value] FROM dbo.StringToTable(',',a.Col2)) b
    END
set @lc=@lc+1
end -- end of while



-Neil

Edited by - aakcse on 06/29/2012 10:31:29
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/29/2012 :  11:56:10  Show Profile  Reply with Quote
What is the definition of the @v_ToTableName table? You have to list the columns of that table in the insert list. Also, there must be one column in the select list for each column you list in the insert list.
Go to Top of Page

aakcse
Aged Yak Warrior

India
517 Posts

Posted - 06/29/2012 :  14:46:45  Show Profile  Reply with Quote
@v_ToTableName is the to table name, I am passing to and from table names as parameter to the sp and col names, of from table as a parameter

I have now changed the else part as below, however this might cause performance ..



	BEGIN
	   SELECT @Var = L from #L WHERE ID = @L
		SET @Var1stCol = SUBSTRING(@VAR,1,CHARINDEX(',',@Var)-1 )
		SET @VAR = SUBSTRING(@VAR,CHARINDEX(',',@Var)+1,LEN(@VAR)) --'b,c,d,e'
		SET @NO_OF_DELIM2=@No_Of_Delim
	 
		WHILE (@NO_OF_DELIM2 > 0)	-- 'a','b,c,d,e'  4>0-- 3>0 --2>0 --1>0
		BEGIN
			IF @NO_OF_DELIM2=1 
			 SET @REST_VAR = @VAR
			ELSE 
			BEGIN
			  SET @REST_VAR = LEFT(@VAR,CHARINDEX(',',@VAR)-1) +''''  --'b'--'c'--'d'--
			  SET @VAR = ''''+SUBSTRING(@VAR,CHARINDEX(',',@Var)+1,LEN(@VAR)) --'c,d,e'--'d,e'--'e'
			END
			  SELECT @SQLString = 'INSERT INTO '+@v_ToTableName+ ' VALUES('+@Var1stCol+ ','+@REST_VAR+')'
			  EXEC (@SQLString)
			  SET @NO_OF_DELIM2 = @NO_OF_DELIM2 - 1 --3--2--1--0
	END


-Neil
Go to Top of Page

aakcse
Aged Yak Warrior

India
517 Posts

Posted - 06/29/2012 :  16:39:23  Show Profile  Reply with Quote
I have tested the above code with different data sets, it is working perfectly only problem might be in future is with performance

-Neil
Go to Top of Page

aakcse
Aged Yak Warrior

India
517 Posts

Posted - 07/02/2012 :  15:14:19  Show Profile  Reply with Quote
Hi Sunitha,

I am facing problem with data like

'a','b','x,y,z'

I have programmed for 'a','x,y,z' how to deal with above data, Can I use patindex and charindex to separate the 'x,y,z' and 'a','b'?



-Neil
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000