November 18, 2010 at 10:16 pm
Hi,
I want to Assign cte query(dynamic) resut to variable
but I am getting error like (Must declare the scalar variable "@val").
my query is:
--------------
declare @val int
declare @tblname1 as varchar(50)
declare @tblname2 as varchar(50)
set @tblname1='tbl_00001'
set @tblname2='tblSt_00001'
exec('with cte1
as
(
select line,Status_text,Zerox_ID from '+@tblname1+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
)
select @val=(select count(*) from cte1)')
thanks in advance
November 18, 2010 at 10:38 pm
I have try already this
but its not working
November 18, 2010 at 10:57 pm
declare @val int
declare @tblname1 as varchar(50)
declare @tblname2 as varchar(50)
set @tblname1='tbl_00001'
set @tblname2='tblSt_00001'
exec(';with cte1
as
(
select line,Status_text,Zerox_ID from '+@tblname1+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
)
select ' + @val + 'count(*) from cte1'
)
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
November 18, 2010 at 11:00 pm
Use sp_executesql() instead of exec(),
using sp_executesql(), you can specify @val as output parameter...
Vishal Gajjar
http://SqlAndMe.com
November 18, 2010 at 11:04 pm
Hi
thank you.Its working fine.but I am trying to print that variable nothing
will be displayed
code
-----
declare @val int
declare @tblname1 as varchar(50)
declare @tblname2 as varchar(50)
set @tblname1='tbl_00001'
set @tblname2='tblSt_00001'
exec(';with cte1
as
(
select line,Status_text,Zerox_ID from '+@tblname1+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'
)
select ' + @val + 'count(*) from cte1'
)
print @val
November 18, 2010 at 11:11 pm
I am alredy try that method also
declare @val int
declare @sql varchar(max)
declare @tblname1 as varchar(50)
declare @tblname2 as varchar(50)
set @tblname1='tbl_00001'
set @tblname2='tblSt_00001'
select @sql=N'with cte1
as
(
select line,Status_text,Zerox_ID from '+@tblname1+' where allocateto is not null
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'close
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'hold
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'otherstatus
)
select @val=count(*) from cte1'
November 18, 2010 at 11:14 pm
remove "@val=" from your query in sp_executesql()
Vishal Gajjar
http://SqlAndMe.com
November 18, 2010 at 11:22 pm
Thank you.
I got correct query.
-------------------------
declare @vcTemp int
declare @sql nvarchar(max)
declare @tblname1 as varchar(50)
declare @tblname2 as varchar(50)
set @tblname1='tbl_00001'
set @tblname2='tblSt_00001'
SELECT @sql='select @vcTemp=count(*) from(
select line,Status_text,Zerox_ID from '+@tblname1+' where allocateto is not null
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'close
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'hold
except
select line,Status_txt,Zerox_file_ID from '+@tblname2+'otherstatus
)as a'
EXEC sp_executesql @sql, N'@vcTemp int output', @vcTemp output
print @vcTemp
This will work fine
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply