<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.2800.1226" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=204162410-11092003><FONT face=Arial color=#0000ff size=2>Maybe
it would help if you would explain more about why you are trying to do
this. How is "@qs" declared and what are you trying to do with
it?</FONT></SPAN></DIV>
<DIV><SPAN class=204162410-11092003><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=204162410-11092003>
<DIV align=left><FONT face=Arial size=2>Doris Manning</FONT></DIV>
<DIV align=left><FONT face=Arial size=2>Database Administrator</FONT></DIV>
<DIV align=left><FONT face=Arial size=2>Hargrove Inc.</FONT></DIV>
<DIV align=left><FONT face=Arial size=2><A
href="http://www.hargroveinc.com/">www.hargroveinc.com</A></FONT></DIV></SPAN></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left><FONT
face=Tahoma size=2>-----Original Message-----<BR><B>From:</B>
dba-sqlserver-bounces@databaseadvisors.com
[mailto:dba-sqlserver-bounces@databaseadvisors.com] <B>On Behalf Of </B>David
Emerson<BR><B>Sent:</B> Wednesday, September 10, 2003 9:19 PM<BR><B>To:</B>
dba-SQLServer@databaseadvisors.com<BR><B>Subject:</B> [dba-SQLServer]Sproc -
Selecting from select statement<BR><BR></FONT></DIV>I am trying to use a
select statement as the source of another select statement in a
Sproc.<BR><BR>The following statement works -<BR><BR><FONT face=Tahoma>SELECT
GateIDNo, MAX(MDQ) AS MaxMDQ, MAX(MDQDate) AS
OldMDQDate<BR><X-TAB> </X-TAB>FROM
dbo.tblGateStationMDQ<BR><X-TAB> </X-TAB>WHERE
MDQ <> 0
<BR><X-TAB> </X-TAB>GROUP BY
GateIDNo<BR><X-TAB> </X-TAB>HAVING
(MAX(MDQDate) < '20031001')<BR><BR>But when I do the following
-<BR><BR>SELECT @qs =<X-TAB> </X-TAB>'SELECT GateIDNo,
MaxMDQ,
OldMDQDate<BR><X-TAB> </X-TAB>FROM
(SELECT GateIDNo, MAX(MDQ) AS MaxMDQ, MAX(MDQDate) AS
OldMDQDate<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>FROM
dbo.tblGateStationMDQ<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>WHERE
MDQ <> 0
<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>GROUP
BY
GateIDNo<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>HAVING
(MAX(MDQDate) < ''20031001''))'<BR><BR></FONT>I get the following error
-<BR><BR>Server: Msg 170, Level 15, State 1, Line 6<BR>Line 6: Incorrect
syntax near ')'.<BR><BR>Using Print (@qs) I get -<BR><BR>SELECT GateIDNo,
<FONT face=Tahoma>Max</FONT>MDQ,
OldMDQDate<BR><X-TAB> </X-TAB>FROM
(SELECT GateIDNo, MAX(MDQ) AS <FONT face=Tahoma>Max</FONT>MDQ, MAX(MDQDate) as
OldMDQDate<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>FROM
dbo.tblGateStationMDQ<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>WHERE
MDQ <> 0
<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>GROUP
BY
GateIDNo<BR><X-TAB> </X-TAB><X-TAB> </X-TAB><X-TAB> </X-TAB>HAVING
(MAX(MDQDate) < '20031001'))<BR><BR>Any suggestions?<BR><X-SIGSEP>
<P></X-SIGSEP>Regards<BR><BR>David Emerson<BR>DALYN Software Ltd<BR>25b
Cunliffe St, Johnsonville<BR>Wellington, New Zealand<BR>Ph/Fax (877) 456-1205
</P></BLOCKQUOTE></BODY></HTML>