Question : T-SQL UDF to Obtain Table Row Count

The T-SQL UDF named 'udf_GetCountOfRows' (defined in the box below) fails to run when I invoke it with the following SELECT statement,

     SELECT dbo.udf_GetCountOfRows( 'ODS_AR_INVOICE_ITEMS' ) AS Row_Count

The SQL Server 2005 Query Engine gives the following error message:

     Msg 245, Level 16, State 1, Line 1
     Conversion failed when converting the nvarchar value 'SELECT ' to data type int.

Please identify the problem here and explain the solution.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
CREATE FUNCTION udf_GetCountOfRows
 ( 
  @sTN SYSNAME  -- Name of Table for which to retrieve Row Count
 )
 
RETURNS INT -- Row Count of the input table name
AS
BEGIN
 DECLARE @sSQL NVARCHAR(500) -- SQL String
 DECLARE @nRC INT -- Row Count
 
 SET @sSQL = N'SELECT ' + @nRC + '= COUNT(*) From [' + @sTN + ']'    
 EXEC sp_executesql @sSQL 
 
 RETURN @nRC
 
END 
GO

Answer : T-SQL UDF to Obtain Table Row Count

I'm trying to post my comment for three times but it does not appears it seems a Bug!!
Anyway up to my knowledge it is impossible to run Dynamic SQL from user defined function so that I create a stored procedure for performing this task hoping that hepls you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ayman A. Nour
-- Create date: 14/10/2009
-- Description:	
-- =============================================
CREATE PROCEDURE sp_DB_Tables_Info
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @Table_Type nvarchar(10), @Table_Name nvarchar(255), @Row_Count integer
	DECLARE @ResultTable TABLE(Table_Type nvarchar(10), Table_Name nvarchar(255), Rows_Count integer)
	DECLARE TABLES_LIST CURSOR FOR
	SELECT DISTINCT
		o.type 
		, o.name 
	FROM sys.objects o LEFT OUTER JOIN	sys.partitions p ON p.object_id = o.object_id 
	WHERE o.type IN ( 'U', 'V') 
	ORDER BY o.type, o.name
	
	DECLARE @tmpSQL nvarchar(1000)
	OPEN TABLES_LIST
	FETCH FROM TABLES_LIST INTO @Table_Type, @Table_Name
	WHILE(@@FETCH_STATUS <> -1)
	BEGIN
		SET @tmpSQL = N'SELECT @Row_Count = COUNT(*) FROM ' + @Table_Name
		BEGIN TRY
			EXEC sp_executesql @tmpSQL, N'@Row_Count integer output', @Row_Count=@Row_Count output
		END TRY
		BEGIN CATCH
			PRINT 'An Error occured during counting rows of ' + @Table_Name
		END CATCH
		INSERT INTO @ResultTable VALUES(@Table_Type, @Table_Name, @Row_Count)
		FETCH NEXT FROM TABLES_LIST INTO @Table_Type, @Table_Name
	END
	
	CLOSE TABLES_LIST
	DEALLOCATE TABLES_LIST
	SELECT * FROM @ResultTable
END
GO
 
-- To execute the stored 
EXEC sp_DB_Tables_Info
Random Solutions  
 
programming4us programming4us