我自己做了一个,没有问题,你可以看一下
use Northwind
go
create proc test
  @StartOrderID int,
  @EndOrderID int,
  @Code varchar(1000) Out
As
  Begin
    Declare @tmp int
        Set @Code=''    
        Declare #cur_orders cursor  for  Select OrderID From Orders 
            where OrderID>=@startOrderID and OrderID<=@EndOrderID
            for read only
        Open #cur_Orders
        fetch next from #cur_orders into @tmp
         while @@fetch_Status=0
          Begin
              Set @Code=@Code+'-'+convert(varchar(8),@tmp)
               fetch next from #cur_orders into @tmp
          End
        close #cur_Orders
        Deallocate #cur_Orders
        return
    
  End
go

解决方案 »

  1.   

    续2
       String ret=null;
        try{
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
          String url
    ="jdbc:microsoft:sqlserver://192.168.0.102:1433;DatabaseName=Northwind";
          String user="sa";
          String password="";
          Connection conn= DriverManager.getConnection(url,user,password);
          CallableStatement stmt=conn.prepareCall("exec test ?,?,?");
          stmt.setInt(1,10248);
          stmt.setInt(2,10284);
          stmt.registerOutParameter(3,Types.VARCHAR);
          stmt.setString(3,ret);
          stmt.execute();
          System.out.println(stmt.getString(3));
          stmt.close();
          stmt=null;
          conn.close();
        }catch(ClassNotFoundException e){
          e.printStackTrace();
        }catch(SQLException e){
          e.printStackTrace();
        }
      

  2.   

    太感谢了!我只是按照jdbc tutorial上套用了{call procedureName(?,?)},毛病就在于此!
    真的很感谢你能写了一个列子来验证我的问题!thank you Djava!
      

  3.   

    上面的例子没有问题,针对你的情况,我又写了一个,应该可以解决你现在的问题-- 新建一个表
      Create table tmpOrders (
         OrderID int,
         CustomerID nchar(5)
     )--把Orders 里的OrderID列全部插入,这样Orders与tmpOrders之间就是1:1关系了
    insert into tmpOrders
    Select distinct orderID,'tmp' from Orderscreate proc test
      @StartOrderID int,
      @EndOrderID int,
      @Code varchar(1000) Out
    As
      Begin
        Declare @newOrderID int
        Declare @newCustomerID nchar(5)
        Declare @DummyInt int
        Declare @DummyChar nchar(5)
            Set @Code=''  
            /*
                                                                    1:1 
                temp table/formal table is synchronized tmpOrders  <---> Orders
                fetch from Orders, update tmpOrders
            */        -- for temp table
            Declare #cur_tmpOrders  Cursor for select OrderID,CustomerID 
                   From tmpOrders 
                      where OrderID>=@startOrderID 
                      and OrderID<=@EndOrderID                 
                      for  update        --for formal table
            Declare #cur_orders cursor  for  Select OrderID,CustomerID 
                 From Orders 
                where OrderID>=@startOrderID 
                and OrderID<=@EndOrderID 
                for read only
            Open #cur_Orders
            Open #cur_tmpOrders        fetch next from #cur_tmpOrders into @DummyInt,@dummyChar --Important!!!
            fetch next from #cur_orders into @NewOrderID,@NewCustomerID
             while @@fetch_Status=0
              Begin
                  --Set @Code=@Code+'-'+convert(varchar(8),@NewOrderID)
                  --update tempOrders use corresponding Orders' data
                   Update tmpOrders set customerID=@newCustomerID 
                        where current of #cur_tmpOrders 
                   --pay attention to sequence of cursor fetch action!
                   fetch next from #cur_tmpOrders into @DummyInt,@dummyChar
                   if @@fetch_Status<>0  break; -- 没有行了
                   fetch next from #cur_orders into @newOrderID,@NewCustomerID
              End        close #cur_Orders
            close #cur_tmpOrders        Deallocate #cur_Orders
            Deallocate #cur_tmpOrders
            Set @Code='Ok'
            return
        
      End
      

  4.   

    程序如下
        try{
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
          String url=
    "jdbc:microsoft:sqlserver://192.168.0.102:1433;DatabaseName=Northwind";
          String user="sa";
          String password="";
          Connection conn= DriverManager.getConnection(url,user,password);
          CallableStatement stmt=conn.prepareCall("exec test  ?,?,?");
          stmt.setInt(1,10248);
          stmt.setInt(2,10284);
          stmt.registerOutParameter(3,Types.VARCHAR,1000);
          stmt.setString(3,ret);
          stmt.executeUpdate();
          System.out.println(stmt.getString(3));
          stmt.close();
          stmt=null;
          conn.close();
          conn=null;
        }catch(ClassNotFoundException e){
          e.printStackTrace();
        }catch(SQLException e){
          e.printStackTrace();
        }