Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
perels
Starting Member
23 Posts |
Posted - 2004-10-29 : 05:27:34
|
Hi all,Here's a tough one:I need to use ntext in subquery.I have two tables; one called Media and another called MediaGroup:Media-tableMediaID intName nvarchar(20)MediaGroup-tableMediaGroupID intRecipients ntext -- contains comma separated MediaID's (CSV) -- like 1,4,5,7,8,99,293,423 I'd normally use the varchar datatype on the Recipients column (instead of ntext) for a job like this, but because the data contained within the Recipients column at times will exceed the maximum length (8,000) of varchar datatype I'm bound to ntext.I wanna do the following (that is using the ntext datatype (Recipients) in a subquery to return all the Media Name's.):SELECT Media.Name FROM Media WHERE MediaID IN ((SELECT Recipients FROM MediaGroup WHERE MediaGroupID = 7)) If I try to run that T-SQL statement SQL Server 2000 will spit out the following:Error 279: the text, ntext, and image data types are invalid in this subquery or aggregate expression. I have read the following: [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=20675[/url] and the article[url]http://www.sqlteam.com/item.asp?ItemID=11499[/url].The problem with the suggested function (CsvToInt) is that it will hold a value of max 1,000 chars - of course I could modify it to take 8,000, but that is not enough for this task. - Maybe a cursor or something would do the trick? I don't know, but I hope some of you out there has an educate guess (and some code ) on what to do in my case.Thanx in advance. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-10-29 : 06:43:02
|
| ???If that did compile would you expect it to work?Try it with a convert varchar.clueyou have the equivalent ofselect 1 where 1 in (select convert(text,'1,2,3'))think about (and try maybe)select 1 where 1 in (select '1,2,3')orselect 1 where 1 in ('1,2,3')==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
perels
Starting Member
23 Posts |
Posted - 2004-10-29 : 07:58:07
|
Hmm... not clear on what you mean.If I convert to varchar Recipient data longer than 8,000 chars would be truncated, so I need a work-around to this problem.I wanna do this, f.x.:SELECT Name FROM Media WHERE MediaID IN (SELECT "1,2,3,5,40,62..." FROM MediaGroup WHERE MediaGroupID = 7)The problem is that the "1,2,3,5,40,62..." is of datatype ntext - which is not allowed in a subquery... so I need a work around...quote: Originally posted by nr ???If that did compile would you expect it to work?Try it with a convert varchar.
|
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-10-29 : 09:28:10
|
| >> The problem is that the "1,2,3,5,40,62..." is of datatype ntext - which is not allowed in a subquery... so I need a work around...No it isn't - see my previous post. If it was varchar it still wouldn't work.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2004-10-29 : 10:00:09
|
| Edit: I misread your post forget what I said. |
 |
|
|
perels
Starting Member
23 Posts |
Posted - 2004-10-29 : 10:19:27
|
Ahhh, sorry I forgot something :-(I've created this function (based on code found somewhere).CREATE FUNCTION CSV2Table_fn(@PInStrSource varchar(8000) = NULL,@pinChrSeparator char(1))/* DECLARE @CSV as nvarchar(10) -- this must be big enough to hold all the separated values SET @CSV = '7;9;13' SELECT ID,Name FROM Users WHERE ID IN ( SELECT ItemValue FROM CSV2Table_fn(@CSV) ) Output ID Name 7 Eric Johnson 9 John M. 13 Fooboo*/ RETURNS @ARRAY TABLE(ItemValue varchar(1000))ASBEGIN-- DECLARE @pInChrSeparator char(1) DECLARE @CurrentStr varchar(2000) DECLARE @ItemStr varchar(200) SET @CurrentStr = @PInStrSource WHILE Datalength(@CurrentStr) > 0 BEGIN IF CHARINDEX(@pInChrSeparator, @CurrentStr,1) > 0 BEGIN SET @ItemStr = SUBSTRING (@CurrentStr, 1, CHARINDEX(@pInChrSeparator, @CurrentStr,1) - 1) SET @CurrentStr = SUBSTRING (@CurrentStr, CHARINDEX(@pInChrSeparator, @CurrentStr,1) + 1, (Datalength(@CurrentStr) - CHARINDEX(@pInChrSeparator, @CurrentStr,1) + 1)) INSERT @ARRAY (ItemValue) VALUES (@ItemStr) END ELSE BEGIN INSERT @ARRAY (ItemValue) VALUES (@CurrentStr) BREAK; END END RETURNEND That way I could do the following:DECLARE @Recipients varchar(8000)SELECT @Recipients = CAST(Recipients as varchar(8000)) FROM MediaGroup WHERE MEDIAGroupID = 7SELECT Media.Name FROM Media WHERE MediaID IN (SELECT ItemValue FROM CSV2Table_fn(@Recipients,',')) My problem is still the varchar(8000) datatype... to small... any suggestions? |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-10-29 : 13:29:25
|
| A function will take a text variable so you could change your function to do a charindex on 8000 chars from the current position and update the currentposition accordingly (if the text variable does work properly).This function seems to keep changiong the string it is searching rather than using a current position.seehttp://www.nigelrivett.net/f_GetEntryDelimiitted.htmlwhich uses the position offset for the charindex==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
perels
Starting Member
23 Posts |
Posted - 2004-11-01 : 04:28:03
|
| It's still not clear to me what I should do, f_GetEntryDelimiitted does not "do a charindex on 8000 chars from the current position and update the currentposition accordingly" - i'm a bit confused.... could you or somebody else help me with this task?Also, would you be so kind to post some code? |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-11-01 : 12:24:06
|
| It doesn't do it but it is easy to replace the string in the charindex by a substring of the text value. Instead of using the offset value of the charindex use a substring.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|