views:

24

answers:

2

Hi all,

Is it possible to hide a dynamic query from the result sets provided from a Stored Procedure?

I am using the @@rowcount of the dynamic query to set a variable that is used to determine whether another query runs or not.

The other query is used by code that I cannot change - hence why I am changing the Stored Procedure. The dynamic query returns as the first result set from the Stored Procedure is now the result of the dynamic query which currently is "breaking" the calling code.

Thanks in advance

+1  A: 

I have managed to solve this by inserting the result of the dynamic query into a temporary table and then retrieving the rowcount from the temporary table.

-- Create query
declare @query nvarchar(max)
set @query = 'select ' + @entityname + 'id from ' + @entityname + ' where ' + @entityname + 'id = ' + cast(@entityid as nvarchar(100))

-- Insert into to temp table - no new result set displayed!
declare @tbl table (EntityID int not null primary key)
insert into @tbl
    exec (@query)

-- Retrieve variable from temporary table
declare @count int
select @count = count(*) from @tbl

Above is the code I ended up using.

tgandrews
A: 

Try wrapping the dynamic query like this:

Set @query = 'SELECT COUNT(*) FROM (' + @Query + ') t'
Joel Coehoorn
The issue was because running exec on the dynamic query was appearing as the first result set of the Stored Procedure.
tgandrews