插入带有交易的其他表格的最大ID的选择更改表



我有一个更改表,我需要为事务表中的ID分配下一个可用ID。批处理完成后,它将发布到交易表上。如何在Select Insert语句中获得另一个表的最大 1?我必须在同一查询中将其连接到code_id。

INSERT INTO CHANGES 
(P.CODE, P.ID, P.CODE_ID, P.FIRST_NAME, P.MIDDLE_NAME, P.LAST_NAME) 
SELECT 'P', MAX(transactionstable.ID) +1, 
'P0000' + MAX(transactionstable.ID) +1, 'first','middle','last';
    INSERT INTO CHANGES (
                         P.CODE, 
                         P.ID, 
                         P.CODE_ID,
                         P.FIRST_NAME,
                         P.MIDDLE_NAME, 
                         P.LAST_NAME
                         ) 
    SELECT  P,      --remove single quote, otherwise it is static value
            MAX(transactionstable.ID) +1, 
           'P0000' + CAST((MAX(transactionstable.ID) +1) as VARCHAR(50)), --Conversion here for string concatenation 
            first,  --remove single quote, otherwise it is static value
            middle, --remove single quote, otherwise it is static value
            last    --remove single quote, otherwise it is static value
/*********************
    missing here
**********************/    
    FROM transactions 

最新更新