如果不存在,则基于SELECT语句创建表

时间:2016-03-03 09:29:50

标签: postgresql create-table

如果基于PostgreSQL 9.2中的select语句不存在,我想创建一个表。当我使用下面的查询时,我会收到如下所述的错误。

查询:

CREATE TABLE IF NOT EXISTS ccdb_archival.bills
SELECT *, now() AS archival_date
FROM ccdb.bills
WHERE bill_date::date >= current_date - interval '3 years' AND bill_date::date < current_date - interval '8 years';

错误:

ERROR:  syntax error at or near "SELECT"
LINE 2:  SELECT *, now() AS archival_date

有人可以建议我如何实现这一目标。

1 个答案:

答案 0 :(得分:1)

我确实得到了替代品。我使用了下面提到的代码。

'use strict';


// import the moongoose helper utilities
var utils = require('../test/utils');
var should = require('should');
// import our User mongoose model
var User = require('../app/models/user.js');

var testUser1 = new Object({ 
    profilePic: "testPic",
    email: "testEmail",
    first_name: "fname",
    last_name: "lname",
    description: "description",
    personality: "personality",
    phone_number: "phoneNum",
    password: "password",
    courses: {
      course_name: "courseTest"
      },
    role: "testRole" //student/admin/teacher
});

var testUser2 = new Object({ 
    profilePic: "testPic2",
    email: "testEmail2",
    first_name: "fname2",
    last_name: "lname2",
    description: "description2",
    personality: "personality2",
    phone_number: "phoneNum2",
    password: "password2",
    courses: {
      course_name: "courseTest2"
      },
    role: "testRole2" //student/admin/teacher
});

describe('Users: models', function () {

describe('#register()', function () {
    it('should register a new User', function (done) {
      User.register(testUser1, function (err, createdUser) {
        should.not.exist(err);
        createdUser.email.should.equal("testEmail");
        createdUser.first_name.should.equal("fname");
        createdUser.phone_number.should.equal("phoneNum");
        createdUser.password.should.equal("password");
        createdUser.role.should.equal("testRole");
        done();
      });
    });
  });

  describe('#getAllUsers', function () {

    it('should return all users in DB', function (done) {

      console.log("about to add stuff");

      User.register(testUser1, function (err, createdUser) {
          console.log(createdUser);
      });

      User.register(testUser2, function (err, createdUser) {
        console.log(createdUser);
      });

      User.getAllUsers(function (err, createdUsers) {
        should.not.exist(err);
        createdUsers.should.be.instanceof(Array).and.have.lengthOf(2);
        done();
      });  
    });
  });
});