English 中文(简体)
服务器在储存程序中使用EXEC/sp_executesql 或仅使用平原ql?
原标题:SQL Server use EXEC/sp_executesql or just plain sql in stored procedure?

我有一个简单的称呼,为最佳质问/优化而制定守则的最佳方式是什么?

下面是我。

ALTER PROCEDURE dbo.OccupierGet

(
@OccupierID int = 0
)

AS
/* SET NOCOUNT ON */
--Get all details that can be editted.
select TOP 1 ID,AccountNumber,FirstName,LastName,Company,Telephone,Notes,
OccupierTypeID,Address,Address2,City,Country,Telephone2,HomePhone,CellPhone,WorkPhone,Fax,EmailAddress 
from dbo.Occupier
where ID = @OccupierID

RETURN

建设q沟,作为str子和 run子,并通过参数,是否更好? 我会问,因为盘问和参数Im使用。

Thank you! Tim

最佳回答

这里没有任何理由使用动态结构。 当您d需要使用能动的电能时,您应考虑<条码>。 优于<代码>EXEC()。 原因多种多样,包括:

  1. > 动态 页: 1 相对于EXEC SP_EXECUTESQL(@SQL);

  2. 远比窒息性更强的投射法。

  3. you can also get variables from within the dynamic SQL scope back out to the calling scope, for example:

DECLARE @i INT, @sql NVARCHAR(MAX), @dbname SYSNAME = N model ;

SET @sql = N SELECT @i = COUNT(*) FROM   
    + @dbname +  .sys.tables;  

EXEC sp_executesql @sql, N @i INT OUTPUT , @i = @i OUTPUT;

PRINT @i;

That s not a very useful example, but it is a common problem when executing dynamic strings. But more to the point, you should only be considering dynamic SQL when you have to, not as a first resort.

问题回答

暂无回答




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签