Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Have SQL Loop a column then pass those values with commas to a variable for use as a clause.

Posted on 2008-06-25
12
Medium Priority
?
205 Views
Last Modified: 2010-03-20
What I need:

I need SQL to loop through a column in my database (sortfield)
I need SQL to pass the values that are returned to a variable and add commas behind each value
I need SQL to place the values into a where clause

Example:

Invoice Table:
IDNum      Sortfield
11             1
12             2
14             3

@sortfield will become '1', '2', '3'
my where clause will look like: where sortfield in ('1', '2', '3')

declare @sortfield
set @sortfield ='All'
 
If (@sortfield)='All'
begin
set @sortfield='Number1'+, +'Number2'+, +'Number3'+, +'etc...
end
 
Select * from invoice where sortfield in (@sortfield)

Open in new window

0
Comment
Question by:stephi01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21866197
why not use this:

select * from tablename where sortfield in(select sortfield from someothertable)
0
 

Author Comment

by:stephi01
ID: 21866313
i need the query to loop through the invoice table and pull data based off of the sortfield numbers in that same table. The sortfields aren't refrenced in the same way in another table.

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21866359
I am not sure I understand.  The invoice table has a field named sortfield that you want to filter on.  You want to filter these values based on the sortfield values in another table, right?  What is the name of the table and the name of the field?
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 22

Expert Comment

by:dportas
ID: 21866387
So where do the sortfields come from? In your example they appear to be hard-coded, which doesn't make much sense to me.

Maybe this helps: http://www.sommarskog.se/arrays-in-sql-2005.html
0
 

Author Comment

by:stephi01
ID: 21866631
I am only using one table. The table is called invoice. Everytime a new invoice is created for a file it is assigned an id number and a sortfield. (the sortfield notates in which order the sees the invoice in the GUI of our software). I have a document that loops through the invoice table for that file and prints a page for each invoice. I am trying to allow the user to isolate a specific invoice to print and have gotten that far. I now need to include logic that allows the user to print all invoices if nessicary. I figure that if i can have the user input the word all i can force the procedure perform the above and pull a page for each row.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 1500 total points
ID: 21867073
Ok, I understand a bit better now.  Try this:

declare @sortfield
set @sortfield ='All'
 
select * from invoices
where sortfield = case when @sortfield = 'ALL' THEN sortfield else @sortfield END
0
 

Author Comment

by:stephi01
ID: 21867231
The sortfield column is set as int.
I get this message when I run the above.


Msg 245, Level 16, State 1, Line 15
Conversion failed when converting the varchar value 'A' to data type int.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21867260
what values are in the variable @sortfield?
0
 

Author Comment

by:stephi01
ID: 21868307
it can vary... it will be either a number or the word "All".
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868322
one single valued number or a string of numbers?
0
 

Author Comment

by:stephi01
ID: 21868910
I got this to work by using

declare  @uidnum varchar (3), @Sort int


set @uidnum='3'

if @uidnum='All'
begin
set @sort=0
end
else
if @uidnum <>'All'
begin
set @sort=@uidnum
end

select * from invoice
where sortfield= case when @sort=0 then sortfield else @uidnum end
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21874049
Good deal.
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question