为数组中的每个 val 运行存储过程

发布于 2024-12-08 21:56:49 字数 1099 浏览 0 评论 0原文

我无法理解这个问题..

我有两个单独的存储过程,我们称它们为:

createTable (takes a varchar userID as input)
runReport (takes two dates for input, as varchar)

createTable 创建一个名为 ##tempTable 的虚拟表(如果它不存在,则添加提供的 userID)。因此,如果我运行

EXEC createTable 'user-32'
EXEC createTable 'user-33'
EXEC createTable 'user-34'

,那么这些 ID 就会存储在 ##tempTable 中。

之后我想运行这个:

 EXEC runReport '2011-01-01', '2011-10-01'

这应该会给我一个很大的美味输出。 问题是,当像这样运行它时,它可以完美地工作:

 mssql_query("EXEC createTable 'user-32'");
 mssql_query("EXEC createTable 'user-33'");
 mssql_query("EXEC createTable 'user-34'");
 mssql_query("EXEC runReport '2011-01-01', '2011-10-01'");

但是当我尝试循环出 createTable-lines 时,runReport-SP 不会返回任何数据。

示例:

$userIDs = explode(',', $userID_str);
foreach ($userIDs as $user) {
  if (mb_strlen($user) > 0) {
    mssql_query("EXEC createTable '$user'");
  }
}

我想知道这是否与连接断开或其他原因有关?看来 runReport-SP 可以找到#tempTable,但当我使用循环时它无法从中读取。

有什么想法吗?

谢谢! :)

Can't wrap my head around this one..

I have two separate stored procedures, let's call them:

createTable (takes a varchar userID as input)
runReport (takes two dates for input, as varchar)

createTable creates a virtual table called ##tempTable (if it doesn't exists and add the provided userID). So if I run

EXEC createTable 'user-32'
EXEC createTable 'user-33'
EXEC createTable 'user-34'

I then have these IDs in ##tempTable.

After that I want to run this:

 EXEC runReport '2011-01-01', '2011-10-01'

Which should give me a big tasty output.
The thing is that this works flawlessly when running it like so:

 mssql_query("EXEC createTable 'user-32'");
 mssql_query("EXEC createTable 'user-33'");
 mssql_query("EXEC createTable 'user-34'");
 mssql_query("EXEC runReport '2011-01-01', '2011-10-01'");

But when I try to loop out the createTable-lines the runReport-SP doesn't return any data.

Example:

$userIDs = explode(',', $userID_str);
foreach ($userIDs as $user) {
  if (mb_strlen($user) > 0) {
    mssql_query("EXEC createTable '$user'");
  }
}

I'm wondering if this could have anything to do with a broken connection or something? It seems that the runReport-SP can find the #tempTable but it can't read from it when I use a loop.

Any ideas?

Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆夏时光 2024-12-15 21:56:49

那么以这种方式调用你的程序怎么样:

$conn = mssql_connect(...);
$stmt = mssql_init("createTable", $conn);
foreach ($userIDs as $user) {
    if(mb_strlen($user) > 0) {
        mssql_bind($stmt, "@userID", &$user, SQLVARCHAR);
        mssql_execute($stmt);
    }
}

$stmt = mssql_init("runReport", $conn);
mssql_bind($stmt, "@date1", "2011-01-01", SQLVARCHAR);
mssql_bind($stmt, "@date2", "2011-01-01", SQLVARCHAR);
$result = mssql_execute($stmt);
print_r(mssql_fetch_array($result));

And what about calling Your procedures this way:

$conn = mssql_connect(...);
$stmt = mssql_init("createTable", $conn);
foreach ($userIDs as $user) {
    if(mb_strlen($user) > 0) {
        mssql_bind($stmt, "@userID", &$user, SQLVARCHAR);
        mssql_execute($stmt);
    }
}

$stmt = mssql_init("runReport", $conn);
mssql_bind($stmt, "@date1", "2011-01-01", SQLVARCHAR);
mssql_bind($stmt, "@date2", "2011-01-01", SQLVARCHAR);
$result = mssql_execute($stmt);
print_r(mssql_fetch_array($result));

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文