| Author |
Topic |
|
hismightiness
Posting Yak Master
164 Posts |
Posted - 2005-06-24 : 14:11:47
|
I have a nasty array of numbers that I need to get a result set out of from a "contact" database table. The following select statement would return the results directly below it.SELECT c.contact_id, c.firstname + ' ' + c.lastname + ' <' + c.email + '>' AS contactFROM contacts cWHERE c.contact_id IN (5,9,15) contact_id...contact-----------..---------------------------------------5............John Dough <email1@domain1.com>9............Krusty Clown <email2@domain2.com>15...........Bart Simpson <email3@domain3.com>(3 row(s) affected)If I had done this right, this would be exactly the results I WOULD have. However, I am performing this through the use of a SProc and not the SQL Query Analyzer:CREATE PROCEDURE dbo.sp_MyArraySProc( @ContactList nvarchar(1000))ASSET NOCOUNT ONSELECT c.contact_id, c.firstname + ' ' + c.lastname + ' <' + c.email + '>' AS contactFROM contacts cWHERE c.contact_id IN (@ContactList)RETURN This throws an error becase the parameter @ContactList is a text variable, and contact_id is an integer. This doesn't work either, it just returns nothing:CREATE PROCEDURE dbo.sp_MyArraySProc( @ContactList nvarchar(1000))ASSET NOCOUNT ONSELECT c.contact_id, c.firstname + ' ' + c.lastname + ' <' + c.email + '>' AS contactFROM contacts cWHERE CAST(c.contact_id AS VARCHAR) IN (@ContactList)RETURN How am I SUPPOSED to do this?- - - -- Will -- - - -http://www.servicerank.com/ |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
hismightiness
Posting Yak Master
164 Posts |
Posted - 2005-06-24 : 15:23:17
|
Excellent!!! That is exactly what I needed! It worked like a charm... For those who may stumble upon this later, here is what I ended up with:ALTER PROCEDURE dbo.sp_GetContacts( @ContactList varchar(1000))ASSET NOCOUNT ON-- @ContactList is the array we wish to parse-- @Separator is the separator charactor such as a commaDECLARE @Separator varchar(1)SELECT @Separator = ','DECLARE @separator_position int -- This is used to locate each separator characterDECLARE @array_value varchar(1000) -- this holds each array value as it is returned-- For my loop to work I need an extra separator at the end. I always look to the-- left of the separator character for each array value-- SET @ContactList = @ContactList + @Separator-- I already have a trailing comma in mine-- Create a table to hold the new valuesCREATE TABLE #ContactTable (contact_id int, contact nvarchar(251))-- Loop through the string searching for separtor charactersWHILE PATINDEX('%' + @separator + '%' , @ContactList) <> 0 BEGIN -- patindex matches the a pattern against a string SELECT @separator_position = PATINDEX('%' + @separator + '%' , @ContactList) SELECT @array_value = LEFT(@ContactList, @separator_position - 1) INSERT INTO #ContactTable SELECT c.contact_id, c.firstname + ' ' + c.lastname + ' <' + c.email + '>' AS contact FROM contacts c WHERE c.contact_id = @array_value -- This replaces what we just processed with an empty string SELECT @ContactList = STUFF(@ContactList, 1, @separator_position, '')ENDSELECT * FROM #ContactTable ORDER BY contactDROP TABLE #ContactTableRETURN- - - -- Will -- - - -http://www.servicerank.com/ |
 |
|
|
hismightiness
Posting Yak Master
164 Posts |
Posted - 2005-06-24 : 15:30:25
|
| Oops! I didn't say thank you... Thank you for your help. I appreciate it greatly!- - - -- Will -- - - -http://www.servicerank.com/ |
 |
|
|
Crito
Starting Member
40 Posts |
Posted - 2005-06-24 : 15:30:35
|
| I'd load a global ##temp table with the array elements and call the proc without passing a parm. You should explicitly drop the global ##temp table at end of proc though, otherwise, unlike a #local temp table, it'll persist until the connection is closed.So your where clause would end up looking something like:WHERE c.contact_id IN (SELECT number from ##array)Gun for hire, have horse, will travel. |
 |
|
|
hismightiness
Posting Yak Master
164 Posts |
Posted - 2005-06-24 : 15:37:55
|
| Sorry Crito... I should probably be considered a "newbie" because I have been the "unofficial" DBA at my company for just under a year and what you posted made me scratch my head. :)I understand the concept of having global objects, but since I have not yet read that chapter in my Wrox SQL Server 2000 book, I fall short of understanding how to do what you suggest and the benefits behind doing it that way. Can you elaborate a bit more for me please? If not, that's fine. I am sure I will learn it soon anyhow. :) Thank you for your post!- - - -- Will -- - - -http://www.servicerank.com/ |
 |
|
|
Crito
Starting Member
40 Posts |
Posted - 2005-06-24 : 15:51:40
|
| You create temp tables just like any other table, with a create table statement. A single number sign (#) makes the scope local. Double number signs (##) makes the scope global. A local temp table is automatically destroyed when you exit the sproc. A global temp table persist until the connection is closed.----------------------------------Gun for hire, have horse, will travel. |
 |
|
|
hismightiness
Posting Yak Master
164 Posts |
Posted - 2005-06-24 : 16:22:29
|
| Wouldn't it be more beneficial to not keep such a table in a global state unless other SProcs are going to use it? It seems to me that the Global temp table would just sit on some memory that could be free doing something else. Is there a performance benefit to using a global table? Is this why you suggested it?I just want to understand so that I can make an informaed decision going forward on my development methods. Thank you again for your replies.- - - -- Will -- - - -http://www.servicerank.com/ |
 |
|
|
|