Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 13th, 2007, 04:32 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping through Array Values in T-SQL

Hiya, Newbie here!

I have a function that will spilt comma separated values and output them as one column of values called Item.

Code:
DECLARE @RecipientID INT
DECLARE @ArrayValues VARCHAR (255) 
SET @ArrayValues = 'One, Two, Three and Four'
SELECT * FROM function(@ArrayValues, ',') as ArraySplit
How can I loop through each value in ArraySplit and compare that value to values from a specific column in a table, and then pull out other columns from that table.

For example:

Code:
While
    SELECT * FROM function(@ArrayValues, ',') as ArraySplit
Begin
    Select TableName.ID, TableName.Name
    Where TableName.TableColumn = ArraySpilt
    Print 'It worked!'
Else
   Print 'It didn't work!'
End
I am not sure of the syntax, and I am hoping my logic for this task is correct.

Any help appreciated!
 
Old April 13th, 2007, 04:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can store that into a table (or temporary table) and do a join with the other table for comparision. And make sure you delete the table that you created during this process.

You can use something like this.
Code:
SELECT * INTO SomeTable FROM function(@ArrayValues, ',') as ArraySplit
Hope that helps.

_________________________
- Vijay G
Strive for Perfection
 
Old April 13th, 2007, 05:34 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aaah cheers! Could I trouble you for more help?

I have tried doing the JOIN to compare the Array value with entries in another table
Code:
DECLARE @RecipientID INT
DECLARE @ArrayValues VARCHAR (255) 
SET @ArrayValues = 'One, Two, Three and four'

SELECT * INTO CategorySelection FROM function(@ArrayValues, ',') as ArraySplit

Select * FROM Categories
JOIN Categories on Categories.[Name] = ArraySplit.Item
I get this error

Code:
Tables or functions 'Categories' and 'Categories' have the same exposed names. Use correlation names to distinguish them.
 
Old April 13th, 2007, 05:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Is the join done correctly? Should be something like this.
Code:
Select * FROM Categories
JOIN CategorySelection on Categories.[Name] = CategorySelection.COLUMNNAME
_________________________
- Vijay G
Strive for Perfection
 
Old April 13th, 2007, 06:14 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aah I see! Ta very much!






Similar Threads
Thread Thread Starter Forum Replies Last Post
looping through static array mister_mister XSLT 9 March 21st, 2008 01:30 PM
3D Array Looping - Formatting in table jordan23 XSLT 3 April 30th, 2007 11:57 AM
3D Array - Looping jordan23 XSLT 5 April 27th, 2007 07:08 AM
Looping controls and array redim mega Excel VBA 2 April 19th, 2005 11:57 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.