Solved

Trying to build table with While loop in SQL

Posted on 2012-04-06
2
740 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 35
T-SQL: Stored Procedure Syntax 3 34
access to sql migration 5 25
Database Owner 3 21
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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