Stored Procedure Variables

妖精的绣舞 提交于 2019-12-02 18:36:21

问题


I am afraid my terminology has hindered me from finding the results I am looking for on the internet. Anyhow, I have a fairly complex stored procedure that I need to pass several variables to. Instead of entering these variables several time through the stored procedure, is there a way to define a variable at the beginning of the procedure, and reference that variable throughout the procedure? For example

IDNumber = 1075, 1050, 1025

Instead of having to use the where clause several time throughout the procedure,

WHERE IDNumber = 1075 AND IDNumber = 1050 AND IDNumber = 1025

is there a way for me to define those integers in a single variable, and pass the variable through the stored procedure?

Sorry for any confusion that accompanies this questions, as SQL is very new to me. Thank you for your time and help.

I'm using SQL Server 2008 R2.


回答1:


Yes you want to pass in a table valued parameter to your stored proc. Give this article a read:

http://www.techrepublic.com/blog/the-enterprise-cloud/passing-table-valued-parameters-in-sql-server-2008/

Basically you will define the type:

CREATE TYPE myType AS TABLE
(                     
       [myId] int NULL
)      

Then create a stored proc that accepts that type:

CREATE PROCEDURE myProc
(
      @TableVariable myType READONLY
)
AS
BEGIN
...


来源:https://stackoverflow.com/questions/20667207/stored-procedure-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!