0

当我作为参数传递给 sp_execute 时,我找不到服务器名称(我已将服务器添加为链接服务器)。当我传递一个硬编码的服务器名称时,它会被执行。

SET @PARMDEFINITION =N'@SOURCE_SERVER_NAME_IN VARCHAR(100), @SOURCE_DB_NAME_IN VARCHAR(100) ,@DESTI_SERVER_NAME_IN VARCHAR(100), @DESTI_DB_NAME_IN VARCHAR(100),
                       @DESTI_STUDENT_COUNT_OUT INT OUTPUT,@SOURCE_STUDENT_COUNT_OUT INT OUTPUT';


SET @SQLSTRING='SELECT @SOURCE_STUDENT_COUNT_OUT=COUNT(*) FROM [@SOURCE_SERVER_NAME_IN].[@SOURCE_DB_NAME_IN].DBO.STUDENT_MST WHERE CONVERT(FLOAT, RANK) <> 0 AND PAID2 =''S'''                 

EXECUTE SP_EXECUTESQL @SQLSTRING,@PARMDEFINITION,@SOURCE_SERVER_NAME_IN=@SOURCE_SERVER_NAME,@DESTI_SERVER_NAME_IN=@DESTI_SERVER_NAME,
@SOURCE_DB_NAME_IN=@SOURCE_DB_NAME,@DESTI_DB_NAME_IN=@DESTI_DB_NAME ,
@SOURCE_STUDENT_COUNT_OUT=@SOURCE_STUDENT_COUNT OUTPUT,@DESTI_STUDENT_COUNT_OUT=@DESTI_STUDENT_COUNT OUTPUT; 

在 sys.servers 中找不到服务器“+@servername +”

请指教。

4

1 回答 1

2

您不能将服务器或表名作为参数传递给sp_executesql

你必须这样做

SET @SQLSTRING='SELECT @SOURCE_STUDENT_COUNT_OUT=COUNT(*) FROM '
    + quotename(@SOURCE_SERVER_NAME_IN) + '.'
    + quotename(@SOURCE_DB_NAME_IN) 
    + '.DBO.STUDENT_MST WHERE CONVERT(FLOAT, RANK) <> 0 AND PAID2 =''S'''                  
于 2012-09-18T12:24:14.853 回答