日期:2014-05-18  浏览次数:20402 次

请问这sql哪里有错?在线等,谢谢
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
alter FUNCTION [dbo].[GetChildId](@ClassId int)
RETURNS table 
AS
--定义变量@LastNode是否是最后一个节点
declare @LastNode int 提示这里有错
--定义游标,
Declare table_sursor Cursor for Select LastNode from Class where ParentId =@ClassId
--打开游标
open table_sursor
--提取第一行记录,赋值给变量@LastNode
Fetch next from table_sursor into @LastNode
--检查@@Fetch_Status以确定是否还可以继续取数
while @@Fetch_Status = 0
begin
  if(@LastNode = 1)
  begin
SELECT ClassName,ClassId From Class where ParentId =@ClassId  
  end
  Fetch next from table_sursor into @LastNode

end

消息 156,级别 15,状态 1,过程 GetChildId,第 5 行
关键字 'declare' 附近有语法错误。

------解决方案--------------------
SQL code
Declare     table_sursor   Cursor   for   Select   LastNode   from   Class   where   ParentId   =@ClassId 
-->
Declare     table_sursor   Cursor   
LOCAL 
STATIC
READ_ONLY
for   Select   LastNode   from   Class   where   ParentId   =@ClassId

------解决方案--------------------
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 
alter FUNCTION [dbo].[GetChildId](@ClassId int) 
RETURNS table
AS 
begin
--定义变量@LastNode是否是最后一个节点 
declare @LastNode int 提示这里有错 
--定义游标, 
Declare table_sursor Cursor for Select LastNode from Class where ParentId =@ClassId 
--打开游标 
open table_sursor 
--提取第一行记录,赋值给变量@LastNode 
Fetch next from table_sursor into @LastNode 
--检查@@Fetch_Status以确定是否还可以继续取数 
while @@Fetch_Status = 0 
begin 
if(@LastNode = 1) 
begin 
SELECT ClassName,ClassId From Class where ParentId =@ClassId
end 
Fetch next from table_sursor into @LastNode 

end 
close table_sursor
deallocate table_sursor
end
------解决方案--------------------
呵呵,少了
begin
end

------解决方案--------------------
函数不能 select 返回数据,应该定义table变量,然后插入数据,然后return 那个表