我尝试在此处执行的操作是使用GetValue3
执行@ID
并获取列出@fullname,@ phonenumber,@ email,@city的@results
。
我遇到了2个错误,而且我一直试图弄清楚如何解决这两个问题。
错误#1
函数中包含的Select语句无法将数据返回给客户端
错误#2
为变量赋值的select语句不能与数据检索结合使用
我该怎么办?
Create Function GetValue3 (@ID INT)
returns nvarchar(350)
as
begin
declare @results nvarchar(350)
declare @fullname nvarchar(75)
declare @phonenumber nvarchar(100)
declare @email nvarchar(50)
declare @city nvarchar(20)
select -- this is where I get the first error.
@fullname = p.Full_Name,
@phonenumber = c.Phone,
@email = c.Email,
@city = c.CityID,
@results = @fullname,
@phonenumber, @email, @city -- this is where I get the 2nd error.
from
d_People p, d_Contacts c
where
c.PeopleID = p.PeopleID
and p.PeopleID = @ID;
return @results
end
答案 0 :(得分:1)
尝试使用以下代码:
Create Function GetValue3 (@ID INT)
returns nvarchar(350)
as
begin
declare @results nvarchar(350)
declare @fullname nvarchar(75)
declare @phonenumber nvarchar(100)
declare @email nvarchar(50)
declare @city nvarchar(20)
select
@fullname = p.Full_Name,
@phonenumber = c.Phone,
@email = c.Email,
@city = c.CityID,
@results = @fullname +','+ @phonenumber +','+ @email +','+ @city
from d_People p, d_Contacts c
where c.PeopleID=p.PeopleID and p.PeopleID=@ID;
return @results
end
答案 1 :(得分:0)
您可以将代码更改为以下内容,我认为它会为您提供所需的内容:
create function GetValue3 (@ID int)
returns nvarchar(350)
as
begin
declare @result nvarchar(350)
select
@result = p.Full_Name + ',' + c.Phone + ',' + c.Email + ',' + c.CityID
from d_People p
, d_Contacts c
where c.PeopleID = p.PeopleID
and p.PeopleID = @ID;
return @result
end
您收到错误的原因是因为在SELECT
声明中您可以 为变量或返回数据分配值,您可以& #39; t将两者混合在一起。
此外,@fullName
和@phoneNumber
等的值不是单独设置在SELECT
的顶部,然后"向下移动"以便在@results = @fullname, @phonenumber, @email, @city
可见。
答案 2 :(得分:0)
Create Function GetValue3 (@ID INT)
returns table
as
return select p.Full_Name, c.Phone, c.Email, c.CityID
from d_People p, d_Contacts c
where c.PeopleID=p.PeopleID and p.PeopleID=@ID;