April 7, 2006 at 5:51 am
im executing this as dynamic sql
Select
@SQLCommand ='Select count(*) from '+ ltrim(rtrim(@TableName))
i want to select this returned number into a variable so i tried this
Select
@SQLCommand ='select @NumRows = (Select count(*) from '+ ltrim(rtrim(@TableName)) +')'
it tells me i have to declare the variable @NumRows, even though i have declared it at the top of the procedure. i just get an error when i try this
select @NumRows = (exec @SQLCommand)
how do i do this??
April 7, 2006 at 6:17 am
Actually the EXEC command is executed in a diff address space of the current so... your var/s are not visible.
What you have to do is to declare the var inside the EXEC command:
declare @TableName varchar(256)
declare @SQLCommand varchar(256)
select @TableName='items '
select @SQLCommand = 'Declare @NumRows int
select @NumRows = count(*) from '+ ltrim(rtrim(@TableName))+
' select @NumRows'
exec(@SQLCommand)
------------
When you 've got a hammer, everything starts to look like a nail...
April 7, 2006 at 6:54 am
ive tried that with this code
declare
@tableName varchar (50)
Declare
@command varchar(50)
select
@tableName = 'customer'
select
@command = 'Declare @NumRows int
select @numRows = (select count(*) from '
+ ltrim(rtrim(@tableName)) + ') select @NumRows'
exec
(@command)
but i keep getting an error saying incorrect syntax near select. i cannot see any incorrect syntax
April 7, 2006 at 7:17 am
The problem is not actually in your code but in the size of the @Command which is too short to keep the whole string.... try
Declare @command varchar(256)
...and everything will work...
------------
When you 've got a hammer, everything starts to look like a nail...
April 7, 2006 at 8:21 am
one more thing ive just realised. ok, i can get the number of rows in each of those tables, but i when i try to use that number when inserting into a table, i simply get a null. the @numRows variable doesnt seem to have any value outside of the dynamic sql statement. i need to get the number of rows in a table into the variable, then further down , insert the variable into a table, like so
set
@SQLCommand ='Declare @NumRows int
select @NumRows = count(*) from '
+ ltrim(rtrim(@TableName))
--+ ' select @NumRows'
exec
( @SQLCommand)
insert
into temp
values
(@tableName,@numRows)
this is the result set i get
Table Name RowCount
tblCurrentQuarter NULL
BreakdownDeductionsOnceOFF NULL
tblCustBreakdown NULL
BreakdownDeductionsOnceOFFq2 NULL
April 7, 2006 at 8:27 am
Seems that you dont read my replies....
------------
When you 've got a hammer, everything starts to look like a nail...
April 7, 2006 at 8:34 am
i did i did!! i am getting a result back for @numRows, but thats in the context of the dynamic sql, and i cant reference that outside of the dynamic sql, in my sproc.
April 7, 2006 at 8:38 am
Try sp_executesql
This question gets asked at least once a month usually more, I'm surprised you couldn't do a simple search to find the answer
Use Pubs
declare @SQLCommand nvarchar(4000)
declare @TableName varchar(50)
declare @myoutputRowcount int
set @TableName = 'Authors'
Select @SQLCommand ='Select @NumRows = count(*) from '+ ltrim(rtrim(@TableName))
exec sp_ExecuteSql @SQLCommand, N'@NumRows int OUTPUT', @NumRows = @myoutputRowcount OUTPUT
select @myoutputRowcount
April 7, 2006 at 9:26 am
thank you. that worked a treat. i would have searched, but i didnt know what to search for.
April 25, 2006 at 1:21 am
sp_executesql is what u r looking 4...
Actually (by reading the SBO) u will see the following:
"Using sp_executesql is recommended over using the EXECUTE statement to execute a string. Not only does the support for parameter substitution make sp_executesql more versatile than EXECUTE, it also makes sp_executesql more efficient because it generates execution plans that are more likely to be reused by SQL Server."
------------
When you 've got a hammer, everything starts to look like a nail...
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply