开发者

Run stored procedure for each val in array

开发者 https://www.devze.com 2023-04-12 11:53 出处:网络
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)

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! :)


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));

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号