Re: Variable and Dynamic SQL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Aaron [SQL Server MVP] (ten.xoc_at_dnartreb.noraa)
Date: 07/21/04


Date: Wed, 21 Jul 2004 11:22:04 -0400

http://www.aspfaq.com/2492

-- 
http://www.aspfaq.com/
(Reverse address to reply.)
"David" <david.paskiet@t-mobile.com> wrote in message
news:9abbaed9.0407210721.8273445@posting.google.com...
> I need to set a variable value base on the return from an executed
> dynamically built sql statement (exec(sql).
>
> I know that by using the statement set @var1 = (Select count(id) from
> table) will produce a value in the variable.  So I thought that by
> using set @var1 = (exec(sql)) would do the same.  WRONG!  I get the
> 'must declare variable' error.  I have found out that the exec runs in
> a different space which explains the error - I think.  Can somebody
> help me answer the problem.  I have placed a very simplistic version
> of my code below.
>
> *********************************************
> declare
>   @iTot integer,
>   @sSQL varchar(1000)
> ;
>
> -- This was the first thought I had but the error popped up
> set @sSQL = 'select @iTot = count(id) from table where field1 <=
> getdate()and year(field1) =  year(getdate()) '
> /*There will be a where clause built here ans added to the above*/
> execute (@sSQL)
>
> -- This was the second thought I had but again DNW
> set @sSQL = 'select count(id) from table where field1 <= getdate()and
> year(field1) =  year(getdate()) '
>
> set @iTot = (exec(@ssql))
>
> *******************************************
>
> All other attempts failed as well.  Please email me as well as post an
> answer.  This is a show stopper for me at this moment and very high
> priority on my list!!!
>
> Thanks.
> David
> 973-292-8956


Relevant Pages

  • Variable and Dynamic SQL
    ... dynamically built sql statement. ... execute ...
    (microsoft.public.sqlserver.server)
  • RE: Dialog Arguements
    ... Built-In Dialog Box Argument Lists ... "David" wrote: ... I need to execute a web query but the user must input username & password ... I thought maybe I could solve this by changing the RefreshBackground ...
    (microsoft.public.excel.programming)
  • Re: Install a Service Programmatically ?
    ... > I surpose you already has added an installer component to your project ?? ... >> various incoming requests. ... >> option to allow server.exe to execute and handle requests even after the ... >> David Sworder ...
    (microsoft.public.dotnet.framework)
  • Re: CSWS 2.0 - newbie help needed again...
    ... David J Dachtera wrote: ... if you use only EXECUTE ... Unofficial OpenVMS Hobbyist Support Page: ...
    (comp.os.vms)
  • RE: Dialog Arguements
    ... "David" wrote: ... I need to execute a web query but the user must input username & password ... I thought maybe I could solve this by changing the RefreshBackground ...
    (microsoft.public.excel.programming)