Apparantly you're still not out of the woods yet. So I'll leave you with an example.
Create the TABLE
type:
CREATE TYPE dbo.document_ids AS TABLE (
document_id BIGINT
);
GO
If you can guarantee that only distinct document_id
values will be passed as a parameter, you can benefit from defining a primary key on the document_id
field:
CREATE TYPE dbo.document_ids AS TABLE (
document_id BIGINT PRIMARY KEY
);
GO
Create functions dependent on that TABLE
type, one scalar-valued and one table-valued:
CREATE FUNCTION dbo.fn_doc_sv (
@doc_ids dbo.document_ids READONLY
)
RETURNS BIGINT
AS
BEGIN
RETURN (SELECT SUM(document_id) FROM @doc_ids);
END
GO
CREATE FUNCTION dbo.fn_doc_tv (
@doc_ids dbo.document_ids READONLY
)
RETURNS TABLE
AS
RETURN (SELECT document_id FROM @doc_ids);
GO
Usage of both functions:
DECLARE @doc_ids dbo.document_ids;
INSERT INTO @doc_ids(document_id)VALUES(2949146),(2949148),(2949149),(14016926),(14025278),(14016928),(14016928),(14025280),(14025280);
SELECT dbo.fn_doc_sv(@doc_ids);
SELECT * FROM dbo.fn_doc_tv(@doc_ids);
Hope that makes it clear for you.