DECLARE INT SELECT ISNULL MAX OD LOGICAL_NUMBER FROM TONLINE_DEVICES O

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
DECLARE @I INT
SELECT @I = ISNULL(MAX(OD.LOGICAL_NUMBER), 0)
FROM TONLINE_DEVICES OD
WHERE OD.ONLINE_DEVICE_ID IN
(
SELECT DEVICES.ONLINE_DEVICE_ID
FROM TMACHINES M
INNER JOIN TSHOPS SHOP ON SHOP.SHOP_ID = M.SHOP_ID
INNER JOIN TMACHINES M_ALL ON M_ALL.SHOP_ID = SHOP.SHOP_ID
INNER JOIN TONLINE_DEVICES DEVICES ON DEVICES.MACHINE_ID = M_ALL.MACHINE_ID
WHERE M.MACHINE_ID = @MACHINEID
)
INSERT INTO TONLINE_DEVICES (ONLINE_DEVICE_ID, SERIAL_NUMBER, PORT,SPEED, ADDRESS,
ONLINE_DEVICE_MODEL_ID,MACHINE_ID,LOGICAL_NUMBER)
SELECT
NEWID(), '', 1, 115200, '', ODM.ONLINE_DEVICE_MODEL_ID, @MACHINEID,
@I + ROW_NUMBER() OVER(ORDER BY ODM.ORDER_NUMBER)
FROM
TONLINE_DEVICE_MODELS ODM,
TONL_DEV_MODEL_MM_SETTING_LINKS ONLL
WHERE
ODM.ONLINE_DEVICE_MODEL_ID = ONLL.ONLINE_DEVICE_MODEL_ID
AND ONLL.MACHINE_MODEL_SETTING_ID = @MACHINEMODELID