Author |
Topic |
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 11:13:46
|
Hello everyone,can someone advise to separate the data into another temp table? i have records look like below, i need the separate col1 value into another temp table, see the output looks like.Declare @tbl1 table (ID INT,Col1 varchar(50),Col2 varchar(50))INSERT INTO @tbl1SELECT 1,'A1;B1;C1;D1',NULLINSERT INTO @tbl1SELECT 2,'X1;X2',NULLINSERT INTO @tbl1SELECT 3,'M1',NULLINSERT INTO @tbl1SELECT 4,'N1;N2;N3',NULLselect * from @tbl1OUTPUT :ID Col11 A11 B11 C11 D12 X12 X23 M14 N14 N24 N3 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 12:32:28
|
Anyone has an idea? |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-03-05 : 13:05:20
|
You can use a string splitter function - there are several available on this forum, for example: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648These links also may be useful (Fig 21):http://www.sqlservercentral.com/articles/Tally+Table/72993/ |
 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 13:17:05
|
How do i pass table name here : @str i have to pass values (col1) from @tbl1 table-- Create temp table to test inserting values intocreate table #t (num int)-- Create a comma delimited string to test withdeclare @str varchar(500)select @str = '4,2,7,7834,45,24,45,77'------------------------------------------------------------ Code to load the delimited string into a table -------------------------------------------------------------- Create insert for comma delimited valuesdeclare @sql varchar(8000)select @sql = 'insert into #t select '+ replace(@str,',',' union all select ')-- Load values from comma delimited string into a tableexec ( @SQL )------------------------------------------------------------------------------------------------------------------ Select values from temp table to show resultsselect * from #tif object_id('tempdb..#t') is not null begin drop table #t end |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-03-05 : 13:29:40
|
Personally, I would use a table functionCREATE FUNCTION [dbo].[udf_Table](@ParmList varchar(8000), @Delim varchar(20))RETURNS @table TABLE (Parameter varchar(255))AS /* SELECT * FROM dbo.udf_Table( 'a|~|b|~|c', '|~|')*/BEGIN DECLARE @x int, @Parameter varchar(255) WHILE CHARINDEX(@Delim, @ParmList)-1 > 0 BEGIN INSERT INTO @table(Parameter) SELECT SUBSTRING(@ParmList,1,CHARINDEX(@Delim, @ParmList)-1) SELECT @ParmList = SUBSTRING(@ParmList,CHARINDEX(@Delim, @ParmList)+LEN(@Delim), LEN(@ParmList)-CHARINDEX(@Delim,@ParmList)) END INSERT INTO @table(Parameter) SELECT @ParmList RETURNEND Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 13:45:09
|
how do we pass the table name instate of values.SELECT * FROM dbo.udf_Table( 'a|~|b|~|c', '|~|')select * from dbo.udf_table (@tbl1) ?? |
 |
|
X002548
Not Just a Number
15586 Posts |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 14:01:33
|
i got invalid column name |
 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2012-03-05 : 14:37:26
|
any update?? |
 |
|
X002548
Not Just a Number
15586 Posts |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-03-05 : 15:55:36
|
use CROSS APPLY, not CROSS JOIN--Gail ShawSQL Server MVP |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-03-05 : 16:22:41
|
...and there it is..Thanks GailDeclare @tbl1 table (ID INT,Col1 varchar(50),Col2 varchar(50))INSERT INTO @tbl1 (ID, Col1, Col2)SELECT 1,'A1;B1;C1;D1',NULL UNION ALLSELECT 2,'X1;X2',NULL UNION ALLSELECT 3,'M1',NULL UNION ALLSELECT 4,'N1;N2;N3',NULLSELECT ID, Parameter FROM @tbl1 CROSS APPLY dbo.udf_Table( Col1, ';')ORDER BY ID, Parameter Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-03-05 : 16:26:43
|
So CROSS APPLY Itself is a table valued function???BOLquote: The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input. Note: To use APPLY, the database compatibility level must be at least 90. There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function.
Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-03-05 : 17:16:45
|
No, cross apply is a join operator, just like inner join, outer join, etc. --Gail ShawSQL Server MVP |
 |
|
|