日期:2014-05-18 浏览次数:20464 次
DECLARE @TableName VARCHAR(50),@Sql NVARCHAR(MAX),@OrderID int , @a varchar(100) SET @TableName = 'Orders'; SET @OrderID=10251; SET @a = 'SELECT * FROM '+QUOTENAME(@TableName) +'WHERE OrderID = @OrderID ORDER BY ORDERID DESC' SET @Sql = 'SELECT * FROM '+QUOTENAME(@TableName) +'WHERE OrderID = '+CAST(@OrderID AS VARCHAR(10))+' ORDER BY ORDERID DESC' PRINT @a PRINT @Sql SELECT * FROM [Orders]WHERE OrderID = @OrderID ORDER BY ORDERID DESC SELECT * FROM [Orders]WHERE OrderID = 10251 ORDER BY ORDERID DESC 中间为啥要将变量转换一下。