简体   繁体   English

如何从ADO / C ++调用MSSQL系统函数?

[英]How Do You Call an MSSQL System Function From ADO/C++?

...specifically, the fn_listextendedproperty system function in MSSQL 2005. ...特别是MSSQL 2005中的fn_listextendedproperty系统功能。

I have added an Extended Property to my database object, named 'schemaVersion'. 我已将扩展属性添加到名为'schemaVersion'的数据库对象中。 In my MSVC application, using ADO, I need to determine if that Extended Property exists and, if it does, return the string value out of it. 在我的MSVC应用程序中,使用ADO,我需要确定扩展属性是否存在,如果存在,则从中返回字符串值。

Here is the T-SQL code that does what I want. 这是执行我想要的T-SQL代码。 How do I write this in C++/ADO, or otherwise get the job done? 如何用C ++ / ADO编写此代码,否则如何完成工作?

select value as schemaVer
from fn_listextendedproperty(default, default, default, default, default, default, default)
where name=N'schemaVersion'

Here's the code I tried at first. 这是我最初尝试的代码。 It failed with the error listed below the code: 它失败,并在代码下面列出了错误:

_CommandPtr cmd;
cmd.CreateInstance(__uuidof(Command));
cmd->ActiveConnection = cnn;

cmd->PutCommandText("select value "
    "from fn_listextendedproperty(default, default, default, default, default, default, default) "
    "where name=N'schemaVersion'");
VARIANT varCount;
cmd->Execute(NULL, NULL, adCmdText);

...here are the errors I peeled out of the ADO errors collection. ...这是我从ADO错误集合中剥离的错误。 The output is from my little utility function which adds the extra text like the thread ID etc, so ignore that. 输出来自我的小实用程序函数,该函数添加了额外的文本,例如线程ID等,因此请忽略它。

(Proc:0x1930, Thread:0x8A0) INFO : ===   1 Provider Error Messages : =======================
(Proc:0x1930, Thread:0x8A0) INFO : [  1]   (-2147217900) 'Incorrect syntax near the keyword 'default'.'
(Proc:0x1930, Thread:0x8A0) INFO :         (SQLState = '42000')
(Proc:0x1930, Thread:0x8A0) INFO :         (Source = 'Microsoft OLE DB Provider for SQL Server')
(Proc:0x1930, Thread:0x8A0) INFO :         (NativeError = 156)
(Proc:0x1930, Thread:0x8A0) INFO : ==========================================================

EDIT : Updated the call according to suggestions. 编辑 :根据建议更新了呼叫。 Also changed "SELECT value AS schemaVer" to just "SELECT value". 还将“ SELECT值AS schemaVer”更改为“ SELECT值”。

EDIT : Changed the first parameter of Execute() to NULL per suggestion. 编辑 :每个建议将Execute()的第一个参数更改为NULL。 This fixed my original problem, and I proceeded to the next. 这解决了我原来的问题,然后继续进行下一个。 :) :)

Try specifying NULL rather than default for each parameter of fn_listextendedproperty. 尝试为fn_listextendedproperty的每个参数指定NULL而不是默认值。 This should hopefully then execute without errors, just leaving you to retrieve the result as your next step. 希望这应该没有错误地执行,只是让您检索结果作为下一步。

I still have not figured out how to do this directly. 我仍然没有想出如何直接做到这一点。 To get on with my life, I wrote a stored procedure which called the function: 为了继续生活,我编写了一个存储过程,该存储过程称为函数:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[mh_getSchemaVersion]
@schemaVer VARCHAR(256) OUTPUT
AS
    select @schemaVer = CAST( (select value from fn_listextendedproperty(default, default, default, default, default, default, default) where name=N'schemaVersion') AS varchar(256) )
    return @@ROWCOUNT

...and then called thst sproc from my ADO/C++ code: ...然后从我的ADO / C ++代码中调用了sproc:

_CommandPtr cmd;
cmd.CreateInstance(__uuidof(Command));
cmd->ActiveConnection = cnn;
cmd->PutCommandText("mh_getSchemaVersion")_l

_variant_t schemaVar;
_ParameterPtr schemaVarParam = cmd->CreateParameter("@schemaVer", adVarChar, adParamOutput, 256);
cmd->GetParameters()->Append((IDispatch*)schemaVarParam);

cmd->Execute(NULL, NULL, adCmdStoredProc);

std::string v = (const char*)(_bstr_t)schemaVarParam->GetValue();

ver->hasVersion_ = true;

...which works, but I didn't want to have to deploy a new stored procedure. ...可以,但是我不想部署新的存储过程。

So if anyone can come up with a solution to the original problem and show me how to call the system function directly from ADO/C++, I will accept that as the answer. 因此,如果任何人都可以提出原始问题的解决方案,并向我展示如何直接从ADO / C ++调用系统函数,那么我将接受它作为答案。 Otherwise I'll just accept this. 否则我会接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM