Re: dynamic sql question

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Tim S (stahta01_at_juno.com)
Date: 11/12/04


Date: 12 Nov 2004 10:12:25 -0800

My best guess below

Tim S

EXECUTE sp_executesql @stmt = N'SELECT @rOUT = ' + QUOTENAME(@colName)
+ ' FROM tblName WHERE eid=@AID', @params = N'@rOUT VARCHAR(100)
OUTPUT, @AID VARCHAR(10)', @rOUT=@data OUTPUT, @AID=@empID

"exBK" <exBK@discussions.microsoft.com> wrote in message news:<0094DCB1-BC08-4664-8A23-1BB263D3EE12@microsoft.com>...
> I have a cursor where I loop through a set of employee_ids.
> While inside the cursor, I have a second set of cursor, which gets the
> column_name from information "information_schema.columns" for a given table.
> The problem I have run into in the following code is:
>
> EXECUTE sp_executesql
> @stmt = N'SELECT @rOUT = [@cName] FROM tblName WHERE eid=@AID',
> @params = N'@rOUT VARCHAR(100) OUTPUT, @cName VARCHAR(20), @AID VARCHAR(10)',
> @rOUT=@data OUTPUT, @cName=@colName, @AID=@empID
>
> I get the error Invalid column name '@cName'
>
> the @colName is coming from the second cursor, which I am not sure how to
> use it dynamically. i.e., I can't use it in a "SELECT" statement to get a
> value. Any tips are greatly appreciated. TIA.



Relevant Pages

  • Re: dynamic sql question
    ... > I have a cursor where I loop through a set of employee_ids. ... > While inside the cursor, I have a second set of cursor, which gets the ... You can't use variables as object names in a SQL query; ...
    (microsoft.public.sqlserver.programming)
  • dynamic sql question
    ... I have a cursor where I loop through a set of employee_ids. ... While inside the cursor, I have a second set of cursor, which gets the ... TIA. ...
    (microsoft.public.sqlserver.programming)
  • RE: Oracle cursor help
    ... rids dbms_utility.uncl_array; ... where <your where clause> ... Subject: Re: Oracle cursor help ... exit the loop: */ ...
    (perl.dbi.users)
  • Command object stops prematurely without error?
    ... However, when run via the ADO Command object, the outer loop Applications ... Declare cApps CURSOR for Select ApplicationID from UCM_Applications ...
    (microsoft.public.data.ado)
  • Re: Calling a SP inside a cursor loop..
    ... every loop iteration of the cursor. ... Fetch next From EffectiveDate_Cursor Into @FLD1,@FLD2 ... and If the Fetch stmt is below the begin Stmt, the loop iterations are ... the proper way to program a cursor loop is: ...
    (comp.databases.ms-sqlserver)