-1
  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 中执行以下操作的存储过程示例: - 多行 - 包含输入和输出参数 - 在存储过程中设置输出参数之一

谁能提供一些见解?即为什么以下不起作用?

消息 156,级别 15,状态 1,过程 CHI_X2,第 31 行
关键字“PROCEDURE”附近的语法不正确。

Nsg 156,级别 15,状态 1,过程 CHI_X2,第 56 行
关键字“PROCEDURE”附近的语法不正确。

消息 102,级别 15,状态 1,过程 CHI_X2,第 112 行“END”附近的语法不正确。

提前致谢!

4

1 回答 1

2

当然,这里是对输出参数的基本介绍。

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

CREATE PROCEDURE dbo.uspMyProc
@SomeParameter varchar(10),
@TheOutputParameter varchar OUTPUT
AS
  -- Do stuff
  SELECT @TheOutputParameter = somecolumn FROM something
  -- or
  SET @TheOutputParameter = someresult
RETURN
于 2013-08-28T20:15:59.150 回答