Author |
Topic |
evanburen
Posting Yak Master
167 Posts |
Posted - 2008-02-06 : 22:21:08
|
I've been reading an article about creating a split function which will take a comma delimited string with id's as input parameter for a SQL query. I need to pass a comma delimmited string of records IDs from checkboxes on a .aspx page and return selected records. I'm not getting any errors from the function or stored procedure but it returns every record in the table regardless of how many Ids I pass to it. Split function:IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE SPECIFIC_NAME = N'Split' ) DROP FUNCTION SplitGOCREATE FUNCTION dbo.Split( @ItemList NVARCHAR(4000), @delimiter CHAR(1))RETURNS @IDTable TABLE (Item VARCHAR(50)) AS BEGIN DECLARE @tempItemList NVARCHAR(4000) SET @tempItemList = @ItemList DECLARE @i INT DECLARE @Item NVARCHAR(4000) SET @tempItemList = REPLACE (@tempItemList, ' ', '') SET @i = CHARINDEX(@delimiter, @tempItemList) WHILE (LEN(@tempItemList) > 0) BEGIN IF @i = 0 SET @Item = @tempItemList ELSE SET @Item = LEFT(@tempItemList, @i - 1) INSERT INTO @IDTable(Item) VALUES(@Item) IF @i = 0 SET @tempItemList = '' ELSE SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i) SET @i = CHARINDEX(@delimiter, @tempItemList) END RETURNEND GOSproc:CREATE PROCEDURE up_ExportSelectedDirectors @p_selectedDirectors NVARCHAR(4000) AS BEGIN SELECT * FROM v_SearchResultsDirectors WHERE IDDir in (SELECT IDDir FROM split(@p_selectedDirectors, ',')) ENDGOEven if I manually run something like this, I get all records instead of just the two.SELECT * FROM v_SearchResultsDirectors WHERE IDDir in (SELECT IDDir FROM split('1234,4321', ',')) Thanks |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-06 : 22:42:14
|
Do you get the expected results from these statements?SELECT * FROM v_SearchResultsDirectors WHERE IDDir in (1234,4321) SELECT IDDir FROM split('1234,4321', ',')EDIT:Actually, your function doesn't return [IDDir] column, it returns [Item] so your statement should blow up, not return all the rows???Be One with the OptimizerTG |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2008-02-06 : 23:15:35
|
Strangely, it didn't blow up - it just didn't work. I changed Item to IDDir in the function and it works now. Thanks a bunch. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-11 : 04:47:11
|
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htmMadhivananFailing to plan is Planning to fail |
 |
|
Dimon
Starting Member
3 Posts |
Posted - 2008-09-09 : 05:04:47
|
How can I use the split-function in a real sql-statement like:select (select * from split(my_column), ',') from my_table  THX |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 07:42:12
|
SELECT t.*FROM Table1 AS tINNER JOIN dbo.fnSplit() AS x ON x.Col1 = t.Col1 E 12°55'05.63"N 56°04'39.26" |
 |
|
Dimon
Starting Member
3 Posts |
Posted - 2008-09-10 : 06:54:53
|
Thank you for answer but I have to use that function for the whole table and not for some itemsThe parameter in the function has to be a column from tableIf write this (original)SELECT P.institutFROM tbl_person AS PINNER JOIN dbo.sp_Split(P.institut,'') AS x ON x.Item = P.institutERROR: "P.institut" couldnt be joinedWithout P. I get error -> unknown column "institut" THX |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-10 : 08:31:26
|
TrySELECT P.institutFROM tbl_person AS Pwhere ','+@institut+',' like '%,'+cast(Item as varchar(10))+',%'MadhivananFailing to plan is Planning to fail |
 |
|
Dimon
Starting Member
3 Posts |
Posted - 2008-09-10 : 10:37:30
|
quote: Originally posted by madhivanan
Thanx I have got another tasklookcolumn 'institut' contains data like this'TZ : 01.04.2003 - 30.09.2003 Gastwissenschaftler¶TZ : 10.05.2004 - 30.04.2006 Gastwissenschaftler¶TZ : 01.04.2006 - 31.12.2007 Gastwissenschaftler'I have to split this string with '¶'TZ : 01.04.2003 - 30.09.2003 GastwissenschaftlerTZ : 10.05.2004 - 30.04.2006 GastwissenschaftlerTZ : 01.04.2006 - 31.12.2007 GastwissenschaftlerI have more then 35.000 Items in column institutTHX |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-11 : 03:51:28
|
quote: Originally posted by Dimon
quote: Originally posted by madhivanan
Thanx I have got another tasklookcolumn 'institut' contains data like this'TZ : 01.04.2003 - 30.09.2003 Gastwissenschaftler¶TZ : 10.05.2004 - 30.04.2006 Gastwissenschaftler¶TZ : 01.04.2006 - 31.12.2007 Gastwissenschaftler'I have to split this string with '¶'TZ : 01.04.2003 - 30.09.2003 GastwissenschaftlerTZ : 10.05.2004 - 30.04.2006 GastwissenschaftlerTZ : 01.04.2006 - 31.12.2007 GastwissenschaftlerI have more then 35.000 Items in column institutTHX
Search for Split function hereMadhivananFailing to plan is Planning to fail |
 |
|
|