我有一个执行的脚本,但是我需要它来接受用户输入,而不是为@lastName
设置变量。
CREATE FUNCTION displayCount() RETURNS INT AS BEGIN DECLARE @number INT, @lastName VARCHAR(40); SET @lastName = 'Ferrier'; SELECT @number = COUNT(*) FROM Person.Person WHERE LastName = @lastName; RETURN @number; END GO SELECT dbo.displayCount() AS howMany
我试图弄乱我认为可行的方法,但没有成功。我遇到错误
“过程或函数dbo.diplayCount的参数过多”
我确定这很简单,而且由于我太新了,所以我看不到它。谢谢您的帮助!
CREATE FUNCTION displayCount(@lastName VARCHAR(40)) RETURNS INT AS BEGIN DECLARE @number INT; SELECT @number = COUNT(*) FROM Person.Person WHERE LastName = @lastName; RETURN @number; END GO SELECT dbo.displayCount('Ferrier') as howMany
答案 0 :(得分:1)
您的函数看起来很好,但是我想您需要使用ALTER
而不是CREATE
,因为该函数已存在于数据库中。
您需要修改而不是创建。
ALTER FUNCTION displayCount(@lastName VARCHAR(40)) RETURNS INT
AS
BEGIN
DECLARE @number INT;
SELECT @number = COUNT(*) FROM Person.Person
WHERE LastName = @lastName;
RETURN @number;
END
GO
SELECT dbo.displayCount('Ferrier') as howMany
答案 1 :(得分:0)
在SSMS
中,展开[您的数据库]>可编程文件夹> Functions文件夹>表值函数>右键单击>新建内联表值函数。...
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: -- Create date: -- Description: -- ============================================= CREATE FUNCTION displayCount ( @lastName varchar(40) ) RETURNS TABLE AS RETURN ( SELECT COUNT(*) AS howMany FROM Person.Person WHERE LastName = @lastName ) GO
然后通过
进行调用 SELECT howMany FROM dbo.displayCount('Ferrier')