[英]inserting records from specific columns in one table to another table depending on multiple criteria
I'm Using SQL server and need to Inset records from one table into another empty table. 我正在使用SQL Server,需要将记录从一个表插入另一个空表。 the first table im pulling from is called accnt and has the fields: code, invno, invdate, ven, and amnt, and imported.
我从中提取的第一个表称为accnt,并具有以下字段:code,invno,invdate,ven和amnt,以及导入的。 the table that the records are going to is called quick and has the fields: date, num, name, account, split, and amount.
记录将转到的表称为快速表,具有以下字段:日期,数字,名称,科目,拆分和金额。 some of the fields are just directly copied which i understand, but what im having trouble with is that some of the fields need to be populated with a different field from accnt depending on the value in account.
我理解有些字段是直接复制的,但是我遇到的麻烦是,根据帐户中的值,某些字段需要使用accnt以外的其他字段填充。 and there is a field in accnt called imported that is either empty or has an x in it.
accnt中有一个名为import的字段,该字段为空或带有x。 i only want to import records where the imported field is empty
我只想导入导入字段为空的记录
the insert statement i have so far should import: date, name, account, and amount directly not depending on the anything. 到目前为止,我拥有的插入语句应直接输入:日期,名称,科目和金额,而不取决于任何内容。 But the num field imports from a different field in accnt depending on the value of the accno field.
但是num字段根据accno字段的值从accnt中的其他字段导入。 the criteria is:
条件是:
if account < 7000 then num = code if account > 7000 then num = invno 如果帐户<7000,则num =代码如果帐户> 7000,则num = invno
here is what i tried, but it didnt work 这是我尝试过的,但是没有用
DELETE FROM quick
INSERT INTO quick (date, num, name, account, amount)
if accnt.accno < '7000'
then SELECT invdate, code, ven, accno, amnt from accnt
if accnt.accno > '7000'
then SELECT invdate, invno, ven, accno, amnt from accnt
how do i accomplish what i'm trying to do? 我如何完成我想做的事情? the table looks like this even though the data in the picture is wrong:
即使图片中的数据有误,该表也会如下所示:
the type and split fields will be populated later with an update query. type和split字段稍后将使用更新查询进行填充。
Try it with a single SELECT and use CASE to separate the two situations. 使用单个SELECT尝试一下,并使用CASE来区分这两种情况。
DELETE FROM quick
INSERT INTO quick (date, num, name, account, amount)
SELECT invdate,
case when accnt.accno < '7000' then code else invno end
, ven, accno, amnt from accnt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.