SQL Server优化器-不工作?
我发现了以下关于 他们说: 在这种情况下,还可以使用一种优化器技巧,即 单个变量被设置为一个潜在的值列表,它将 指定列表中最后一项的值 然后他们提供以下样本:SQL Server优化器-不工作?,sql,sql-server,tsql,stored-procedures,Sql,Sql Server,Tsql,Stored Procedures,我发现了以下关于 他们说: 在这种情况下,还可以使用一种优化器技巧,即 单个变量被设置为一个潜在的值列表,它将 指定列表中最后一项的值 然后他们提供以下样本: CREATE PROCEDURE [dbo].[usp_PageResults_NAI] ( @startRowIndex int, @maximumRows int ) AS DECLARE @first_id int, @startRow int -- A check can be added to make
CREATE PROCEDURE [dbo].[usp_PageResults_NAI]
(
@startRowIndex int,
@maximumRows int
)
AS
DECLARE @first_id int, @startRow int
-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that
-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid
-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows
SELECT e.*, d.name as DepartmentName
FROM employees e
INNER JOIN Departments D ON
e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID
SET ROWCOUNT 0
GO
基于上述文章,我编写了以下T-SQL存储过程:
CREATE PROCEDURE Find_Programs
@programId INTEGER
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SelectQuery NVARCHAR(2000)
DECLARE @first_id INTEGER
SET @SelectQuery = 'SELECT @first_id = bp.program_id FROM program bp WHERE bp.program_id >= @programId ORDER BY bp.program_id'
EXECUTE sp_Executesql @SelectQuery, N'@first_id INTEGER, @programId INTEGER', @first_id, @programId
PRINT 'first_id: ' + CONVERT(VARCHAR(10),@first_id);
END
但是当我运行EXEC dbo.Find_Programs@programId=0时,我没有得到任何输出。似乎
@first\u id
没有更改其值知道为什么吗?因为您没有指定@first\u id
是一个输出参数-您正在将其传递到sp\u Executesql,但没有将其返回。解释如何将输出参数与sp_Executesql一起使用。因为您没有指定@first_id
是一个输出参数-您正在将其传递到sp_Executesql,但没有将其返回。解释如何在sp_Executesql中使用输出参数。谢谢-这帮助我了解到此处的输出指的是sp_Executesql的输出,不是调用sp_Executesql
的存储过程。谢谢-这帮助我了解到这里的输出指的是sp_Executesql
的输出,而不是调用sp_Executesql
的存储过程。