一个存储过程
CREATE PROC dbo.au_inAccount
@id VARCHAR(10)
AS
IF NOT EXISTS (SELECT ID FROM USERS WHERE ID=@id)
BEGIN
BEGIN TRANSACTION
DECLARE @STR NVARCHAR(4000)
SET @STR =N'SELECT @username=USERNAME,@email=EMAIL,@address=ADDRESS FROM OPENQUERY(ODB,''' + 'SELECT USERNAME,EMAIL,ADDRESS FROM OLD_USERS WHERE ID = ''''' + @id + ''''''')'
DECLARE @username VARCHAR(20)
DECLARE @email VARCHAR(50)
DECLARE @address NVARCHAR(100)
EXEC sp_executesql @STR,
N'@username VARCHAR(20) OUT,@email VARCHAR(50) OUT,@address NVARCHAR(100) OUT',
@username OUT,@email OUT,@address OUT
--PRINT @username + ' || ' + @email + ' || ' + @address
INSERT INTO USERS(ID,USERNAME,EMAIL,ADDRESS) VALUES(@id,@username,@email,@address)
COMMIT
END
GO
@id VARCHAR(10)
AS
IF NOT EXISTS (SELECT ID FROM USERS WHERE ID=@id)
BEGIN
BEGIN TRANSACTION
DECLARE @STR NVARCHAR(4000)
SET @STR =N'SELECT @username=USERNAME,@email=EMAIL,@address=ADDRESS FROM OPENQUERY(ODB,''' + 'SELECT USERNAME,EMAIL,ADDRESS FROM OLD_USERS WHERE ID = ''''' + @id + ''''''')'
DECLARE @username VARCHAR(20)
DECLARE @email VARCHAR(50)
DECLARE @address NVARCHAR(100)
EXEC sp_executesql @STR,
N'@username VARCHAR(20) OUT,@email VARCHAR(50) OUT,@address NVARCHAR(100) OUT',
@username OUT,@email OUT,@address OUT
--PRINT @username + ' || ' + @email + ' || ' + @address
INSERT INTO USERS(ID,USERNAME,EMAIL,ADDRESS) VALUES(@id,@username,@email,@address)
COMMIT
END
GO