更新时更改数据

时间:2015-08-05 08:22:08

标签: sql sql-server

我有两个表,当我进行插入时,我不想传输创建的数据,我想放入sysdate。我有下一个代码:

create or replace procedure procedure1 as 
begin
  INSERT INTO tlp
            (given_name, 
             namel, 
             email, sysdate) 
SELECT first_name, 
       namel, 
       mail 
FROM   glob 
WHERE  ( NOT EXISTS (SELECT email 
                     FROM   tlp 
                     WHERE  glob.mail = tlp.email ) 
         AND glob.mail IS NOT NULL ) 
        OR ( NOT EXISTS (SELECT namel 
                         FROM   tlp 
                         WHERE  glob.namel = tlp.namel ) 
             --AND glob.mail IS NULL 
             ); 
end procedure1; 

2 个答案:

答案 0 :(得分:1)

create or replace procedure procedure1 as 
begin
  INSERT INTO tlp
            (given_name, 
             namel, 
             email, sysdate) 
SELECT first_name, 
       namel, 
       mail,
       GETDATE() as  sysdate       
FROM   glob 
WHERE  ( NOT EXISTS (SELECT email 
                     FROM   tlp 
                     WHERE  glob.mail = tlp.email ) 
         AND glob.mail IS NOT NULL ) 
        OR ( NOT EXISTS (SELECT namel 
                         FROM   tlp 
                         WHERE  glob.namel = tlp.namel ) 
             --AND glob.mail IS NULL 
             ); 
end procedure1;

答案 1 :(得分:0)

procedure insert tlp中,为表4提供的列数为columns,而从表glob中选择的3为{ {1}}。

如果您想insert sysdate列表tlp的一列,请提及 列名称并在sysdate语句中添加select

假设tlp的表结构是

tlp (
  given_name, 
  namel, 
  email, 
  date_created --if clmn name is not "date_created", then change it in the proc.
)

现在试试这个;

create or replace procedure 
procedure1 as 
begin
  INSERT INTO tlp (
    given_name, namel, 
    email, date_created
  ) 
  SELECT 
    first_name, namel, 
    mail, sysdate --note sysdate is given in select query
  FROM   glob 
  WHERE (
    NOT EXISTS (
      SELECT email 
      FROM   tlp 
      WHERE  glob.mail = tlp.email 
    ) 
    AND glob.mail IS NOT NULL 
  ) OR ( 
    NOT EXISTS (
      SELECT namel 
      FROM   tlp 
      WHERE  glob.namel = tlp.namel 
    ) 
    --AND glob.mail IS NULL 
  ); 
end procedure1;