Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Must declare the variable
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Must declare the variable
Miscellaneous
Thread ID:
00942650
Message ID:
00942650
Views:
57
Can someone tell me how to get around this issue?
The Exec does not see my declare @NewId!

Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@NewId'.
if exists (select * from sysobjects where id = object_id('Ims_sy_GetNextPrevID') and sysstat & 0xf = 4)
	drop procedure Ims_sy_GetNextPrevID
go
Create Procedure Ims_sy_GetNextPrevID


@Table	varchar(50),
@ReqID	int

as

BEGIN 
	declare @NewId 	int,
		@SqlCmd varchar(300),
		@cReqID	varchar(10)

	set @cReqID = ltrim(rtrim(STR(@ReqID, 12, 0)))
	set @NewId = -1
	
	set @SqlCmd ='select top 1 @NewId=' + @Table+'id into #TempFL from '+@Table +' where '+@Table+'id > ' + @cReqID 

	exec (@SqlCmd)
	Print @NewId

	set @NewId = isnull(@NewId,0)

	if @NewId = 0
		begin
			select top 1 @NewId=repflid 
				from repfl 
				where repflid < @ReqID
				order by repflid desc
		end
	
	select @NewId
END

go

dbo.Ims_sy_GetNextPrevID 'Repfl',103
Next
Reply
Map
View

Click here to load this message in the networking platform