运行代码后为什么提示'go' 附近有语法错误。??
我把运行后的代码拷贝到查询分析器里可以运行的呀,好奇怪,为什么呢??        public DataSet GetNodeByID(decimal AID)
        {
            string sb = @"Create Function f_cid(@ID varchar(30)) returns @t_level table(id varchar(30) , level int) 
                          as
                          begin
                          declare @level int
                          set @level = 1
                          insert into @t_level select @id , @level
                          while @@ROWCOUNT > 0
                          begin
                          set @level = @level + 1
                          insert into @t_level select a.id , @level
                          from sys_questiondir a , @t_Level b
                          where a.parentid = b.id and b.level = @level - 1
                          end
                          return
                          end";
                    sb+=@"
                          go
                        ";
                    sb += "select a.* from Sys_questiondir a , f_cid(" + AID + ") b where a.id = b.id drop function f_cid  ";
            Siwei.LFCNET2.Data.Base bsql = new Siwei.LFCNET2.Data.Base();
            DataSet ds = bsql.RunSQL2(sb.ToString(), null, "Sys_questiondir");
            return ds;
        }

解决方案 »

  1.   

    分成两段写吧  先执行创建函数的SQL 再Select
      

  2.   

    go不是sql语句,是sql客户端可识别的命令
      

  3.   

    Create Function f_cid(@ID varchar(30)) returns @t_level table(id varchar(30) , level int) 
                              as
                              begin
                              declare @level int
                              set @level = 1
                              insert into @t_level select @id , @level
                              while @@ROWCOUNT > 0
                              begin
                              set @level = @level + 1
                              insert into @t_level select a.id , @level
                              from sys_questiondir a , @t_Level b
                              where a.parentid = b.id and b.level = @level - 1
                              end
                              return
                              end
                              go
                            select a.* from Sys_questiondir a , f_cid(382) b where a.id = b.id drop function f_cid
    这是我调试的时候拷贝出来的SQL代码,在查询分析器运行没问题的呀
      

  4.   

    〉〉这是我调试的时候拷贝出来的SQL代码,在查询分析器运行没问题的呀查询分析器是sql客户端,它可以识别go用来分批提交,但go不是sql语句,sql服务器不能识别所以不能用在你的程序中。
      

  5.   

    declare @sql varchar(900)
    set @sql = "create .........................."
    exec(@sql)
      

  6.   

    Create Function 不需要你在客户端执行,数据库对象除了临时表之类除外其他都应该在数据库端部署。
      

  7.   

    楼主还真执着,昨天就会过这帖子了
    建函数的直接写sql server里
    程序里写select 那句就行了
      

  8.   

    呵呵,我何尝不想直接写在sql server里??有规定