在INSERT..SELECT中包含硬编码的值

时间:2018-12-28 14:31:56

标签: sql sql-server tsql

我在SQL中有此存储过程,可以正常工作。但是我想在执行过程时在Assesable列中插入文本'POSTED';我需要帮助来实现这一目标。

CREATE PROCEDURE [dbo].[spBankToAll]
AS
BEGIN
    INSERT INTO AllPayandRec (BID, EntryDate, Typez, FullName, Assesable)
        SELECT 
            Bank.BID, Bank.EntryDate, Bank.Type, Bank.Assesable  
        FROM 
            Bank                       
        LEFT JOIN 
            AllPayandRec ON Bank.BID = AllPayandRec.BID 
        WHERE 
            AllPayandRec.BID IS NULL
END

因此,将“ POSTED”插入到所有行中作为执行过程的一部分

3 个答案:

答案 0 :(得分:2)

这非常有效,我认为我怀恨在心。 :o)

CREATE PROCEDURE [dbo].[spBankToAll]
AS
BEGIN
INSERT INTO AllPayandRec           
      (BID
      ,EntryDate
      ,Typez
      ,FullName
      ,Assesable)
SELECT Bank.BID
      ,Bank.EntryDate
      ,Bank.Type
      ,Bank.FullName
      ,'POSTED'  
FROM Bank                      
 Left JOIN AllPayandRec ON Bank.BID = AllPayandRec.BID 
WHERE AllPayandRec.BID IS Null
END

答案 1 :(得分:2)

只需在"POSTED"语句中用SELECT替换它:

INSERT INTO AllPayandRec (BID, EntryDate, Typez, FullName, Assesable)
     SELECT Bank.BID, Bank.EntryDate, Bank.Type, Bank.FullName, 'POSTED'
     FROM Bank LEFT JOIN 
          AllPayandRec 
          ON Bank.BID = AllPayandRec.BID 
     WHERE AllPayandRec.BID IS NULL;

答案 2 :(得分:0)

System.out.println(myList);