Solved

Incorrect syntax near the keyword where subquery

Posted on 2008-10-31
5
852 Views
Last Modified: 2012-05-05
Hi all

can anyone help me debug the query below
I am having an error  "Incorrect syntax near the keyword where subquery"

If you do have a work around for the problem below; I will prefer the work around.

Col1|   Col1|     |Col3|   |Col4|       |Col5|   |Col6|                  |Col7|                                          |Col8|          
200 |   Mind|     |Price|   |Unnit|     |0|      01.01.2008 14:47:00      30.12.2008 14:47:00          |1|
200 |   Mind|     |Price|   |Unnit|     |1|     31 .10.2008 22:47:00      1.11.2008 14:47:00           |2|
200 |   Mind|     |Price|   |Unnit|     |1|      16.11.2008 14:47:00      20.11.2008 14:47:00          |3|
200 |   Mind|     |Price|   |Unnit|     |1|      21.11.2008 14:47:00      25.11.2008 14:47:00          |4|
600 |   Mind|     |Price|   |Unnit|     |1|                                                                                         |5|
400 |   Mind|     |Price|   |Unnit|     |1|                                                                                         |6|
600 |   Mind|     |Price|   |Unnit|     |1|                                                                                         |7|
800 |   Mind|     |Price|   |Unnit|     |1|                                                                                         |8|
200 |   Mind|     |Price|   |Unnit|     |1|        29.10.2008 14:47:00      29.10.2008 14:47:00         |9|
 
In This regard
the row below will be selected because it meets the criteria
200 |   Mind|     |Price|   |Unnit|     |1|     31 .10.2008 22:47:00      1.11.2008 14:47:00           |2|

The where condition is (|Col6| <= getdate() and |Col7| >= getdate())'      

Note I need just a query that will retrive only the primary key  |2| of Col8
set @sql_where = 
 

'where (T00A.F2AnID='+ltrim(rtrim(str(@intAID)))+') 

	and              

		(T01_ART.F3BID = 

									(select F3BID  

									 from 	T01_ART	

									 where 	(F2AnID='+ltrim(rtrim(str(@intAID)))+')

									 and 	F03_Art = T00A.F03_Art

									 and 	(T01_ART.F0_Preis = (SELECT Max(ISNULL(F0_Preis, 0)) 

																FROM 	T01_ART	inner join T00A									

																where 	(T01_ART.F2AnID='+ltrim(rtrim(str(@intAID)))+')

																and 	T01_ART.F03_Art = T00A.F03_Art 

																and 	F010_Gue <= getdate() and F011_Gue >= getdate()

																GROUP BY F03_Art

																))

									))

	

	

	and (F010_Gue <= getdate() and F011_Gue >= getdate())'	

	

	set @sql_where = @sql_where+' where  (T00A.F027=0 or T00A.F029=3 or T00A.F027=7)'

Open in new window

0
Comment
Question by:ZURINET
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22848615
you are missing after inner join on what you joining them (line 12 in your code)

something like

FROM    T01_ART inner join T00A on T00A.something = T01_ART .something
0
 

Author Comment

by:ZURINET
ID: 22848735
Hi Cvioj123

I put the join condition

Now I am getting
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22849835
can u past your new code again ..
0
 

Author Comment

by:ZURINET
ID: 22849905
set @sql_where =
 
'where (T00A.F2AnID='+ltrim(rtrim(str(@intAID)))+')
        and              
                (T01_ART.F3BID =
                                                                        (select F3BID  
                                                                         from   T01_ART
                                                                         where  (F2AnID='+ltrim(rtrim(str(@intAID)))+')
                                                                         and    F03_Art = T00A.F03_Art
                                                                         and    (T01_ART.F0_Preis = (SELECT Max(ISNULL(F0_Preis, 0))
                                                                                                                                FROM    T01_ART inner join T00A                                                                
  on T00A.F03_Art  = T01_ART.F03_Art                                                                                                                         where   (T01_ART.F2AnID='+ltrim(rtrim(str(@intAID)))+')
                                                                                                                                and     T01_ART.F03_Art = T00A.F03_Art
                                                                                                                                and     F010_Gue <= getdate() and F011_Gue >= getdate()
                                                                                                                                GROUP BY F03_Art
                                                                                                                                ))
                                                                        ))
       
       
        and (F010_Gue <= getdate() and F011_Gue >= getdate())'  
       
        set @sql_where = @sql_where+' where  (T00A.F027=0 or T00A.F029=3 or T00A.F027=7)'
0
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 500 total points
ID: 22850921
can u run this query and post results
just declare your @intAID and set value to it

declare @intAID int
set @intAID = yourValue

SELECT F3BID

FROM T01_ART

WHERE

	( F2AnID = ltrim(rtrim(str( @intAID )))	)

	And F03_Art = T00A.F03_Art

	And	(	T01_ART.F0_Preis =(

													SELECT Max ( ISNULL ( F0_Preis , 0 ) )

													FROM T01_ART

														INNER JOIN T00A

																ON T00A.F03_Art = T01_ART.F03_Art

													WHERE

														( T01_ART.F2AnID = ltrim(rtrim(str( @intAID ))))

														And T01_ART.F03_Art = T00A.F03_Art

														And F010_Gue <= getdate ( )

														And F011_Gue >= getdate ( )

														GROUP BY F03_Art

												)

)

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 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

9 Experts available now in Live!

Get 1:1 Help Now