我有一个存储过程,当我从MSSQL Server GUI调用时。它正确返回结果。但是当我从我的C ++代码调用相同的过程时,它执行正确但不给出输出参数结果。 我尝试了存储过程简单地返回输出,而没有做任何其它成功但是下面给出的存储过程失败。我认为在执行存储过程时发生了一些错误。 继承我的存储过程:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'putFileinFTandJT' AND type = 'P')
DROP PROCEDURE putFileinFTandJT
GO
CREATE PROCEDURE putFileinFTandJT
/* Put incoming file in File Table & Job Table */
/* IN */ @input varchar(max),
/* IN */ @flName varchar(max),
/* IN */ @uid INT,
/* IN */ @jbid INT,
/* OUT */@t INT OUTPUT,
/* OUT */@t1 INT OUTPUT
AS
DECLARE @orgFileID INT = -1;
DECLARE @temp1 INT;
DECLARE @newFileID INT;
DECLARE @versionCnt INT = 0;
/* SET autocommit = 0; */
EXEC getDirId @input,@temp1 OUTPUT ;
if @temp1 = -1
EXEC putDir @uid,@input,@temp1 OUTPUT;
select @orgFileID = FileID from FileTable where DirID=@temp1 and FileName=@flName and VersionNumber = 0;
IF @orgFileID = -1
BEGIN
set @orgFileID = 0;
insert into FileTable(FileName,DirID,IsDirectory,UserID,VersionNumber,isduplicate) values(@flName,@temp1,0,@uid,0,0);
END
ELSE
BEGIN
select @versionCnt = count(*) from VersionTable where FileID = @orgFileID;
insert into FileTable(FileName,DirID,IsDirectory,UserID,VersionNumber,isduplicate) values(@flName,@temp1,0,@uid,@versionCnt + 1,0);
SET @versionCnt = @versionCnt + 1;
END
select @newFileID = FileID from FileTable where FileName = @flName and DirID = @temp1 and VersionNumber = @versionCnt;
insert into JobTable values(@jbid,@newFileID);
commit;
set @t = @newFileID;
set @t1 = @orgFileID;
GO
//编辑 这是cpp代码
HWND desktopHandle = GetDesktopWindow();
SQLCHAR buff[255]={0};
int intval1=0,intval2=0,intval3 = 0,intval4,intval5;
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0);
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
retcode = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
retcode = SQLDriverConnect(hdbc, desktopHandle, (SQLWCHAR*) L"Driver={SQL Server};Server=WIN-E9EO4VT0V8I;Database=connector;UID=sa;PWD=spsoft_123;Trusted_Connection=False;", SQL_NTS, NULL, 0, NULL, 0);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
retcode = SQLPrepare(hstmt, (SQLWCHAR*)L"{call putFileinFTandJT(?,?,?,?,?,?,?)}" , SQL_NTS);
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 100, 0, szQuote, 0, &cbValue2);
retcode = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 50, 0, szQuote2, 0, &cbValue2);
retcode = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &iuser , 0, &cbValue5);
retcode = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_SLONG , SQL_INTEGER, 0, 0, &ibackup , 0, &cbValue5);
retcode = SQLBindParameter(hstmt, 5, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &intval1, 0, &cbValue6);
retcode = SQLBindParameter(hstmt, 6, SQL_PARAM_OUTPUT, SQL_C_SLONG , SQL_INTEGER, 0, 0, &intval2, 0, &cbValue7);
retcode = SQLBindParameter(hstmt, 7, SQL_PARAM_OUTPUT, SQL_C_SLONG , SQL_INTEGER, 0, 0, &intval3, 0, &cbValue8);
retcode = SQLExecute(hstmt);
if(retcode==SQL_ERROR)
{
SQLWCHAR sqlstate[1024];
SQLWCHAR message[1024];
if(SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, sqlstate, NULL, message, 1024, NULL))
std::cout<<"Message: "<<(wchar_t*)message<<"\nSQLSTATE: "<<sqlstate<<std::endl;
}
请帮忙,因为我没有得到任何想法。 感谢
答案 0 :(得分:1)
结果中是否还有其他结果计数?我不确定你正在使用哪个API,但是根据我的经验从SP调用中获取输出参数通常意味着遍历结果返回跳过最终RPC结果的结果计数。 (您也可以尝试将set nocount on
放入SP中。