Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in SQLServer to generate an insert from another insert statement without using functions?

I have a translation table that is related to almost every table in the database. I need to populate this table everytime. I make an insert in one of this tables. In ORACLE I did this way:

 INSERT  INTO M_ANYTABLE
         ( ID_ANYTABLE ,
           TEXT ,
           DATE ,
           TRANS_ID

 )

 VALUES  ( 'PEPE' ,
           'PEPE' ,
           SYSDATE ,
           FN_TRD_NOCOMMIT('TEXT TRANSLATE')
         );

Function FN_TRD_NOCOMMIT make the inserts in translations table.

I can't replicate this in SQLServer because functions cannot change the Database in any way. I fixed this making Triggers (one for each Table). Although I know is not a elegant solution.

What I am asking is if anyone can come with a better way of making inserts through the original insert statement (above) or any other solution that you have deployed for making inserts through a function or whatever doesn't imply using one trigger for each Table. Any ideas?

I hope I made my point correctly. Sorry in case of bad English. Thanks in advance.

like image 437
Julio Jiménez Avatar asked Feb 28 '26 22:02

Julio Jiménez


1 Answers

I am not sure if I understood you right, but you can use OUTPUT for thet:

CREATE TABLE #test
(
    ID_ANYTABLE VARCHAR(123),
           TEXT VARCHAR(123) ,
           DATE DATETIME
);

CREATE TABLE #test2
(
    ID_ANYTABLE VARCHAR(123),
           TEXT VARCHAR(123) ,
           DATE DATETIME
)

 INSERT  INTO #test
         ( ID_ANYTABLE ,
           TEXT ,
           DATE   
         )  
 OUTPUT INSERTED.ID_ANYTABLE, INSERTED.TEXT, INSERTED.DATE INTO    #test2      
 VALUES  ( 'PEPE' ,
           'PEPE' ,
           GETDATE() 
         );

SELECT * FROM #test
SELECT * FROM #test2         
like image 86
Dmitrij Kultasev Avatar answered Mar 03 '26 16:03

Dmitrij Kultasev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!