DECLARE @sql VARCHAR (8000)
DECLARE @Id VARCHAR(20)
SELECT @Id= '31124'
SELECT @sql =
'
IF EXISTS
(
SELECT * FROM OPENQUERY
(
SERVERNAME,
''SELECT Name FROM table WHERE Id= ''''' + @Id + '''''''
)
)
BEGIN
UPDATE OPENQUERY
(
SERVERNAME,
''SELECT Name FROM table WHERE Id= ''''' + @Id + '''''''
)
SET Name = ''NewName''
ELSE
INSERT OPENQUERY
(
SERVERNAME,
''SELECT Name FROM table''
)
VALUES
(
''NewName''
)
END
'
EXEC (@sql)
Если у вас нет особых требований для использования EXEC
, он вам не нужен.
DECLARE @Id VARCHAR(20)
SELECT @Id= '31124'
IF EXISTS
(
SELECT * FROM OPENQUERY
(
SERVERNAME,
'SELECT Name FROM table WHERE Id= ''' + @Id + ''''
)
)
BEGIN
UPDATE OPENQUERY
(
SERVERNAME,
'SELECT Name FROM table WHERE Id= ''' + @Id + ''''
)
SET Name = 'NewName'
ELSE
INSERT OPENQUERY
(
SERVERNAME,
'SELECT Name FROM table'
)
VALUES
(
'NewName'
)
END