Solved

Trying to build table with While loop in SQL

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

717 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