I need to find a way to give multiple values to one parameter.
An example to explain what I want to achieve:
CREATE TABLE Students
(
code int,
name varchar(20)
);
INSERT INTO Students VALUES (1,'abc');
INSERT INTO Students VALUES (2,'def');
INSERT INTO Students VALUES (3,'ghi');
INSERT INTO Students VALUES (4,'jkl');
SELECT * FROM students;
CREATE FUNCTION dbo.GetCount (@name VARCHAR(8000))
RETURNS INT
AS
BEGIN
DECLARE @TheResultCount INT;
SELECT @TheResultCount = COUNT(*)
FROM Students
WHERE name IN (@name);
RETURN @TheResultCount;
END;
SELECT dbo.GetCount('abc,def,ghi,jkl');
How do I achieve this requirement?
2条答案
按热度按时间hpxqektj1#
Passing multiple values to a function really should use a table-valued parameter.
As an example you would have a suitably named table type defined:
And your function would use it like so:
And you would use the function like so:
If however you really must pass a comma-delimited string, your function would look like:
qojgxg4l2#
you can use
string_split
if you're usingSQL Server 2016
and later.for older versions of
SQL Server
you can do this :