CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2001
    Posts
    3

    SQL Stored Procedure Problem

    Hi
    I have a question regarding SLQ variables. I am setting the variable through a cursor. When I check the syntax of this SQL, it says that I need to declare @TableName on the line that says
    select @eid = eid from @tableName . Is it not possible to do this? I know that I've used such variable in a where clause. Is it because I am using it in a from clause? Do I need some other kind of syntax to do this?

    declare @elementClassId int,
    @eid eid,
    @className varchar(32),
    @tableName varchar(32),
    @aid eid,
    @attributeName varchar(32)

    declare classCursor cursor for
    select classId, TableName from tmbElementClasses where classAttributes = 3
    order by TableName

    open classCursor
    fetch classCursor into @elementClassId, @tableName
    while (@@fetch_status = 0)
    BEGIN

    if( @elementClassId is not NULL and @tableName is not NULL )
    begin
    select @eid = eid from @tableName
    end
    fetch classCursor into @elementClassId, @tableName

    END
    close classCursor
    deallocate classCursor

    Thanks in advance




  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: SQL Stored Procedure Problem

    It is not possible to substitute a variable for a table or view name in a select statement, sorry.

    The best workaround is to have:

    If @tablename = TCLMDETS
    Select @eid = eid FROM TCLMDETS
    Else
    Select @eid = eid FROM TCLMTRXN

    or as many IFs as you have tables.

    HTH,
    D

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured