CREATE TRIGGER TR_SUBECARGINSERTSTOCK ON STOCK
AFTER INSERT
	,UPDATE
AS
BEGIN
	SET NOCOUNT ON;

	DELETE
	FROM SUBECARDS
	WHERE CARDID IN (
			SELECT INSERTED.STOCKID
			FROM INSERTED
			)
		AND ROOT = 301

	INSERT INTO SUBECARDS (
		COMPANYID
		,ROOT
		,CARDID
		,SUBEID
		,STATUS
		,SUBEPRICE
		)
	SELECT STOCK.COMPANYID
		,301
		,STOCK.STOCKID
		,SUBE.SUBEID
		,0
		,0
	FROM SUBE
		,STOCK
	WHERE STOCK.COMPANYID = (
			SELECT COMPANYID
			FROM INSERTED
			)
		AND SUBE.COMPANYID = (
			SELECT COMPANYID
			FROM INSERTED
			)
		AND STOCK.STOCKID = (
			SELECT INSERTED.STOCKID
			FROM INSERTED
			)
END
