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.
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)