2011-10-20 82 views
2

在我的情況下,我需要使用存儲過程方面的一些幫助:我有一個帶有單個XML列的表,其中包含字段VoucherCodeQuantity,SQL xml列中的數據看起來像這樣的:在存儲過程中使用XML插入和更新

<CampaignVoucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" VoucherCode="Vouch001" Quantity="2" /> 

下面的方法將調用我的存儲過程,如果一個特定憑證存在基於我的優惠券代碼檢查,然後無論是在我的GridView中添加一個新行或更新現有的憑證:

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click 
     Dim dbCommand As DbCommand = Nothing 
     'Dim cmd As New SqlCommand() 

     If TextBox1.Text = "" Or DropDownList1.SelectedIndex = 0 Then 
      Exit Sub 
     End If 

     Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Test").ConnectionString) 
     Dim da As New SqlDataAdapter("SELECT CustomerID, VoucherXML FROM Customers", con) 

     Dim cmd As New SqlCommand("Campaign_InsertRewardsVoucher_XML", con) 
     cmd.CommandType = CommandType.StoredProcedure 
     Dim dt As New DataTable() 
     da.Fill(dt) 

     ' Here we'll add a blank row to the returned DataTable 
     Dim dr As DataRow = dt.NewRow() 
     dt.Rows.InsertAt(dr, 0) 
     'Creating the first row of GridView to be Editable 
     GridView1.EditIndex = 0 
     GridView1.DataSource = dt 
     GridView1.DataBind() 
     'Changing the Text for Inserting a New Record 
     DirectCast(GridView1.Rows(0).Cells(0).Controls(0), LinkButton).Text = "Insert" 



     ' Serialization ---------------------------- 

     Dim cm As New CampaignVoucher(DropDownList1.SelectedValue, TextBox1.Text) 
     Dim serializer As New XMLserializer(cm.[GetType]()) 
     Dim memoryStream As New MemoryStream() 
     Dim writer As New XmlTextWriter(memoryStream, Encoding.UTF8) 

     serializer.Serialize(writer, cm) 

     'get the stream from the writer 
     memoryStream = TryCast(writer.BaseStream, MemoryStream) 

     'apply encoding to the stream 
     Dim enc As New UTF8Encoding() 
     Dim xml As String = enc.GetString(memoryStream.ToArray()).Trim() 

     ' -------------------------------------------  



     cmd.Parameters.Add("@p1", SqlDbType.VarChar, 50).Value = DropDownList1.SelectedValue 
     cmd.Parameters.Add("@p2", SqlDbType.Text).Value = xml 

     cmd.Connection = con 
     con.Open() 
     cmd.ExecuteScalar() 
     con.Close() 

     GridView1.EditIndex = -1 
     BindData() 

     TextBox1.Text = "" 

    End Sub 

回到頂部le,我寫了這個工作存儲過程,如下所示,只是爲了傳統存儲的目的:1個表包含VoucherCodeQuantity列,現在使用XML列,現在封裝了VoucherCodeQuantity值,我是輸給如何重寫我的存儲過程,嘗試不同的方法,但顯然我改壞了的話,請諮詢,謝謝!:

ALTER PROCEDURE [dbo].[Campaign_InsertRewardsVoucher] 
    @VoucherCode nvarchar(50) =NULL, 
    @Quantity   int = NULL 
    [email protected] int = NULL 

AS 
BEGIN 
DECLARE @ExistingQuantity Int = Null 
IF EXISTS (SELECT * FROM ForTest_Campaign_Voucher WHERE [email protected]) 

BEGIN 
    SET @ExistingQuantity = (SELECT Quantity from ForTest_Campaign_Voucher Where [email protected]) 
    SET @ExistingQuantity = (@ExistingQuantity + @Quantity) 

    UPDATE ForTest_Campaign_Voucher SET [email protected], [email protected] Where [email protected] 
END 
ELSE 
    INSERT INTO ForTest_Campaign_Voucher(VoucherCode, Quantity) VALUES(@VoucherCode, @Quantity) 
END 

回答

0
ALTER PROCEDURE [dbo].[Campaign_InsertRewardsVoucher] 
    @VoucherCode nvarchar(50) =NULL, 
    @Quantity   int = NULL 
AS 
    BEGIN 
     DECLARE @ExistingQuantity Int  
     SET @ExistingQuantity = (SELECT xmlFieldName.value('(/CampaignVoucher/@Quantity)[1]', 'int') 
           FROM ForTest_Campaign_Voucher 
           WHERE xmlFieldName.value('(/CampaignVoucher/@VoucherCode)[1]', 'nvarchar(50)') = @VoucherCode) 
     IF @ExistingQuantity IS NULL 
      BEGIN  
       INSERT INTO ForTest_Campaign_Voucher 
        (
         xmlFieldName 
        ) 
       VALUES 
        (
         '<CampaignVoucher xmlns:xsd="http://www.w3.org/2001/XMLSchema" VoucherCode="' + @VoucherCode + '" Quantity="' + CAST(@Quantity AS NVARCHAR(16)) + '" />' 
        ) 
      END 
     ELSE 
      DECLARE @NewQuantity INT 
      SET @NewQuantity = @ExistingQuantity + @Quantity 
      UPDATE ForTest_Campaign_Voucher 
      SET xmlFieldName='<CampaignVoucher xmlns:xsd="http://www.w3.org/2001/XMLSchema" VoucherCode="' + @VoucherCode + '" Quantity="' + CAST(@NewQuantity AS NVARCHAR(16)) + '" />' 
      WHERE xmlFieldName.value('(/CampaignVoucher/@VoucherCode)[1]', 'nvarchar(50)') = @VoucherCode 
    END 
GO