本文介绍了如何在单个更新查询中更新多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有三张桌子...... 1.Categorycrt productid(主键) productcategory Girthfrom GirthTo 2。 Itemcre Itemid(主键) Itemname 3.Pricefix PriceId(primarykey) Itemid(Itemcre的外键) Productid(Categorycrt的外键) Rate 我需要更新Pricefix表...请帮我查询更新pricefix tabletable ..... 我有pricefix表格的gridview 我的aspx页面 < asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False onrowcancelingedit = GridView1_RowCancelingEdit onrowdeleting = GridView1_RowDeleting onrowediting = GridView1_RowEditing onrowupdating = GridView1_RowUpdating > < 列 > < asp:TemplateField HeaderText = PriceId > < itemtemplate > < asp:标签 ID = lblPriceId runat = server 文字 =' <% #Eval( PriceId)%>' > '> < / itemtemplate > < asp:TemplateField HeaderText = ItemName > < itemtemplate > < asp:标签 ID = lblItemId runat = 服务器 文字 =' <% #Eval( Itemname)%>' > '> < / itemtemplate > < edititemtemplate > < asp:TextBox ID = txtItemId runat = server 文本 =' <% #Eval( 项目名称)%>' > '> < / edititemtemplate > < asp:TemplateField HeaderText = ProductCategory > < itemtemplate > < asp:标签 ID = lblPdtId runat = server 文本 =' <% #Eval( ProductCategory)%>' > '> < / itemtemplate > < edititemtemplate > < asp:TextBox ID = txtPdtId runat = server 文本 =' <% #Eval( ProductCategory)%>' > '> < / edititemtemplate > < asp:TemplateField HeaderText = Rate > < itemtemplate > < asp:标签 ID = lblRate runat = server 文字 =' <% #Eval( Rate)%>' > '> < / itemtemplate > < edititemtemplate > < asp:TextBox ID = txtRate runat = server 文本 =' <% #Eval( 评价)%>' > '> < / edititemtemplate > < asp:TemplateField HeaderText = 行动 > < itemtemplate > < asp:LinkButton ID = 编辑 runat = server CommandName = 编辑 文本 = 编辑 > < asp:LinkButton ID = 删除 runat = server CommandName = 删除 文本 = 删除 > < / itemtemplate > < edititemtemplate > < asp:LinkButton ID = 更新 runat = server CommandName的 = 更新 文本 = 更新 > < asp:LinkButton ID = 取消 runat = server CommandName = 取消 文本 = c ancel > < / edititemtemplate > < / columns > while点击所选行的网格更新和取消链接按钮的编辑按钮。我需要更新pricefix表的productcategory,itemname和rate.actually在pricefix表中存储为productcategory和itemname的foreignkey值的值。 ...实际上在更新pricefix的gridview时,更新的productcategory和itemname应该在其父表中更改,即Itemcre和Categorycrt以及rate inPricefix表本身... 解决方案 我创建了一个示例存储过程,其中包含多个更新查询。 CREATE procedure USP_TEST_Procedure @ pTYPE VARCHAR ( 10 )= ' ', @ EMP_ID VARCHAR ( 10 )= ' ', @ EMP_NAME VARCHAR ( 10 )= ' ', @ YMD VARCHAR ( 20 )= ' ', AS BEGIN DECLARE @ TERM_ST_DT DATETIME = getdate(), 声明 @ EMP_ROLE varchar ( 20 ); 如果 @ pTYPE = ' S1' BEgin 选择 @ EMP_ROLE = max(role_Name)来自 user_Roles 其中 emp_id = @ EMP_ID END ELSE BEGIN SET @ EMP_ROLE = ' Admin' END 更新 table1 set Role_name = @ EMP_ROLE 其中 emp_id = @ EMP_ID update table2 set emp_name = @ EMP_NAME,joinDate = @ YMD 其中 emp_id = @ EMP_ID insert into table3(empid,empname) values ( @ EMP_ID , @ EMP_NAME ) END - 执行程序 exec USP_TEST_Procedure ' s1',' 10001',' TESTUSER',' 2014-05-08' 您可以在存储过程中使用表类型变量。并在存储过程中传递全表.. 你可以使用该变量做任何事情(插入/更新/操作) 点击以下链接获取更多信息 http://www.sqlservercentral.com/blogs/steve_jones/2012/09/19/creating-a-user-defined-table-type/ [ ^ ] I have three tables......1.Categorycrtproductid(primary key) productcategory Girthfrom GirthTo2.ItemcreItemid(primary key)Itemname3.PricefixPriceId(primarykey)Itemid(foreign key of Itemcre)Productid(foreign Key of Categorycrt)RateI need to update the Pricefix table...please help me with the query for updating the pricefix tabletable.....I have a gridview for pricefix tableMy aspx page<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" > <columns> <asp:TemplateField HeaderText="PriceId"> <itemtemplate> <asp:Label ID="lblPriceId" runat="server" Text='<%#Eval("PriceId") %>'>'> </itemtemplate> <asp:TemplateField HeaderText="ItemName"> <itemtemplate> <asp:Label ID="lblItemId" runat="server" Text='<%#Eval("Itemname") %>'>'> </itemtemplate> <edititemtemplate> <asp:TextBox ID="txtItemId" runat="server" Text='<%#Eval("Itemname") %>'>' > </edititemtemplate> <asp:TemplateField HeaderText="ProductCategory"> <itemtemplate> <asp:Label ID="lblPdtId" runat="server" Text='<%#Eval("ProductCategory") %>'>'> </itemtemplate> <edititemtemplate> <asp:TextBox ID="txtPdtId" runat="server" Text='<%#Eval("ProductCategory") %>'>' > </edititemtemplate> <asp:TemplateField HeaderText="Rate"> <itemtemplate> <asp:Label ID="lblRate" runat="server" Text='<%#Eval("Rate") %>'>'> </itemtemplate> <edititemtemplate> <asp:TextBox ID="txtRate" runat="server" Text='<%#Eval("Rate") %>'>' > </edititemtemplate> <asp:TemplateField HeaderText="Action"> <itemtemplate> <asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit"> <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete"> </itemtemplate> <edititemtemplate> <asp:LinkButton ID="Update" runat="server" CommandName="Update" Text="Update"> <asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel" Text="cancel"> </edititemtemplate> </columns>while clicking on the edit button of the grid update and cancel link button appears for the selected row .i need to update the productcategory,itemname and rate of the pricefix table.actually in pricefix table the value stored as foreignkey value of productcategory and, itemname....actually while updating the gridview of pricefix the updated productcategory and itemname should be changed in their parent table i.e, Itemcre and Categorycrt and rate inPricefix table itself... 解决方案 I have created a sample Stored procedure which has more then one update query.CREATE procedure USP_TEST_Procedure @pTYPE VARCHAR(10) = '',@EMP_ID VARCHAR(10) = '',@EMP_NAME VARCHAR(10) = '',@YMD VARCHAR(20) = '',ASBEGINDECLARE @TERM_ST_DT DATETIME = getdate(),Declare @EMP_ROLE varchar(20);if @pTYPE='S1'BEginselect @EMP_ROLE=max(role_Name) from user_Roles where emp_id=@EMP_IDENDELSEBEGINSET @EMP_ROLE='Admin'ENDUpdate table1 set Role_name=@EMP_ROLE where emp_id=@EMP_IDupdate table2 set emp_name=@EMP_NAME,joinDate=@YMD where emp_id=@EMP_IDinsert into table3 (empid,empname) values(@EMP_ID,@EMP_NAME)END-- To execute the procedureexec USP_TEST_Procedure 's1','10001','TESTUSER','2014-05-08'You can use table type variable in Stored procedures. and pass full table in stored procedure..you can do any thing with that variable(insert/update/manipulation)follow the below link for more informationhttp://www.sqlservercentral.com/blogs/steve_jones/2012/09/19/creating-a-user-defined-table-type/[^] 这篇关于如何在单个更新查询中更新多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 20:45