1

我有 4 张不同的桌子

  • 帐户,
  • 分支,
  • 提单者,
  • tblbiller 政策,

包含其列的 4 个表

  • 帐户 (accntID) 主键...
  • 分支(branchID)主键...
  • tblbillers (Billerid) 和
  • tblbilerpolicy (PolicyID Primary Key, accountID, BranchID, BillerID, Enabled, ServiceCharge, MerchantCOmission,PLUCode)

现在我想像这样插入表 tblbilerpolicy:

INSERT INTO dbo.tblbillerpolicy
      ( 

        AccountID                  ,
        BranchID                   ,
        BillerID                   ,
        Enabled                    ,
        ServiceCharge              ,
        MerchantComission          ,
        PLUCode        

      ) 

Select 142, 2171, 2, 'YES', 0.00, 3.50, 'NULL'

UNION ALL

Select 143, 2171, 2, 'YES', 0.00, 3.50, 'NULL'

UNION ALL

Select  143, 2171, 2, 'YES', 0.00, 3.50, 'NULL'

UNION ALL

Select  143, 2171, 2, 'YES', 0.00, 3.50, 'NULL'

where AccountID = accntid <-- for account
and BranchID = branchid <-- for branch
    and BillerID = billerid <-- for tblbillers

现在我真正想做的是将这个“ONLY”插入与上述表(帐户,分支,账单)连接的tblbillerpolicy。,

4

1 回答 1

1

克里斯蒂安·赫尔曼 CM,

您是否要将数据(accountID 142、143 等)插入到具有以下条件的表中:
- accountID (142, 143) 已经在 account 表中,
- branchID (2171) 已经在表中分支
- billerID (2) 已经在账单表中


如果是,请尝试以下查询

INSERT INTO dbo.tblbillerpolicy
  ( 
    AccountID                  ,
    BranchID                   ,
    BillerID                   ,
    Enabled                    ,
    ServiceCharge              ,
    MerchantComission          ,
    PLUCode        
  ) 

select t.* from
(Select AccountID=142, BranchID=2171, BillerID=2, Enabled='YES', ServiceCharge=0.00,  MerchantComission=3.50, PLUCode='NULL'
UNION ALL
Select AccountID=143, BranchID=2171, BillerID=2, Enabled='YES', ServiceCharge=0.00, MerchantComission=3.50, PLUCode='NULL'
UNION ALL
Select  AccountID=143, BranchID=2171, BillerID=2, Enabled='YES', ServiceCharge=0.00, MerchantComission=3.50, PLUCode='NULL'
UNION ALL
Select  AccountID=143, BranchID=2171, BillerID=2, Enabled='YES', ServiceCharge=0.00, MerchantComission=3.50, PLUCode='NULL') t,
account a, branch b, tblbillers tb
where a.AccountID = t.accountID --for account
and b.BranchID = t.BranchID --for branch
and tb.BillerID = t.BillerID --for tblbillers
于 2012-10-18T04:41:49.240 回答