Thank you for helping me with the problem of transferring data between 3 Databases (same server).
I would like to edit the question
Suppose I have 3 Databases: Data1 and Data2 and Data3.
And in these data there are the same tables and columns.
Now I use this command and it works ok
CREATE PROCEDURE [dbo].[copy]
@id_id INT,
@namecreate Nvarchar(50),
@txtdataname sysname
AS BEGIN
DECLARE @InsertedRows TABLE (id INT)
--Insert Master
insert into [Data2].[dbo].[Table1] (cot1, cot2, cot3, cot4, namecreate)
OUTPUT inserted.id
INTO @InsertedRows
SELECT cot1, cot2, cot3, cot4, @namecreate From Table1 WHERE (id = @id_id)
--Insert Detail
insert into [Data2].[dbo].[Table2] (id, cot1, cot2, cot3, cot4)
SELECT (SELECT TOP (1) id FROM @InsertedRows), cot1, cot2, cot3, cot4 From Table2 WHERE (id = @id_id)
END
I want to change that Data2 place to a dynamic passed in @txtdataname Because I tried to write it like this, I got an error
CREATE PROCEDURE [dbo].[copy]
@id_id INT,
@namecreate Nvarchar(50),
@txtdataname sysname
AS BEGIN
DECLARE @InsertedRows TABLE (id INT)
--Insert Master
insert into [@txtdataname].[dbo].[Table1] (cot1, cot2, cot3, cot4, namecreate)
OUTPUT inserted.id
INTO @InsertedRows
SELECT cot1, cot2, cot3, cot4, @namecreate From Table1 WHERE (id = @id_id)
--Insert Detail
insert into [@txtdataname].[dbo].[Table2] (id, cot1, cot2, cot3, cot4)
SELECT (SELECT TOP (1) id FROM @InsertedRows), cot1, cot2, cot3, cot4 From Table2 WHERE (id = @id_id)
END