嵌套存储过程
我需要创建一个存储过程,它将返回代码列表,然后我需要调用另一个存储过程来逐个检查每个代码。
我该怎么做?
CREATE PROCEDURE [dbo].[paBltBuscarBoletasASA] @id_Asa int
AS
DECLARE @Query int, @Contador int
SET @Contador = 0
BEGIN
SET NOCOUNT ON;
SET @Query = (
SELECT
localizacion.c_Fk_IdBoleta
FROM
Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE
boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = @id_Asa) //This query give the list of Codes. For example 45550711, 40480711, 80110711... etc
exec dbo.paBltMarcarErroresBoleta @Query //And here I need send one by one that list of Codes
END
I need create a stored proc that will return a list of a code, and then I need to call another stored proc to review each code, one by one.
How can I do this?
CREATE PROCEDURE [dbo].[paBltBuscarBoletasASA] @id_Asa int
AS
DECLARE @Query int, @Contador int
SET @Contador = 0
BEGIN
SET NOCOUNT ON;
SET @Query = (
SELECT
localizacion.c_Fk_IdBoleta
FROM
Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE
boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = @id_Asa) //This query give the list of Codes. For example 45550711, 40480711, 80110711... etc
exec dbo.paBltMarcarErroresBoleta @Query //And here I need send one by one that list of Codes
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑添加标量函数并在查询中调用它,例如:
You may consider adding an scalar function and call it in your query, like:
为您设置为等于 @Query 的查询声明 CURSOR,然后在 WHILE @@FETCH_STATUS = 0 循环中将每个后续值插入到变量中。然后将 @Query 变量传递给第二个存储过程,就像您当前所做的那样。下面是一个示例:
其他光标帮助:http://msdn.microsoft.com/ en-us/library/ms180169.aspx
Declare a CURSOR for the query that you are setting equal to @Query, and then insert into the variable each subsequent value in a WHILE @@FETCH_STATUS = 0 loop. Then pass the @Query variable to the second stored procedure as you are currently doing. Here is an example:
Additional cursor help: http://msdn.microsoft.com/en-us/library/ms180169.aspx