将简单查询链接到执行多个存储过程的脚本

时间:2012-01-10 15:03:16

标签: sql sql-server

对不起,我对此有点新鲜,所以只是试着把我的脑袋连接起来。

目前我有一个正常的查询 - SELECT FROM WHERE,它基本上找到了我需要更新几个表中哪个链接的2000条记录。

有人可以告诉我如何将这个简单的查询链接到其他东西,这样我基本上可以在同一个脚本中执行几个存储过程吗?但只影响我的简单查询返回的记录?

道歉,这可能听起来像泥一样清晰!

*编辑 - 更详细*

所以这是我的Select查询:

SELECT [MembershipTermID]
,[MemberStatusProgKey]
,[StartDate]
,[EndDate]
,[AdditionalDiscount]
,[EntryDateTime]
,[UpdateDateTime]
,[MembershipID]
,[AgentID]
,[PlanVersionID]
,[ForceThroughReference]
,[IsForceThrough]
,[NextTermPrePaid]
,[IsBillingMonthly]
,[CICSMEMBERNUM]
,[CICSHISTORY]
,[TMPSeqNoColumn]
,[LastPaymentDate]
,[PaidToDate]
,[IsIndeterminate]
,DATEDIFF(MONTH, PaidToDate, GETDATE()) as MonthsDifference
,dbo.FullMonthsSeparation (PaidToDate, GETDATE())
FROM [Apollo].[dbo].[MembershipTerm] 
WHERE MemberStatusProgKey='DORMANT' 
AND IsBillingMonthly=1 
AND dbo.FullMonthsSeparation (PaidToDate, GETDATE()) >= 2

因此,使用返回的行我想执行多个存储过程来更新数据库中需要更改这些行的所有内容。下面是一个存储过程的示例,我想如果不是更多,我将需要执行其中的大约10个:

USE [Apollo]
GO
/****** Object:  StoredProcedure [dbo].[spCancellationDetailInsert]    Script Date:       01/10/2012 10:21:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/* ************************* INSERT *************************/
/* Auto Generated 11/29/2006 7:28:53 PM by Object Builder */
/* ************************* INSERT *************************/


 ALTER Procedure [dbo].[spCancellationDetailInsert]
@StampUser char (10),
    @CancellationDetailID int,
    @RefundAmount float,
    @OldEndDate datetime,
    @EffectiveDate datetime,
    @CancelDate datetime,
    @ReasonCodeProgKey nvarchar (50)

As



/* insert CancellationDetail record     */
Insert [CancellationDetail]
(
    RefundAmount,
    OldEndDate,
    EffectiveDate,
    CancelDate,
    ReasonCodeProgKey
)
Values
(
    @RefundAmount,
    @OldEndDate,
    @EffectiveDate,
    @CancelDate,
    @ReasonCodeProgKey
)
If @@Error <> 0 GoTo InsertErrorHandler

/* save the key of the new row created by the insert */
Select @CancellationDetailID = Scope_Identity()

/* add audit record  */
Insert CancellationDetailAudit
(StampUser,
StampDateTime,
StampAction,
CancellationDetailID,
RefundAmount,
OldEndDate,
EffectiveDate,
CancelDate,
ReasonCodeProgKey)

Values
(@StampUser ,
GetDate() ,
'I',
@CancellationDetailID,
@RefundAmount,
@OldEndDate,
@EffectiveDate,
@CancelDate,
@ReasonCodeProgKey)

If @@Error <> 0 GoTo AuditInsertErrorHandler

Select
 CancellationDetailID = @CancellationDetailID

Return (0)
InsertErrorHandler:
Raiserror ('SQL Error whilst inserting CancellationDetailrecord: Error Code %d',17,1,@@Error)
With Log
Return  (99)

AuditInsertErrorHandler:
Raiserror ('SQL Error whilst inserting audit record for CancellationDetailInsert: Error Code %d',17,1,@@Error)
With Log
Return  (99)

2 个答案:

答案 0 :(得分:1)

如果你问的是我的想法 -

存储过程可以包含(几乎)任何有效的SQL语句。这包括返回多个结果集,执行多个更新以及调用其他存储过程。

例如:

CREATE PROCEDURE usp_Sample AS

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
SELECT * FROM INFORMATION_SCHEMA.TABLES

UPDATE Users SET Active = 0 WHERE ExpiredDate < GetDate()

SELECT Active, COUNT(*) FROM Users GROUP BY Active

EXEC usp_Sample2

GO

显然,这是一个相当人为的例子,但假设存在所有物体,它运行得非常好。

答案 1 :(得分:0)

为了在同一时间执行更多查询,您只需在选择后附加它们。

所以你可以做到

Select *
From table1

Select *
From table2

Select *
From table3

根据需要多次,他们都将独立执行。

如果你想根据SELECT进行更新,你通常会这样做:

UPDATE table1
WHERE ID IN (SELECT ID FROM TABLE2)

关于您的存储过程,如果您发布了更多详细信息,将会有所帮助。