Solved

Trying to build table with While loop in SQL

Posted on 2012-04-06
2
738 Views
Last Modified: 2012-04-09
Hello and thank you for taking a moment. I am trying to build a  table by looping through the contents of an existing table.  Once built I try to select from the newly created table (@BPRLIKELISTRESULTS)  but sql server gives the following error:

[Msg 4145, Level 15, State 1, Line 32
An expression of non-boolean type specified in a context where a condition is expected, near 'SELECT'.


Line 32 is the select statement at the bottom of my query here below. If there is any advice as to how to solve this , it would be greatly appreciated.

DECLARE @RowsToProcess  int
DECLARE @CurrentRow     int
DECLARE @SelectCol1     int

DECLARE @BPRLIKELISTRESULTS TABLE ( Value varchar(20))



DECLARE @BPRLIKELIST TABLE (RowID int not null primary key identity(1,1), Listitem varchar(20))  

INSERT into @BPRLIKELIST (Listitem)
SELECT Value
FROM         OlisUserFilters
WHERE     (ReqTitle = 'BPR') and (Active = 1) and (Operator = 'Like')  and (UserID = 3183)
 
Select * From @BPRLIKELIST

SET @RowsToProcess=@@ROWCOUNT
SET @CurrentRow=0
WHILE @CurrentRow<@RowsToProcess
BEGIN
     SET @CurrentRow=@CurrentRow+1
 
     Insert into @BPRLIKELISTRESULTS (Value)
     Select Distinct officeLocation
     from VPSI_Driver
     Where (Select Listitem from @BPRLIKELIST where RowID=@CurrentRow)
     
End


SELECT *
FROM @BPRLIKELISTRESULTS
0
Comment
Question by:jazzcatone
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 37817450
I am not convinced this should be in a WHILE loop; however, the issue is that you likely want a JOIN, EXISTS, or something connecting the sub-query to the outer query. As the error indicates, you are simply calling a sub-query within the WHERE clause -- that is not a valid WHERE condition.

EDIT: Does the VPSI_Driver table have a column that associates to Listitem (i.e., the [Value] column of OlisUserFilters)?

In other words, you may be able to do something as simple as this:
SELECT DISTINCT VPSI_Driver.officeLocation
FROM OlisUserFilters
JOIN VPSI_Driver 
  ON VPSI_Driver.Listitem_Equivalent_Column = OlisUserFilters.[Value]
WHERE (OlisUserFilters.ReqTitle = 'BPR') 
  AND (OlisUserFilters.Active = 1) 
  AND (OlisUserFilters.Operator = 'Like')  
  AND (OlisUserFilters.UserID = 3183)
;

Open in new window

0
 
LVL 7

Assisted Solution

by:Anoo S Pillai
Anoo S Pillai earned 250 total points
ID: 37818107
The issue within the code is at :-

 Insert into @BPRLIKELISTRESULTS (Value)
     Select Distinct officeLocation
     from VPSI_Driver
     Where (Select Listitem from @BPRLIKELIST where RowID=@CurrentRow)

The where caluse should be having a column name and operator before the subquery , say like Where <VPSI_Driver_Column> IN  (Select Listitem from @BPRLIKELIST where RowID=@CurrentRow)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now