Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a dataset containing query like this.
SQL
if @Type = 2
begin
	exec Sp_Test1@cid
;
with result as(select * from Table1 where id=@aid)
select id from result;

end

go

exec Sp_Test2@aid

select * from Table1 where id= @aid


For some reasons I have to call the Sp_Test2 only inside that go.
It shows an error that @aid is not declared.

But @aid is a parameter of my SSRS report and is accessible inside that if condition.
Please help me.
Thanks
Posted
Comments
PIEBALDconsult 19-Aug-15 14:43pm    
Why would you use GO anyway?

You can't use variables across GO: https://msdn.microsoft.com/en-us/library/ms188037.aspx[^] - it effectively starts a new SQL "session".

"The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command."
 
Share this answer
 
You could enable SQLCMD mode:
SQL
:setvar myvar 10
declare @aid int = $(myvar);
select @aid
go
declare @aid int = $(myvar);
select @aid

Or simply:
SQL
:setvar myvar 10
select $(myvar)
go
select $(myvar)


This is an other approach:
SQL
declare @aid int = 10
create table #vars(aid int)
insert into #vars values(@aid)

select @aid

go
declare @aid int
select @aid = aid from #vars
select @aid


[Update] SQLCMD mode is not available in ReportingServices
 
Share this answer
 
v2
Comments
Baiju christadima 19-Aug-15 14:49pm    
I have done this. But an error showing #vars unavilable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900