SQL Server错误中存储过程未解决

时间:2013-08-28 20:09:05

标签: sql sql-server-2008

  CREATE FUNCTION CHI_X2 
  (
  @a1 INT,
  @b1 INT,
  @a2 INT,
  @b2 INT
  )
  RETURNS int
  AS
  BEGIN
  -- Declare the return variable here
  DECLARE @Result int

  DECLARE @tr1 INT;
  DECLARE @tr2 INT;
  DECLARE @tc1 INT;
  DECLARE @tc2 INT;
  DECLARE @ca1 INT;
  DECLARE @ca2 INT;
  DECLARE @cb1 INT;
  DECLARE @cb2 INT;
  DECLARE @xi INT;
  DECLARE @nt INT;

  CREATE PROCEDURE [dbo].[pro1]
  AS
  BEGIN

     SET @tr1 = @a1+@b1
     SET @tr2 = @a2+@b2
     SET @tc1 = @a1+@a2
     SET @tc2 = @b1+@b2
     SET @nt = @tr1+@tr2
     SET @ca1 =(@tc1/@nt*@tr1)
     SET @ca2 =(@tc1/@nt*@tr2)
     SET @cb1 =(@tc2/@nt*@tr1)
     SET @cb2 =(@tc2/@nt*@tr2)
     SET @xi =((power((@a1 -@ca1),2)/@ca1)+(power((@a2 -@ca2),2)/@ca2)+(power((@b1-@cb1),2)/@cb1)+(power((@b2 -@cb2),2)/@cb2))
     -- Add the T-SQL statements to compute the return value here


  SELECT @Result = @xi

  -- Return the result of the function
  RETURN @Result


  --END CHI_X2

  CREATE PROCEDURE [dbo].[pro2]
  AS
  begin

     DECLARE @max_chi INT
     DECLARE @maxpos INT
     DECLARE @n INT
     DECLARE @SWV_CUR_OUT_Sessionnumber VARCHAR(255)
     DECLARE @SWV_CUR_OUT_sessioncount VARCHAR(255)
     DECLARE @SWV_CUR_OUT_timespent VARCHAR(255)
     DECLARE @SWV_cursor_var1 CURSOR
     DECLARE @SWV_CUR_IN_Sessionnumber VARCHAR(255)
     DECLARE @SWV_CUR_IN_sessioncount VARCHAR(255)
     DECLARE @SWV_CUR_IN_timespent VARCHAR(255)
     delete from CH_TABLE
     commit
     SET @SWV_cursor_var1 = CURSOR  FOR select sessionnumber, sessioncount, timespent from clusters  order by sessionnumber asc
     OPEN @SWV_cursor_var1
     FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_CUR_OUT_sessionnumber,@SWV_CUR_OUT_sessioncount,@SWV_CUR_OUT_timespent
     while @@FETCH_STATUS = 0
     begin
        SET @max_chi = -999
        SET @maxpos = NULL
        SET @SWV_cursor_var1 = CURSOR  FOR select sessionnumber, sessioncount, timespent from clusters  order by sessionnumber asc
        OPEN @SWV_cursor_var1
        FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_CUR_IN_sessionnumber,@SWV_CUR_IN_sessioncount,@SWV_CUR_IN_timespent
        while @@FETCH_STATUS = 0
        begin
           select   @n = count(*) from(select x1 as x from CH_TABLE union all select x2 from CH_TABLE) AS TabAl
           where x = @SWV_CUR_OUT_sessionnumber or x = @SWV_CUR_IN_sessionnumber
   if @n = 0
  begin
    -- SET @xi = round(CHI_X2(cur_out.sessioncount,cur_out.timespent,cur_in.sessioncount,cur_in.timespent),2)
     if @xi > @max_chi
     begin
        SET @max_chi = @xi
        SET @maxpos = cur_in.sessionnumber
     end
  end

     FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_CUR_IN_sessionnumber,@SWV_CUR_IN_sessioncount,@SWV_CUR_IN_timespent
        end
  if @max_chi > -999
  begin
  INSERT INTO CH_TABLE(SNO, P, T) VALUES(cur_out.sessionnumber, @maxpos, @max_chi)

     commit
  end


  CLOSE @SWV_cursor_var1
  FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_CUR_OUT_sessionnumber,@SWV_CUR_OUT_sessioncount,@SWV_CUR_OUT_timespent
     end
     CLOSE @SWV_cursor_var1

  END

我来自MySQL背景,最近开始迁移到SQL Server。

在我的生活中,我不能找到在SQL Server中执行以下操作的存储过程的示例: - 多线 - 包含输入和输出参数 - 设置存储过程中的一个输出参数

任何人都可以提供一些见解吗?即为什么以下不起作用?

  

Msg 156,Level 15,State 1,Procedure CHI_X2,Line 31
  关键字'PROCEDURE'附近的语法不正确。

     

Nsg 156,Level 15,State 1,Procedure CHI_X2,Line 56
  关键字'PROCEDURE'附近的语法不正确。

     

Msg 102,Level 15,State 1,Procedure CHI_X2,Line 112   'END'附近的语法不正确。

提前致谢!

1 个答案:

答案 0 :(得分:2)

当然,here's a basic introduction来输出参数。

这是它的短暂和甜蜜。请注意输出参数上的OUTPUT关键字:

CREATE PROCEDURE dbo.uspMyProc
@SomeParameter varchar(10),
@TheOutputParameter varchar OUTPUT
AS
  -- Do stuff
  SELECT @TheOutputParameter = somecolumn FROM something
  -- or
  SET @TheOutputParameter = someresult
RETURN