in Education by
I need to synchronize two tables. TableA Id Name TableB Id Name RefID --It's a Foreign key, defined as primary key in Table "TableReference" TableReference RefID -- Identity Column, auto increment I need to merge TableA and TableB in such a way that on each insert in TableB, a value should be inserted into TableReference and that inserted value should be copied into RefId column of TableB. What I am doing? I am using SSIS hence I need to have a SSIS based solution OR SQL based solution. I know how to merge tables using Merge SQL command but I am not able to insert a value into TableRef and copy it back to TableB. Could not figure out how i can accomplish this. SQL user-defined functions don't allow INSERT so I can't use it. Merge TabaleB T Using Table A S On S.Id=T.Id WHEN MATCHED THEN UPDATE T.ID=S.ID, T.NAME=S.NAME WHEN NOT MATCHED BY TARGET THEN INSERT(S.ID,S.NAME, {Somehow here i need a function call that inserts in TableRef and Returns SCOPE_IDENTITY}) The problem is T-SQL functions don't allow INSERT and a stored procedure can't be called here as Merge doesn't allow any TSQL thing other than INSERT after WHEN NOT MATCHED BY TARGET THEN. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
If you are able to add a trigger in your Table2 then you should use INSERTED row. Refer to this video to learn MERGE STATEMENT in SQL. Use this code: CREATE TRIGGER dbo.tr_Table2 ON dbo.Table2 FOR INSERT AS BEGIN SET NOCOUNT ON; INSERT TableReference DEFAULT VALUES; DECLARE @RefId INT; SELECT @RefId = SCOPE_IDENTITY(); UPDATE ta SET ta.Ref_Id = @Ref_Id FROM dbo.TableB AS ta INNER JOIN INSERTED AS i1 ON i1.Id= ta.Id END GO

Related questions

0 votes
    I need to synchronize two tables. TableA Id Name TableB Id Name RefID --It's a Foreign key, defined as ... MATCHED BY TARGET THEN. Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [ ... not supply any columns. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the ... of the management studio.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have seen SQL that uses both != and for not equal. What is the preferred syntax and why? I like !=, ... reminds me of Visual Basic. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part: 2008-09-22 00: ... How can I get that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table (col1, col2, ... .id = other_table.id Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    looking for a bit of advice on SQL indexing. Whats the best type of index to create on a non- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I am creating a script on the fly to ftp some files from a remote computer. I create a file ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am creating a script on the fly to ftp some files from a remote computer. I create a file ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
...