我有这个用户定义的功能:
CREATE FUNCTION [dbo].[COUNT_ROWS_TABLE]()
RETURNS TINYINT
AS
BEGIN
DECLARE @ROW_COUNT TINYINT
SELECT @ROW_COUNT = COUNT(*) FROM EMPLOYEE
RETURN @ROW_COUNT
END
GO
问题是它只适用于表[dbo].[EMPLOYEE]
,我不想为数据库中的每个表复制粘贴此函数。
到目前为止我的尝试:
CREATE FUNCTION [dbo].[COUNT_ROWS_TABLE](@TABLE_NAME VARCHAR(50))
RETURNS TINYINT
AS
BEGIN
DECLARE @SQL_COMMAND NVARCHAR(100)
DECLARE @PARAM NVARCHAR(50)
DECLARE @ROW_COUNT TINYINT
SET @SQL_COMMAND = N'SELECT @RESULT = COUNT(*) FROM ' + @TABLE_NAME
SET @PARAM = N'@RESULT TINYINT OUTPUT'
EXEC SP_EXECUTESQL @SQL_COMMAND, @PARAM, @RESULT = @ROW_COUNT OUTPUT
RETURN @ROW_COUNT
END
GO
该代码不起作用,因为它不允许函数内的那些语句。
但它可以在存储过程中运行,但前提是我PRINT
变量而不是RETURN
。
我需要它成为一个函数,因为我需要在IF
语句中调用它。
有关如何实现这一目标的任何想法?谢谢。
答案 0 :(得分:4)
您可以使用如下
ALTER FUNCTION [dbo].[COUNT_ROWS_TABLE](@TABLE_NAME VARCHAR(50))
RETURNS TINYINT
AS
BEGIN
DECLARE @ROW_COUNT TINYINT
SELECT @ROW_COUNT = SUM(b.rows)
FROM SYS.TABLES a INNER JOIN sys.partitions b ON b.OBJECT_ID = a.OBJECT_ID
WHERE a.is_ms_shipped = 0 AND b.index_id IN (1,0)
and a.name=@TABLE_NAME
GROUP BY a.name
RETURN @ROW_COUNT
END
GO
答案 1 :(得分:0)
对于这种将对象名称作为参数传递的Query,您需要使用Dynamic Sql,但由于您只返回总行数,因此可以使用Dynamic sql使用sys schema cataloge视图来实现,您不能在UDF定义中使用Dynamic Sql。
这是你需要做的......
CREATE PROCEDURE [dbo].[COUNT_ROWS_TABLE]
@TABLE_NAME SYSNAME,
@Count INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(MAX);
SET @Sql = N'SELECT @Count = COUNT(*) FROM ' + QUOTENAME(@TABLE_NAME)
EXECUTE sp_executesql @Sql
, N'@Count INT OUTPUT'
, @Count OUTPUT
END
GO
CREATE FUNCTION [dbo].[COUNT_ROWS_TABLE]
(
@TableName SYSNAME
)
RETURNS INT
AS
BEGIN
DECLARE @Row_Count INT;
SELECT @Row_Count = i.rowcnt
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
AND o.NAME = @TableName
RETURN @Row_Count;
END