简体   繁体   English

在 Microsoft Sql Server Management Studio 中测试存储过程

[英]Test a stored procedure in Microsoft Sql Server Management Studio

如何在 Microsoft Sql Server Management Studio 中测试现有的存储过程?

Not sure of best approach here is how I do it:不确定最好的方法是我如何做到的:

You can right click the sp > tasks > execute to > new query window.您可以右键单击 sp > 任务 > 执行到 > 新查询窗口。 This will allow you to call the SP with parameters.这将允许您使用参数调用 SP。

You can then do selects at various points in the SP for debugging.然后,您可以在 SP 中的各个点进行选择以进行调试。

The other way if it is a really complex SP is to take the code out of an SP and just declare variables in place of the parameters then you can just run the TSQL code directly.如果它是一个非常复杂的 SP,另一种方法是将代码从 SP 中取出并声明变量来代替参数,然后您可以直接运行 TSQL 代码。

Would love to hear any better ways though.不过很想听听任何更好的方法。

Here The explanation/Example from MSDN Using Variables and Parameters (Database Engine)这里是MSDN 中使用变量和参数(数据库引擎)的解释/示例

Transact-SQL has several ways to pass data between Transact-SQL statements. Transact-SQL 有多种方法在 Transact-SQL 语句之间传递数据。 These include the following:其中包括:

Transact-SQL local variables. Transact-SQL 局部变量。

A Transact-SQL variable is an object in Transact-SQL batches and scripts that can hold a data value. Transact-SQL 变量是 Transact-SQL 批处理和脚本中可以保存数据值的对象。 After the variable has been declared, or defined, one statement in a batch can set the variable to a value and a later statement in the batch can get the value from the variable.在声明或定义变量后,批处理中的一条语句可以将变量设置为一个值,而批处理中的后面一条语句可以从该变量中获取该值。 For example:例如:

Copy复制

USE AdventureWorks2008R2;
GO
DECLARE @EmpIDVar int;
SET @EmpIDVar = 1234;
SELECT *
FROM HumanRresources.Employee
WHERE BusinessEntityID = @EmpIDVar;

Note The maximum number of local variables that can be declared in a batch is 10,000.注意 可以在批处理中声明的最大局部变量数为 10,000。 Transact-SQL parameters. Transact-SQL 参数。

A parameter is an object used to pass data between a stored procedure and the batch or script that executes the stored procedure.参数是用于在存储过程和执行存储过程的批处理或脚本之间传递数据的对象。 Parameters can be either input or output parameters.参数可以是输入或输出参数。 For example:例如:

Copy复制

USE AdventureWorks2008R2;
GO
CREATE PROCEDURE ParmSample @EmpIDParm int AS
SELECT BusinessEntityID, JobTitle
FROM HumanResources.Employee
WHERE BusinessEntityID = @EmpIDParm ;
GO

EXEC ParmSample @EmpIDParm = 109 ;
GO

Applications use application variables and parameter markers to work with the data from Transact-SQL statements.应用程序使用应用程序变量和参数标记来处理来自 Transact-SQL 语句的数据。

Application variables应用变量

The application programming languages such as C, C++, Basic, and Java have their own variables for holding data. C、C++、Basic 和 Java 等应用程序编程语言都有自己的变量来保存数据。 Applications using the database APIs must move the data returned by Transact-SQL statements into application variables before they can work with the data.使用数据库 API 的应用程序必须将 Transact-SQL 语句返回的数据移动到应用程序变量中,然后才能使用数据。 This is typically done using a process called binding.这通常使用称为绑定的过程来完成。 The application uses an API function to bind the result set column to a program variable.应用程序使用 API 函数将结果集列绑定到程序变量。 When a row is fetched the API provider or driver moves the data from the column to the bound program variable.当获取一行时,API 提供程序或驱动程序将数据从列移动到绑定的程序变量。

Parameter markers参数标记

Parameter markers are supported by the ADO, OLE DB, and ODBC-based database APIs. ADO、OLE DB 和基于 ODBC 的数据库 API 支持参数标记。 A parameter marker is a question mark (?) placed in the location of an input expression in a Transact-SQL statement.参数标记是放置在 Transact-SQL 语句中输入表达式位置的问号 (?)。 The parameter marker is then bound to an application variable.然后将参数标记绑定到应用程序变量。 This allows data from application variables to be used as input in Transact-SQL statements.这允许将来自应用程序变量的数据用作 Transact-SQL 语句中的输入。 Parameter markers also let stored procedure output parameters and return codes be bound to application variables.参数标记还允许存储过程输出参数和返回码绑定到应用程序变量。 The output data is then returned to the bound variables when the procedure is executed.然后在执行过程时将输出数据返回到绑定变量。 The DB-Library API also supports binding stored procedure parameter and return codes to program variables. DB-Library API 还支持将存储过程参数和返回码绑定到程序变量。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM