所以我有以下存储过程:
CREATE PROCEDURE dbo.uspUpdateOrInsertDataObjectValue
@ParentDataObjectId INT,
@DataObjectName NVARCHAR(256),
@DataObjectValue NVARCHAR(256),
@ForceInsert BIT,
@ReturnDataObjectId INT OUT
WITH RECOMPILE
AS
BEGIN
if @ForceInsert = 0
BEGIN
UPDATE DataObjects SET Value=@DataObjectValue WHERE Name=@DataObjectName AND ParentDataObjectId=@ParentDataObjectId
END
if ( @ForceInsert = 1 ) OR ( @@ROWCOUNT = 0 )
BEGIN
--Spend time on the hierarchy id stuff only if we have to insert
DECLARE @ParentTreeId HierarchyId
DECLARE @LastChildTreeId HierarchyId
SELECT @ParentTreeId = TreeId FROM DataObjects WHERE DataObjectId=@ParentDataObjectId
SELECT @LastChildTreeId = MAX(TreeId) FROM DataObjects WHERE TreeId.GetAncestor(1)=@ParentTreeId
INSERT INTO DataObjects ( Name, Value, ParentDataObjectId, TreeId ) VALUES ( @DataObjectName, @DataObjectValue, @ParentDataObjectId, @ParentTreeId.GetDescendant( @LastChildTreeId, NULL ) )
END
SELECT @ReturnDataObjectId = (SELECT TOP 1 DataObjectId FROM DataObjects WHERE Name=@DataObjectName AND ParentDataObjectId=@ParentDataObjectId ORDER BY DataObjectId DESC)
RETURN 0
END
GO
`
我正在执行它:
int iParentDataObjectId = 1;
int iDataObjectId = 0;
SQLWCHAR name[256];
SQLWCHAR value[256];
_sntprintf_s( (wchar_t*)name, 256, _TRUNCATE, _T("%s"), pCDBDataObject->GetName() );
_sntprintf_s( (wchar_t*)value, 256, _TRUNCATE, _T("%s"), pCDBDataObject->GetString() );
result = SQLBindParameter( sqlStatementHandle, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &iParentDataObjectId, 0, NULL );
result = SQLBindParameter( sqlStatementHandle, 2, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, 256, 0, name, 256, NULL );
result = SQLBindParameter( sqlStatementHandle, 3, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, 256, 0, value, 256, NULL );
result = SQLBindParameter( sqlStatementHandle, 4, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, 1, 0, &forceInsert, 0, NULL );
result = SQLBindParameter( sqlStatementHandle, 5, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &iDataObjectId, 0, NULL );
result = SQLExecDirect( sqlStatementHandle, (SQLWCHAR*)_T("{CALL uspUpdateOrInsertDataObjectValue (?,?,?,?,?)}"), SQL_NTS );
'
存储过程正确执行并插入或更新数据库,但iDataObjectId永远不会更改。
但是当我在Sql Server Management Studio的脚本中执行它时:
DECLARE @testval int
EXECUTE uspUpdateOrInsertDataObjectValue 1, 'Config', 'testasdfasdfasdfas', 1, @testval OUT
SELECT * from DataObjects where DataObjectId=@testval
输出值分配给@testval,后续的select语句工作正常。
我错过了什么?
答案 0 :(得分:0)
在仔细检查这个例子之后我找到了答案:http://technet.microsoft.com/en-us/library/ms403283.aspx
在输出变量设置之前,您需要使用SqlMoreResults清除查询(调用)的结果集。不是我期望的行为,但它正好在文档中。