看看这个创建函数的语句有什么错误?

huazaiboy 2003-10-20 03:26:10
1。什么时候需要用到返回一个table型数据,具体语法上需要注意些什么?
2。以下创建函数的语句有什么错误?
use mytest
create function salesbystore
returns table
as
return (select id,name from dbo.person)
...全文
58 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
ok,多谢!
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
我也不解,查帮助也没查到,记住就行了。
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
还是不解,begin 。。。。 end 应该是如有只有一条语句用也无妨,多条语句必须用的,为何这里确会报错呢?本人水平非常菜,望见谅
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
1:是不要加begin & end
2:此处的返回表跟存储过程返回一个Select 语句是一样的。
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
to to txlicenhe(马可)
为什么你上面写的带begin 与end 的语句也报错?
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
use mytest
go
if object_id('salesbystore6') is not null
drop function salesbystore6
go
create function salesbystore6()
returns table
as
begin
return (select id,name from dbo.person)
end
go

上面的语句报错?:
Server: Msg 170, Level 15, State 31, Procedure salesbystore6, Line 6
Line 6: Incorrect syntax near 'BEGIN'.
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
to txlicenhe(马可)
非常感谢您,有耐心的回答了我这么多幼稚的问题,再次感谢你,能谈谈什么时候需要使用到这种函数吗?返回值为一个表感觉很诧异
wzh1215 2003-10-20
  • 打赏
  • 举报
回复
SQL SERVER 7.0
运行出错:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'function'.
Server: Msg 178, Level 15, State 1, Line 4
A RETURN statement with a return status can only be used in a stored procedure.

晕。。。。
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
还有就是,明明有些函数因为没有参数,调用的时候不用写函数名后边的括号。为什么这里确必须要???非常的不解,比如: select Current_User等
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
use mytest
go
if object_id('salesbystore6') is not null
drop function salesbystore6
go
create function salesbystore6()
returns table
as
return (select id,name from dbo.person)
go

txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
use mytest
go
if object_id('salesbystore6') is not null
drop function salesbystore6
go
create function salesbystore6()
returns table
as
begin
return (select id,name from dbo.person)
end
go
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
ok了,麻烦各位大虾再看看我加了begin与end 为什么又报错了?
use mytest
go
create function salesbystore6()
returns table
as
begin
return (select id,name from dbo.person)
end
go


报错信息如下:
Server: Msg 2714, Level 16, State 5, Procedure salesbystore7, Line 6
There is already an object named 'salesbystore7' in the database.
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
全部代码如下:
use mytest
go
create function salesbystore ()
returns table
as
return (select id,name from dbo.person)

yujohny 2003-10-20
  • 打赏
  • 举报
回复
use mytest
go
--这里要加go
--这里是不是有其它代码????

create function salesbystore()
returns table
as
return (select id,name from dbo.person)
go
sdhdy 2003-10-20
  • 打赏
  • 举报
回复
先执行以下
use mytest
go
然后再执行创建函数语句
sdhdy 2003-10-20
  • 打赏
  • 举报
回复
楼上的
huazaiboy 2003-10-20
  • 打赏
  • 举报
回复
还是不行啊,以下你提示的错误信息:

Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'go'.
Server: Msg 111, Level 15, State 1, Line 3
'CREATE FUNCTION' must be the first statement in a query batch.
Server: Msg 178, Level 15, State 1, Line 6
A RETURN statement with a return value cannot be used in this context.

郁闷啊!这么几条语句调试我半天。
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
use mytest
go -- 此处加go
create function salesbystore () -- 加括号
returns table
as
return (select id,name from dbo.person)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧