Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PostgreSQL:在批处理中创建多个pgplsql过程_Php_Postgresql - Fatal编程技术网

Php PostgreSQL:在批处理中创建多个pgplsql过程

Php PostgreSQL:在批处理中创建多个pgplsql过程,php,postgresql,Php,Postgresql,我有一个PHP脚本,其中包含几个类似的SQL表(只有1列不同): 我想为它们中的每一个创建一个upsert过程: define('SQL_PROCS', ' create or replace function hide_id_upsert( _ID varchar, _USERNAME varchar ) returns void as $BODY$ begin update hide_

我有一个PHP脚本,其中包含几个类似的SQL表(只有1列不同):

我想为它们中的每一个创建一个upsert过程:

define('SQL_PROCS', '
create or replace function hide_id_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update hide_id set
                        ID       = _ID,
                        USERNAME = _USERNAME,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into hide_id (
                                ID,
                                USERNAME
                        ) values (
                                _ID,
                                _USERNAME
                        );
                end if;
        end;
$BODY$ language plpgsql;
');
我想知道,我是否可以通过PgPlSQL或PHP方法批量完成它。比如:

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS_TEMPLATE, $key, $key, ...);
   $sth->execute();
}
但是没有上述必须多次指定$key的丑陋

在CentOS 5.5 Linux下使用PostgreSQL 8.4.5和PHP 5.1.6


谢谢大家!!Alex

嗯,我看不到您在字符串中的任何地方使用
%s
作为
sprintf
,但是如果您想在同一
sprintf
字符串中多次使用同一变量,您可以对第一个参数使用
%1$s
,如下所示:

sprintf('%1$s %1$s', 'hubba') // hubba hubba
编辑:以下是链接:。当然,这不是非常明确的,但我希望这是你正在寻找的

编辑:那么,在您的情况下,您应该:

define('SQL_PROCS', '
create or replace function %1$s_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update %1$s set
                        ID       = _ID,
                        %2$s = _%2$s,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into %1$s (
                                ID,
                                %2$s
                        ) values (
                                _ID,
                                _%2$s
                        );
                end if;
        end;
$BODY$ language plpgsql;
');

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS, 'hide_' . strtolower($key), $key));
   $sth->execute();
}

谢谢你提供这些信息,我已经更新了我的问题,让它更清楚。也许有更好的解决方案……这是怎么回事?它使用sprintf的两个参数来解释小写表名和大写列。(另外,它缺少一个结束参数,我将在2秒钟内添加它。我们开始了。)
define('SQL_PROCS', '
create or replace function %1$s_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update %1$s set
                        ID       = _ID,
                        %2$s = _%2$s,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into %1$s (
                                ID,
                                %2$s
                        ) values (
                                _ID,
                                _%2$s
                        );
                end if;
        end;
$BODY$ language plpgsql;
');

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS, 'hide_' . strtolower($key), $key));
   $sth->execute();
}