MYSQL UPDATE正在创建新记录

时间:2014-12-16 04:34:47

标签: sql-server stored-procedures

正如标题所说。当我在mysql中调用UPDATE方法时,它会更新单元格。但是用旧数据创建一个新行。

这是我使用的存储过程:

Procedure [dbo].[Insert_Invoice_And_Insert_Address]
@UserID int,
@Occasion nvarchar(50),
@FlowerName nvarchar(50),
@FirstName nvarchar(50),
@LastName nvarchar(50),
@StreetAddress nvarchar(50),
@Suite nvarchar(50),
@City nvarchar(50),
@Country nvarchar(50),
@Province nvarchar(50),
@PostalCode nvarchar(50)
as
DECLARE @TaxCode money
DECLARE @InvoiceID int
DECLARE @FlowerID int
DECLARE @AddressID int
DECLARE @CountryID int
DECLARE @ProvinceID int


SET @TaxCode = (SELECT [TaxRate]
                FROM [Flowers].[dbo].[TaxCode]
                where [Status] like 1)
                SELECT @TaxCode;

SET @CountryID = (SELECT [CountryID]
                FROM [Flowers].[dbo].[Country]
                WHERE [DisplayValue] like @Country)
                SELECT @CountryID

SET @ProvinceID = (SELECT [ProvinceID]
                FROM [Flowers].[dbo].[Province]
                WHERE [Name] Like @Province)
                SELECT @ProvinceID

SET @FlowerID = (SELECT [FlowerID]
                FROM [Flowers].[dbo].[Flowers]
                where [Description] like @FlowerName)
                SELECT @FlowerID;

IF @FlowerID in (SELECT [Flower]
                FROM [Flowers].[dbo].[InvoiceDetails]
                WHERE [Processed] = 0 and [InvoiceID] = (SELECT [InvoiceID]
                                    FROM [Flowers].[dbo].[Invoices]
                                    WHERE [UserID] = @UserID and [Processed] = 0)) BEGIN
                SET @InvoiceID = (SELECT [InvoiceID]
                    FROM [Flowers].[dbo].[Invoices]
                    where [UserID] like @UserID and [Processed] like 0);
                    SELECT @InvoiceID;
                UPDATE [Flowers].[dbo].[InvoiceDetails]
                    SET [ItemNumber] = [ItemNumber] + 1
                    WHERE InvoiceID like @InvoiceID and [Processed] like 0 and Flower like @FlowerID;
                END
                ELSE
INSERT INTO [Flowers].[dbo].[Invoices]
            ([UserID]
            ,[TaxCode]
            ,[Processed]
            )
        VALUES
            (@UserID
            ,@TaxCode
            ,0
            )

INSERT INTO [Flowers].[dbo].[Addresses]
            ([Street]
            ,[apt]
            ,[City]
            ,[Province]
            ,[Country]
            ,[PostalCode]
            )
            VALUES
            (@StreetAddress
            ,@Suite
            ,@City
            ,@ProvinceID
            ,@CountryID
            ,@PostalCode
            )

SET @AddressID = (SELECT [AddressID]
                FROM [Flowers].[dbo].[Addresses]
                where [PostalCode] like @PostalCode and [Street] like @StreetAddress)
                SELECT @AddressID;

SET @InvoiceID = (SELECT [InvoiceID]
                FROM [Flowers].[dbo].[Invoices]
                where [UserID] like @UserID and [Processed] like 0);
                SELECT @InvoiceID;

INSERT INTO [Flowers].[dbo].[InvoiceDetails]
            ([ItemNumber]
            ,[Flower]
            ,[Processed]
            ,[DeliveryAddress]
            ,[InvoiceID]
            )
        VALUES
            (1
            ,@FlowerID
            ,0
            ,@AddressID
            ,@InvoiceID
            )

它的用途是订单页面。并且为了向您的购物篮添加更多数量,您最终只需发送与您第一次订购相关的信息。我使用IF ELSE语句来确定这是否是第一个订单,或者它是否添加更多数量。 因为你想知道为什么会这样做。它的学校项目。他们特别要求这样做。

编辑:

以下是我遇到问题的代码snipet:

UPDATE [Flowers].[dbo].[InvoiceDetails]
                    SET [ItemNumber] = [ItemNumber] + 1
                    WHERE InvoiceID like @InvoiceID and [Processed] like 0 and Flower like @FlowerID;

1 个答案:

答案 0 :(得分:0)

请在您的脚本中尝试此修改(您错过了您的BEGIN .. END在您的其他块中(3个语句):

(...)

IF @FlowerID in (SELECT [Flower]
                    FROM [Flowers].[dbo].[InvoiceDetails]
                    WHERE [Processed] = 0 and [InvoiceID] = (SELECT [InvoiceID]
                                        FROM [Flowers].[dbo].[Invoices]
                                        WHERE [UserID] = @UserID and [Processed] = 0)) 
    BEGIN
                    SET @InvoiceID = (SELECT [InvoiceID]
                        FROM [Flowers].[dbo].[Invoices]
                        where [UserID] like @UserID and [Processed] like 0);
                        SELECT @InvoiceID;
                    UPDATE [Flowers].[dbo].[InvoiceDetails]
                        SET [ItemNumber] = [ItemNumber] + 1
                        WHERE InvoiceID like @InvoiceID and [Processed] like 0 and Flower like @FlowerID;
   END
ELSE
   BEGIN
        INSERT INTO [Flowers].[dbo].[Invoices]
                ([UserID]
                ,[TaxCode]
                ,[Processed]
                )
            VALUES
                (@UserID
                ,@TaxCode
                ,0
                )

    INSERT INTO [Flowers].[dbo].[Addresses]
                ([Street]
                ,[apt]
                ,[City]
                ,[Province]
                ,[Country]
                ,[PostalCode]
                )
                VALUES
                (@StreetAddress
                ,@Suite
                ,@City
                ,@ProvinceID
                ,@CountryID
                ,@PostalCode
                )

    SET @AddressID = (SELECT [AddressID]
                    FROM [Flowers].[dbo].[Addresses]
                    where [PostalCode] like @PostalCode and [Street] like @StreetAddress)
                    SELECT @AddressID;

    SET @InvoiceID = (SELECT [InvoiceID]
                    FROM [Flowers].[dbo].[Invoices]
                    where [UserID] like @UserID and [Processed] like 0);
                    SELECT @InvoiceID;

    INSERT INTO [Flowers].[dbo].[InvoiceDetails]
                ([ItemNumber]
                ,[Flower]
                ,[Processed]
                ,[DeliveryAddress]
                ,[InvoiceID]
                )
            VALUES
                (1
                ,@FlowerID
                ,0
                ,@AddressID
                ,@InvoiceID
                )
    END