I have a SQL server 7.0 database. Within it I am construsting a table with information to be used in a report (Crystal Reports). In order to make this as simple as possible I am putting all the required information into a single table. What I initially do is feed the key information into this table - for instance CustomerID, UserID. I then use code in my application to cycle through this data and update the entries according to the key information - i.e. I update the entry with UserName, UserSurname according to the UserID entry found. Similarly I update CustomerName, CustomerAddress1, CustomerAddress2 according to the entry in the CustomerID field. This is al done through the program and is taking some time. Is there a query I can run that will do this task for me.
I'm looking for something like :
UPDATE [RptTable] RT
SET RT.[CustomerName] = (SELECT C.[CustomerName] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]),
RT.[CustomerAddress1] = (SELECT C.[CustomerAddress1] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]),
RT.[CustomerAddress2] = (SELECT C.[CustomerAddress2] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]), ...
...and so on.
Is this the way to go about things - my impression of this is that I'm going to be accessing the [Customers] table more than once and will slow things down - is there a way to get the required information (on one foul swoop) [CustomerName], [CustomerAddress1], [CustomerAddress2], etc. from the [Customers] table and use the [CustomerID] link to perform the UPDATE.
It could be that I'm missing the obvious on this one ....
Can anyone offer me a pointer or two ?
Thanks in advance.
Steve
I'm looking for something like :
UPDATE [RptTable] RT
SET RT.[CustomerName] = (SELECT C.[CustomerName] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]),
RT.[CustomerAddress1] = (SELECT C.[CustomerAddress1] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]),
RT.[CustomerAddress2] = (SELECT C.[CustomerAddress2] FROM [Customers] C WHERE C.[CustomerID] = RT.[CustomerID]), ...
...and so on.
Is this the way to go about things - my impression of this is that I'm going to be accessing the [Customers] table more than once and will slow things down - is there a way to get the required information (on one foul swoop) [CustomerName], [CustomerAddress1], [CustomerAddress2], etc. from the [Customers] table and use the [CustomerID] link to perform the UPDATE.
It could be that I'm missing the obvious on this one ....
Can anyone offer me a pointer or two ?
Thanks in advance.
Steve