Results 1 to 7 of 7

Thread: What's wrong in this code?

  1. #1

    Thread Starter
    Hyperactive Member jeba's Avatar
    Join Date
    Feb 2000
    Posts
    265

    What's wrong in this code?

    Hi there!
    I am getting the following error when I run the following SP.

    Server: Msg 137, Level 15, State 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@maxid'.


    Code:
    CREATE  Procedure spFindMax @tablename varchar(50), @pykey varchar(50)  As 
    Declare @SqlStr varchar(1000) , @maxid int
    set @SqlStr ='Select @maxid=ISNULL(max('+ @pykey  + '),0) From ' + @tablename
    Exec (@SqlStr)
    print @maxid
    Please help me. Thanx in advance.
    J£ßä

  2. #2
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: What's wrong in this code?

    Dear Jeba,

    Well, I have tested ur proc. I have removed the @maxid from the
    code and executed it. It works fine.

    CREATE Procedure spFindMax @tablename varchar(50), @pykey varchar(50) As
    Declare @SqlStr varchar(1000)
    set @SqlStr ='Select ISNULL(max('+ @pykey + '),0) From ' + @tablename
    Exec (@SqlStr)
    Last edited by Venkrishna; Jun 18th, 2001 at 07:10 AM.
    there r no alternatives 4 hardwork.

  3. #3

    Thread Starter
    Hyperactive Member jeba's Avatar
    Join Date
    Feb 2000
    Posts
    265
    Dear Venkrishna!
    well, thanks for your reply. I wanted to use @maxid as the OUTPUT parameter, so that I can eliminate creation of a recordset.
    J£ßä

  4. #4
    Addicted Member
    Join Date
    May 2001
    Posts
    153
    Ok Jeba,

    I understand.
    there r no alternatives 4 hardwork.

  5. #5

    Thread Starter
    Hyperactive Member jeba's Avatar
    Join Date
    Feb 2000
    Posts
    265
    Hi VB_DBA!
    Thanks a lot for the reply. Sorry for disturbing you again...
    Instead of printing, can you please return the @maxid as the OUTPUT parameter? I've tried to assign the value of @maxid to another variable, but it is giving an error:

    Couldn't assign a varchar value to int datatype.

    Please help.
    J£ßä

  6. #6
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Do you have to have it returned as an output parameter? You could do the following and it would return the MaxID as a recordset:

    CREATE Procedure spFindMax @tablename varchar(50),
    @pykey varchar(50) As

    Declare @SqlStr varchar(1000)
    Set @SqlStr = ''

    set @SqlStr = @SqlStr + 'Select ISNULL(max(' + @pykey + '),0) '
    set @SqlStr = @SqlStr + 'From ' + @tablename

    Exec (@SqlStr)

    Other than doing it this way, I don't believe you are going to be able to use an Output parameter with this type of stored proc.

    Chris

  7. #7

    Thread Starter
    Hyperactive Member jeba's Avatar
    Join Date
    Feb 2000
    Posts
    265
    Hi Chris!
    It would be better(..the best) if I could return it as Output parameter, because I have to call this Stored procedure in many places. So I feel opening a recordset for each instance is a bit costly. Why can't I assign the @maxid variable to the output parameter?

    Pls help.
    J£ßä

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