2 gl19910916 gl19910916 于 2014.10.14 17:39 提问

在程序中调用存储过程确查不出数据?

在sql数据库中调用存储过程可以查询出结果,在程序中调用存储过程确查不出数据?这是什么原因?

1个回答

asiaasia666
asiaasia666   2014.10.15 12:52
        use test1
    go
    IF EXISTS (SELECT *
               FROM   sys.objects
               WHERE  object_id = Object_id(N'[t_sp_test111]')
                      AND Objectproperty(object_id, N'IsUserTable') = 1)
        drop table t_sp_test111
    go
    create table t_sp_test111 (id int)
    go
    IF EXISTS (SELECT *
               FROM   sys.objects
               WHERE  object_id = Object_id(N'[sp_test111]')
                      AND Objectproperty(object_id, N'IsProcedure') = 1)
    drop procedure sp_test111
    go

    create procedure sp_test111 ( @i int, @c varchar(100) output)
    as
    insert into t_sp_test111 values (100)
    set @i = 1
    print @i
    set @c = '@c value'
    insert into t_sp_test111 values (200)
    --RAISERROR('testest 错误错误了!!!',16,-1)
    insert into t_sp_test111 values (900)
    set @c = '@c value2'
    go

sp_test111有2个参数,第一个是输入参数,第二个是output。
如果是用java程序调用:

    public void testProceRaiseError(Connection conn) throws Exception{

    java.sql.CallableStatement psc = conn.prepareCall("{ call sp_test111 (?,?)}");

// CallableStatement psc = conn.prepareCall(" exec sp_test111 ?,? "); -- is ok too
psc.setInt(1,123);
psc.registerOutParameter(2, Types.VARCHAR);

    psc.execute();
    String cOutStr = psc.getString(2);
    System.out.println(cOutStr);
}

注意我会写上psc.registerOutParameter(2, Types.VARCHAR);

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!