Solved

MSSQL UPDATE command when SELECT is joining multiple tables from separate databases

Posted on 2013-05-22
4
256 Views
Last Modified: 2013-05-28
I have a SQL select pulling data from 3 different views in 2 different databases:

SELECT A.[ItemKey], A.[TagID], A.[PersonKey], A.[PersonSite], A.[ItemClass], A.[Location], A.[ItemStatus], A.[Manufacturer], A.[Model], A.[SerialNumber], A.[SystemName], A.[PersonID], A.[CompanyName], A.[SiteName], A.[ItemType], B.[CarrierName], B.[CarrierID], B.[TrackingNum], B.[Notes], C.[FirstName], C.[LastName]
FROM DATABASE.dbo.[View_OPS_ItemTags] A
INNER JOIN DATABASE2.dbo.[View_ItemTracker] B ON A.[ItemKey] = B.[ItemKey]
INNER JOIN DATABASE.dbo.[View_UserList] C ON C.[UserKey] = B.[UserKey]
WHERE ((A.[PersonID] = @PersonID) AND (A.[ItemStatus] = @ItemStatus)) ORDER BY A.[TagID]

This works great and display the data exactly how I need but I'm having trouble creating the controls to update the items in the GridView I have generated. I can create a separate gridview using just tables and modify the data with standard gridview edit controls but when I use the above select command and try to create the update string it either does nothing or errors out on the page:

IE:
UPDATE DATABASE2.dbo.[Tracking] A SET [UserKey] = @UserKey, [ItemKey] = @ItemKey, [TicketID] = @TicketID, [StatusID] = @StatusID, [Notes] = @Notes, [CarrierID] = @CarrierID, [TrackingNum] = @TrackingNum, [ReasonID] = @ReasonID WHERE [TrackID] = @original_TrackID

I'm relatively new to this and have no clue where to go from here since most examples online use a small single database that I'm having trouble translating over to what I'm trying to accomplish. Personally, I'd prefer to have this done in a code behind but I'll take whatever I can get at this point.

Full code:

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="DATABASE" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="ItemKey" HeaderText="ItemKey" SortExpression="ItemKey" ReadOnly="true"/>
        <asp:BoundField DataField="TagID" HeaderText="TagID" SortExpression="TagID" ReadOnly="true"/>
        <asp:BoundField DataField="PersonKey" HeaderText="PersonKey" SortExpression="PersonKey" ReadOnly="true"/>
        <asp:BoundField DataField="PersonSite" HeaderText="PersonSite" SortExpression="PersonSite" ReadOnly="true"/>
        <asp:BoundField DataField="ItemClass" HeaderText="ItemClass" SortExpression="ItemClass" ReadOnly="true"/>
        <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" ReadOnly="true"/>
        <asp:BoundField DataField="ItemStatus" HeaderText="ItemStatus" SortExpression="ItemStatus" ReadOnly="true"/>
        <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer" SortExpression="Manufacturer" ReadOnly="true"/>
        <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" ReadOnly="true"/>
        <asp:BoundField DataField="SerialNumber" HeaderText="SerialNumber" SortExpression="SerialNumber" ReadOnly="true"/>
        <asp:BoundField DataField="SystemName" HeaderText="SystemName" SortExpression="SystemName" ReadOnly="true"/>
        <asp:BoundField DataField="PersonID" HeaderText="PersonID" SortExpression="PersonID" ReadOnly="true"/>
        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" ReadOnly="true"/>
        <asp:BoundField DataField="SiteName" HeaderText="SiteName" SortExpression="SiteName" ReadOnly="true"/>
        <asp:BoundField DataField="ItemType" HeaderText="ItemType" SortExpression="ItemType" ReadOnly="true"/>
        <asp:BoundField DataField="CarrierName" HeaderText="CarrierName" SortExpression="CarrierName" />
        <asp:BoundField DataField="CarrierID" HeaderText="CarrierID" SortExpression="CarrierID" ReadOnly="true"/>
        <asp:BoundField DataField="TrackingNum" HeaderText="TrackingNum" SortExpression="TrackingNum" />
        <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" ReadOnly="true"/>
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" ReadOnly="true" />
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:SqlDataSource runat="server" ID="DATABASE" 
    ConnectionString="<%$ ConnectionStrings:DATABASE %>" 
    SelectCommand="SELECT A.[ItemKey], A.[TagID], A.[PersonKey], A.[PersonSite], A.[ItemClass], A.[Location], A.[ItemStatus], A.[Manufacturer], A.[Model], A.[SerialNumber], A.[SystemName], A.[PersonID], A.[CompanyName], A.[SiteName], A.[ItemType], B.[CarrierName], B.[CarrierID], B.[TrackingNum], B.[Notes], C.[FirstName], C.[LastName] FROM DATABASE.dbo.[View_OPS_ItemTags] A INNER JOIN DATABASE2.dbo.[View_ItemTracker] B ON A.[ItemKey] = B.[ItemKey] INNER JOIN DATABASE.dbo.[View_UserList] C ON C.[UserKey] = B.[UserKey] WHERE ((A.[PersonID] = @PersonID) AND (A.[ItemStatus] = @ItemStatus)) ORDER BY A.[TagID]"
    UpdateCommand="UPDATE DATABASE2.dbo.[Tracking] A SET [UserKey] = @UserKey, [ItemKey] = @ItemKey, [TicketID] = @TicketID, [StatusID] = @StatusID, [Notes] = @Notes, [CarrierID] = @CarrierID, [TrackingNum] = @TrackingNum, [ReasonID] = @ReasonID WHERE [TrackID] = @original_TrackID"
    DeleteCommand="DELETE FROM DATABASE2.dbo.[Tracking] WHERE [TrackID] = @original_TrackID"
    >
	<DeleteParameters>
		<asp:parameter Name="original_TrackID" Type="Int32" />
	</DeleteParameters>
	<InsertParameters>
		<asp:parameter Name="UserKey" Type="Int32" />
		<asp:parameter Name="ItemKey" Type="Int32" />
		<asp:parameter Name="TicketID" Type="Int32" />
		<asp:parameter Name="StatusID" Type="Int32" />
		<asp:parameter Name="Notes" Type="String" />
		<asp:parameter Name="CarrierID" Type="Int32" />
		<asp:parameter Name="TrackingNum" Type="String" />
		<asp:parameter Name="ReasonID" Type="Int32" />
	</InsertParameters>
	<UpdateParameters>
		<asp:parameter Name="UserKey" Type="Int32" />
		<asp:parameter Name="ItemKey" Type="Int32" />
		<asp:parameter Name="TicketID" Type="Int32" />
		<asp:parameter Name="StatusID" Type="Int32" />
		<asp:parameter Name="Notes" Type="String" />
		<asp:parameter Name="CarrierID" Type="Int32" />
		<asp:parameter Name="TrackingNum" Type="String" />
		<asp:parameter Name="ReasonID" Type="Int32" />
		<asp:parameter Name="original_TrackID" Type="Int32" />
	</UpdateParameters>

	<SelectParameters>
		<asp:parameter DefaultValue="COMP" Name="PersonID" Type="String" />
		<asp:parameter DefaultValue="Active" Name="ItemStatus" Type="String" />
	</SelectParameters>
</asp:SqlDataSource>

Open in new window


I'll eventually want to add some kind of control to insert new rows into the second database and remove rows if needed.

FYI, DATABASE.dbo.* is read only where DATABASE2.dbo.* is the one I need to write to.

Thanks.
0
Comment
Question by:BigDeer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39189079
>I have a SQL select pulling data from 3 different views in 2 different databases:
>but I'm having trouble creating the controls to update the items

<wild guess>
Last I remember a view is not updateable when there are more than two tables in a JOIN.
0
 
LVL 3

Author Comment

by:BigDeer
ID: 39189098
Well, that would be a problem.

Is there a way in a code behind to use a separate select and update to modify the data from table to table instead of directly updating the view?
0
 
LVL 3

Author Comment

by:BigDeer
ID: 39193487
I've requested that this question be closed as follows:

Accepted answer: 0 points for BigDeer's comment #a39189098

for the following reason:

You're correct. I figured out how to build the update query automatically from SSMS and using the SELECT to join the tables I needed to create my view so I didnt have the 3 table uneditable view in my update command. Many thanks.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question