在表中的每一行上使用存储过程

时间:2016-02-11 21:48:48

标签: sql sql-server

我有一些包含一些帐户和供应商的表格。我需要在每一行上运行一个存储过程,将它们添加到其他表中。

我正在阅读的所有东西都说不要使用循环,这是有道理的。但有人可以告诉我如果没有它们我会怎么做?

样本表

 AccountNum | VendorName
------------------------
1234       | Vendor1
2345       | Vendor2
3456       | Vendor1
4567       | Vendor3

然后我的存储过程如下。所以我需要将每行的帐号numbe rand供应商传入下面的存储过程。 (不要担心前提号码和用户,他们可能是所有人的N / A)。那么有没有办法在不循环每一行的情况下做到这一点?我正在使用SQL Server Management Studio 2014.干杯

USE [PEEL_ICEM]
GO
/****** Object:  StoredProcedure [dbo].[AddAccountNumber]    Script Date:     2/11/2016 3:59:16 PM ******/
SET ANSI_NULLS ON
GO 
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[AddAccountNumber]
@AccountNumber nvarchar(150),
@UserName nvarchar(100),    
@PremiseNumber nvarchar(150) = Null,
@VendorName nvarchar (100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

SET XACT_ABORT on

IF @VendorName IS NOT NULL
BEGIN
    DECLARE @CustomerID int
    DECLARE @LIPharseID int
    DECLARE @ExportLabelID int
    DECLARE @CustVenID  int
    DECLARE @AccountNo varchar(20)
    --Get the Customer ID with the user name
    SELECT @CustomerID = [Customers].[CustomerID]
    FROM [CustomerUsers]
    INNER JOIN [Customers] ON [Customers].[CustomerID] = [CustomerUsers].CustomerID
    INNER JOIN Users ON Users.UserID = [CustomerUsers].UserID WHERE Users.Username = @UserName
    print  @CustomerID 
    --Get the VendorID from the Vendor name, test
    DECLARE @VendorID int
    SELECT @VendorID = [VendorID]  FROM [Vendors] WHERE VendorName = @VendorName
    print  @VendorID    
    BEGIN TRAN
        --Check if the customer vendor combination already exists       
        SELECT @CustVenID = [CustomerVendorID] FROM [PEEL_ICEM].[dbo].[CustomerVendors] WHERE [CustomerID] = @CustomerID AND [VendorID] = @VendorID
        print  @CustVenID
        Select @AccountNo = [AccountNumber] From [PEEL_ICEM].[dbo].[AccountVendor] WHERE [CustomerVendorID] = @CustVenID AND [AccountNumber] = @AccountNumber
        print  @AccountNo
        IF @CustVenID IS NULL  AND @VendorID <> '' AND @CustomerID <> '' 
            BEGIN                
                INSERT INTO [CustomerVendors]
                       ([CustomerID]
                       ,[VendorID])
                 VALUES
                       (@CustomerID,@VendorID)      
                SELECT @CustVenID = SCOPE_IDENTITY()                
            END 
        print  @CustVenID
        if @AccountNo is Null
            begin
                IF @CustVenID IS NOT NULL AND @AccountNumber <> ''
                    BEGIN   
                        INSERT INTO [AccountVendor]
                        ([AccountNumber]
                        ,[CustomerVendorID]
                        ,[PremiseNumber])           
                        VALUES
                        (@AccountNumber,@CustVenID, @PremiseNumber)
                    END
                ELSE
                    BEGIN
                    SELECT 'ERROR' as Status
                END     
            End

    COMMIT TRAN 

    SELECT [VendorName],[AccountNumber],[PremiseNumber]     
    FROM [CustomerVendors]
    LEFT JOIN [AccountVendor] ON [CustomerVendors].[CustomerVendorID] = [AccountVendor].[CustomerVendorID]
    LEFT JOIN [Vendors] ON [Vendors].[VendorID] = [CustomerVendors].[VendorID]
    WHERE [CustomerVendors].CustomerID = @CustomerID
END

结束

0 个答案:

没有答案