简体   繁体   中英

SQL - create function that will take a table variable as an input

I'm having trouble writing a function that will take a table variable as an input and return the total number of rows in that table.

Here is my try:

CREATE FUNCTION fTableRows( @table TABLE )
RETURNS INT AS
BEGIN
    RETURN( SELECT COUNT(*) FROM @table )
END

If you do this in SQL server 2008 + you have use user defined data type - table. Good explanation can be found here: Pass table as parameter into SQL Udf

CREATE FUNCTION getTableRows
(
    @TableName VARCHAR(30)
)
RETURNS INT AS
BEGIN
    RETURN( SELECT COUNT(*) FROM @TableName)
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM