将Unix变量传递给psql date_part函数

时间:2017-10-04 10:43:17

标签: sql postgresql variables unix psql

这是我的剧本:

/// <summary>
/// The operation to be invoked by the action execution.
/// </summary>
public UML.Classes.Kernel.Operation operation { 
    get{
        // first get the operations guid which is stored in the Classifier_guid column
        XmlDocument operationGUIDxml = this.model.SQLQuery(@"select o.Classifier_guid from t_object o
                            where o.Object_ID = " + this.id.ToString());
        XmlNode operationGUIDNode = operationGUIDxml.SelectSingleNode(this.model.formatXPath("//Classifier_guid"));
        return this.model.getOperationByGUID(operationGUIDNode.InnerText);
    }
}

我得到的错误:

  

[root] #sh new.sh错误:列“2017”不存在第1行:... om   log_archive其中date_part('year',log_time)=“2017”和...

当我尝试在没有变量的情况下手动完成时,会获得所需的结果。

请帮忙。

1 个答案:

答案 0 :(得分:0)

双引号(")表示标识符。只需删除它们并使用裸号,你应该没问题:

psql -U postgres -d dbname -t -c "select id, time, code, level, message, trace  from logarchive where date_part('year',log_time) = $Year and date_part('month',log_time) = $Month and date_part('day',log_time) = $Day and date_part('hour',log_time) = $Hour and log_level = 'ERROR' limit 5;" > /tmp/List.txt