Incorrect syntax near the keyword where subquery

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

ZURINETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cvijo123Commented:
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
ZURINETAuthor Commented:
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
Cvijo123Commented:
can u past your new code again ..
0
ZURINETAuthor Commented:
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
Cvijo123Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.