如何编写sql server 2008函数
我需要用一个参数编写函数并返回两列。我试过下面的代码如何编写sql server 2008函数,sql,sql-server,Sql,Sql Server,我需要用一个参数编写函数并返回两列。我试过下面的代码 create function fun_initial_score (@phy_id varchar(20)) returns table as declare @vc int; set @vc =(select MAX(visited_count) from tbl_all_purple_flag_level ) return (SELECT pflag.Score, pflag.Disability_Le
create function fun_initial_score (@phy_id varchar(20))
returns table
as
declare @vc int;
set @vc =(select MAX(visited_count) from tbl_all_purple_flag_level )
return (SELECT
pflag.Score,
pflag.Disability_Level
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id=@phy_id
and pflag.visited_count=@vc)
我知道上面的代码显示错误,但我需要用上面的概念写函数。如何编写。有人能帮我吗?…谢谢…如果您正在尝试编写,那么整个函数体只能是提供给返回的单个select语句。但谢天谢地,这在这里似乎是可行的。我看不出单独的查询和变量保存@vc的原因:
@初始分数是一个标量变量。它只能包含一个值。你想让函数返回一个由两列组成的结果集吗?我编辑了我的代码,看到了。谢谢,我得到了它…但我需要从两个函数中进行选择我尝试了这个选择*从dbo.fun_初始_分数'01091400003',dbo.fun_当前_分数'01091400003'@user3170098-不需要调用函数两次,但应该为它生成的结果集提供别名:select*from dbo.fun\u initial\u score'01091400003'alias\u name
create function fun_initial_score (@phy_id varchar(20))
returns table
as
return (SELECT
pflag.Score,
pflag.Disability_Level
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id=@phy_id
and pflag.visited_count=(select MAX(visited_count) from tbl_all_purple_flag_level ))