Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ALTER FUNCTION [dbo].[FNGetDataRowCount](@DatasetID AS INT)  
RETURNS BIGINT  
AS  
  BEGIN
  	

  DECLARE @retval AS BIGINT
  DECLARE @temp TABLE (
 
                table_name sysname ,  
                row_count INT ,
                reserved_size VARCHAR(50),  
                data_size VARCHAR(50),  
                index_size VARCHAR(50),  
                unused_size VARCHAR(50)
                )  
                  
                DECLARE @datasetName varchar(100)  
                SET @datasetName = '['+Cast(@DatasetID AS varchar)+']'  
                  

INSERT @temp  
    EXEC sp_spaceused @datasetName  

 SELECT @retval=row_count from @temp  WHERE table_name=@DatasetID
 
 
 RETURN @retval
 
  END




above function giving error-
SQL
Msg 443, Level 16, State 14, Procedure FNGetDataRowCount, Line 22
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
Posted
Comments
Herman<T>.Instance 7-Sep-11 6:44am    
what is the purpose of INSERT @temp ?

you cannot call a stored procedure from function..
 
Share this answer
 
As pointed out here: Call a stored procedure in select query[^]:

The problem with that call is that there's no guarantee that the stored procedure won't change the state of the database if it's run. Because of this, the SQL server won't allow you to use a procedure inside a function.

As a workaround you should use for example SELECT statements instead of the procedures to insert data in the intermediate table.
 
Share this answer
 

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