Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Trigger Problem Cannot Insert to Second Table

$
0
0

Hi,

I have a question. Encountered something weird during testing. 

I have Table A , Table B and Table C.

Originally, after insert, Table A will trigger some value to Table B if the data is not existed yet. 

I changed the Trigger a bit, by adding another line to the trigger, if the record not exist, it will insert to Table C.

When I run that, the data is populated to Table B only.

Then, I changed the Table C to a temp table. Then re-run. The data is populated to Table B and temp table.

I am using MSSQL 2012. Thank you. I am unable to insert record to Table C.

---------------------------------------------------------------------------------------------------------------------------------

CREATE TRIGGER [TG].[TR_I_TableA]
ON [TG].[T_TableA]
AFTER INSERT
AS 
BEGIN
SET NOCOUNT ON

DECLARE test1BIGINT
DECLARE test2BIGINT

SELECT test1 = [testA],
test2 = [testB]
FROM[Inserted]
WHERE result = 1

IF(@@ROWCOUNT > 0)
BEGIN 
IF NOT EXISTS(SELECT [testA] FROM [BO].[T_TableB] WHERE [testA] = test1 AND [testB] = test2)
BEGIN
INSERT INTO [BO].[T_TableB]
(
[testA],
[testB] )
VALUES
(
test1,
test2
)
END


IF NOT EXISTS(SELECT [testA] FROM [BO].[T_TableC] WHERE [testA] = test1 AND [testB] = test2 )
BEGIN

INSERT INTO [BO].[T_TableC]
(
[testA]
)
VALUES
(
test1
)
END


END
SET NOCOUNT OFF
END
GO 


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>