Solved

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

Posted on 2013-05-22
4
249 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
  • 2
4 Comments
 
LVL 65

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.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now