您可以通过使默认值等于 NULL 来实现 T-SQL 存储过程中的可选参数。
将 NULL 用作“忽略此参数”的示例:
CREATE PROCEDURE [dbo].[dcspFilterEmpList]
@ProductName nvarchar(200) = null,
@ProductGroupID int = null
AS BEGIN
SELECT
prod_id AS 'ID',
prod_name AS 'Name'
FROM dbo.Products
WHERE (prod_group_id = @ProductGroupID OR @ProductGroupID IS NULL)
AND (prod_name = @ProductName OR @ProductName IS NULL)
END
It's perfectly fine to define a stored procedure to take a table-valued parameter in T-SQL.
Here is an article on the subject http://sqlwithmanoj.wordpress.com/2012/09/10/passing-multipledynamic-values-to-stored-procedures-functions-part4-by-using-tvp/
If you need more info on this google for "table-valued parameter"
Example using multi-valued parameters:
CREATE TYPE XTV_ProductNames AS TABLE --extension, table valued == XTV
( ProductName nvarchar(50))
GO
CREATE TYPE XTV_ProductGroups AS TABLE --extension, table valued == XTV
( ProductGroupID int))
GO
CREATE PROCEDURE [dbo].[dcspFilterEmpList]
@TVP1 XTV_ProductNames READONLY
,@TVP2 XTV_ProductGroups READONLY
AS BEGIN
SELECT
prod_id AS 'ID',
prod_name AS 'Name'
FROM dbo.Products as p
INNER JOIN @TVP1 as s
ON p.prod_name = s.ProductName
UNION
SELECT
prod_id AS 'ID',
prod_name AS 'Name'
FROM dbo.Products as p
INNER JOIN @TVP2 as s
ON p.prod_group_id = s.ProductGroupID
END