当我将参数传递给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 +'
请告知。
答案 0 :(得分: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'''