[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Trying to build table with While loop in SQL

Posted on 2012-04-06
2
Medium Priority
?
748 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
[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
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 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 1000 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

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.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

649 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