I have a stored procedure in SQL Server 2012 which has a few parameters including table type parameters as well, as SQL server 2012 does not support table type parameters i wrote another stored procedure by setting default values for this table type parameter and having only varchar as parameter for the new procedure
DECLARE @pTempTable TABLE(
region varchar(max),
country varchar(max),
username varchar(max),
firstname varchar(max),
lastname varchar(max),
accessform varchar(max),
accessmodule varchar(max),
adate varchar(max),
atime varchar(max)
)
BEGIN
DECLARE @pTestFromDate DATETIME = '2008/01/01', @pTestToDate DATETIME = '2014/12/31'
DECLARE @pCountryIds GuidsTableType
INSERT @pCountryIds VALUES(NULL)
DECLARE @pRegionIds GuidsTableType
INSERT @pRegionIds VALUES(NULL)
DECLARE @pUserId UNIQUEIDENTIFIER = NULL
DECLARE @pModuleId UNIQUEIDENTIFIER = NULL
insert into @pTempTable exec usp_rpt_UserAudit
@RoleName = @pRoleName
,@pCountryIds=@pCountryIds
,@pRegionIds=@pRegionIds
,@pUserId=@pUserId
,@pModuleId=@pModuleId
,@pFromDate=@pTestFromDate
,@pToDate=@pTestToDate
SELECT * from @pTempTable
END
GO
I have written a java code to execute this stored procedure, i am able to establish the connection, but the execute() method always returns false. The java code is as below
String finalSql = "{ call usp_rpt_sprocexecution(?) } ";
CallableStatement cs = connection.prepareCall(finalSql);
cs.setString(1, "Super Administrator");
System.out.println(cs.execute());
int c=0;
while(!((cs.getMoreResults() == false) && (cs.getUpdateCount() <= 1))){
System.out.println(c++);
ResultSet rs = cs.getResultSet();
System.out.println(rs.getString(1));
}
When i execute this same stored procedure in Management Studio it returns correct result table.
SET NOCOUNT ONto the start of the procedure. The messages like1 row(s) affectedare being passed back, and probably being interpreted as result sets. This will suppress these messages.