Using a LEFT OUTER JOIN WHERE then updating same data - sql

I have two databases running on MSSQL 2005, SOURCE and DESTINATION, which have the same structure and tables in them.
I'm trying to update data from s to d.
In this example, I'm trying to copy data from s to d using a join and only bringing across entries which aren't already in d.
I'm then trying to update the same records just inserted with vales thus:
INSERT DESTINATION.ITEM_REPLENISH_VENDOR ([ITEM_CODE],[VEND_CODE],[PRIMARY_VENDOR],[PURCHASE_MEASURE],[STD_COST],[LAST_COST],[EOQ],[VENDOR_PART_NO],[LEAD_TIME],[COST])
SELECT s.[ITEM_CODE],s.[VEND_CODE],s.[PRIMARY_VENDOR],s.[PURCHASE_MEASURE],s.[STD_COST],s.[LAST_COST],s.[EOQ],s.[VENDOR_PART_NO],s.[LEAD_TIME], s.[COST] FROM SOURCE.dbo.ITEM_REPLENISH_VENDOR s
LEFT OUTER JOIN DESTINATION.dbo.ITEM_REPLENISH_VENDOR d ON (d.ITEM_CODE = s.ITEM_CODE)
WHERE d.ITEM_CODE IS NULL
UPDATE DESTINATION.dbo.ITEM_REPLENISH_VENDOR
SET VEND_CODE='100004', PRIMARY_VENDOR='T',STD_COST='0',LAST_COST='0',COST='0'
WHERE
My issue is once I reach the second WHERE I don't know how to refer to the data I've just updated. This script is going to run either every day at a set time and I don't want to overwrite that whole column with those values, just the entries that have been inserted on this execution.

It looks like you want the output clause This will let you stash away the inserted values.
-- item_code needs to have the same type as the source table
declare #inserted table (item_code int not null primary key);
insert destination.item_replenish_vendor (
[item_code], [vend_code], [primary_vendor],
[purchase_measure], [std_cost], [last_cost],
[eoq], [vendor_part_no], [lead_time],[cost]
) -- save inserted values
output
inserted.item_code into #inserted
select
s.[item_code], s.[vend_code], s.[primary_vendor],
s.[purchase_measure], s.[std_cost], s.[last_cost],
s.[eoq], s.[vendor_part_no], s.[lead_time], s.[cost]
from
source.dbo.item_replenish_vendor s
left outer join
destination.dbo.item_replenish_vendor d
on d.item_code = s.item_code
where
d.item_code is null;
update
d
set
vend_code = '100004',
primary_vendor = 'T',
std_cost = '0',
last_cost = '0,
cost = '0'
from
destination.dbo.item_replenish_vendor d
inner join
#inserted i
on d.item_code = i.item_code;
In this case, you could just put constant values in the insert statement, instead of doing things in two steps...

In the example you have:
UPDATE DESTINATION.dbo.ITEM_REPLENISH_VENDOR
SET VEND_CODE='100004', PRIMARY_VENDOR='T',STD_COST='0',LAST_COST='0',COST='0'
If your VEND_CODE, PRIMARY_VENDOR, STD_COST, LAST_COST, COST are always going to be a static value, you could just put them into the first query.
INSERT DESTINATION.ITEM_REPLENISH_VENDOR ([ITEM_CODE],[VEND_CODE],[PRIMARY_VENDOR],[PURCHASE_MEASURE],[STD_COST],[LAST_COST],[EOQ],[VENDOR_PART_NO],[LEAD_TIME],[COST])
SELECT s.[ITEM_CODE],'100004','T',s.[PURCHASE_MEASURE],'0','0',s.[EOQ],s.[VENDOR_PART_NO],s.[LEAD_TIME], '0'
FROM SOURCE.dbo.ITEM_REPLENISH_VENDOR s
LEFT OUTER JOIN DESTINATION.dbo.ITEM_REPLENISH_VENDOR d ON (d.ITEM_CODE = s.ITEM_CODE)
WHERE d.ITEM_CODE IS NULL
but if they do need to be calculated after insert, then I agree with Laurence's approach.

Related

SQL Loop and update table with sum

I have the following shiftdate table that stores the amount of hours worked at rate1 and rate2 (these are stored in another table). I want to use this data to update the shiftdateTotal field with the total value. Hoping you can help?
SHIFTDATE Table
shiftdateID int
hourlyRate1ID int
hoursRate1 int
hourlyRate2ID int
hoursRate2 int
shiftDateTotal decimal
LISTITEM Table
ListitemID int
description string
rateOfPay decimal
Example listItem row (23, BankHolidayRate, 12.50)
I'm able to create a SQL select that does the required calculation, see below:
SELECT shiftDateID,
hoursRate1, t2.rateOfPay as rate1,
hoursRate2, t3.rateOfPay as rate2,
(hoursRate1 * t2.rateOfPay) + ([hoursRate2] * t3.rateOfPay) as myTotal
from dbo.ShiftDate t1
inner join dbo.ListItem t2
on t1.hourlyRate1ID = t2.listItemID
inner join dbo.ListItem t3
on t1.hourlyRate2ID = t3.listItemID
But now i'm a bit lost.. Using this query or something similar how can I insert the myTotal value into the shiftDateTotal for the required shiftdateID row?
Can i store this into a temp table, then loop through the data and update the SHIFTDATE table with myTotal?
You can just turn this into an update. The transformation is pretty direct:
update sd
set shiftDateTotal = (sd2.hoursRate1 * li1.rateOfPay) + (sd2.hoursRate2 * li2.rateOfPay)
from dbo.ShiftDate sd inner join
dbo.ListItem li1
on sd.hourlyRate1ID = li1.listItemID inner join
dbo.ListItem li2
on sd.hourlyRate2ID = li2.listItemID;
Notes:
I replaced the meaningless table aliases (t1, t2, t3) with more meaningful ones (abbreviations for the table names).
I qualified all column names.
You might want to be careful, in case any of the rate ids are NULL/missing in the original table. In that case, you will want left joins and coalesce()s.

How to update a column in a table from a select query?

I have a query, but i want the results of the query to be updated to another table, in 3rd, 4th, 5th and 6th column because i already have values in the first and second column.
my query:
INSERT INTO customer_info (reader_ID,tag_no,area,max_timestamp)
SELECT aisle_info.reader_ID,tag_no,
aisle_info.area,
TIMESTAMPDIFF(SECOND,MIN(timestamp),MAX(timestamp)) AS MAXTIME
FROM tag_logs
INNER JOIN aisle_info ON
tag_logs.reader_ID = aisle_info.reader_ID
WHERE tag_no = 515988190124;
1364 - Field 'Customer_name' doesn't have a default value
In TSQL - you should write an update statement instead of insert into select as you are updating (adding) values to an existing row in table customer_info. The pseudo code should look like:
Update C
set C.reader_ID = A.reader_ID,
C.tag_no = A.tag_no,
C.area = A.area,
C.max_timestamp = TIMESTAMPDIFF(SECOND,MIN(T.timestamp),MAX(T.timestamp))
from customer_info C
join aisle_info A on A. tag_no = C. tag_no --Change here for correct join condition
join tag_logs T ON T.reader_ID = A.reader_ID
WHERE T.tag_no = 515988190124;

Sql Query with Temp tables in Visual Studio 2010 data grid

I apologize up front if this has been answered, i read several other posts and didn't see a clear answer to my question. I am a beginner at VS2010. Basically i have the below query and i want it to display in the data grid view when i run my program.
I can use VS2010 to join two actual tables but as you can see below the temp tables are very important.
IF OBJECT_ID('tempdb..#tempbatch') IS NOT NULL DROP TABLE #tempbatch
IF OBJECT_ID('tempdb..#tempbatch2') IS NOT NULL DROP TABLE #tempbatch2
IF OBJECT_ID('tempdb..#tempbatch1') IS NOT NULL DROP TABLE #tempbatch1
IF OBJECT_ID('tempdb..#tempbatch3') IS NOT NULL DROP TABLE #tempbatch3
IF OBJECT_ID('tempdb..#tempbatch4') IS NOT NULL DROP TABLE #tempbatch4
IF OBJECT_ID('tempdb..#tempbatch5') IS NOT NULL DROP TABLE #tempbatch5
IF OBJECT_ID('tempdb..#tempbatch6') IS NOT NULL DROP TABLE #tempbatch6
IF OBJECT_ID('tempdb..#tempbatch7') IS NOT NULL DROP TABLE #tempbatch7
IF OBJECT_ID('tempdb..#tempbatch8') IS NOT NULL DROP TABLE #tempbatch8
IF OBJECT_ID('tempdb..#tempbatch9') IS NOT NULL DROP TABLE #tempbatch9
IF OBJECT_ID('tempdb..#tempbatch10') IS NOT NULL DROP TABLE #tempbatch10
create table #tempbatch (rowid bigint primary key identity(1,1), shipmentno varchar(64))
insert into #tempbatch select * from #unitnames
select distinct b.dcsID, a.BoxType, b.BoxNO, b.shipmentno, b.PaletteWithinShipment into #tempbatch1 from #tempbatch c
join dva..Boxmapping as a
on c.shipmentno = a.shipmentno
join dva..Boxmapping as b
on a.boxno = b.BoxNO
--where b.shipmentno = '#rmn'
group by b.dcsID, a.BoxType, b.BoxNO, b.shipmentno, b.PaletteWithinShipment
order by b.PaletteWithinShipment, b.BoxNO
--select dcsid,boxtype,boxno,shipmentno from #tempbatch1
select distinct a.dcsid,b.dcsid as manifestDCS,b.rmn into #tempbatch3 from #tempbatch1 a
left outer join dva..manifestDCS b
on a.dcsid = b.dcsid
select distinct b.dcsid,a.rmn into #tempbatch5 from #tempbatch3 a
left outer join dva..manifestdcs b
on a.rmn = b.rmn
select b.dcsid as deliverexDCSID,a.dcsid,a.rmn,pbatch into #tempbatch4 from #tempbatch5 a
left outer join #tempbatch1 b
on a.dcsid = b.dcsid
join dva..document c
on a.dcsid = c.dcsid
where a.dcsid not in (select dcsid from dva..document where ftpstime is null) and a.dcsid not in (select dcsid from dva..boxmapping)
delete from #tempbatch4 where deliverexdcsid is not null
----- ******************************** START OF SECOND QUERY *********************************-------------
select * into #tempbatch6 from dva..Boxmapping
select distinct c.rmn,c.dcsid,b.dcsid as BoxDCSID,a.pbatch into #tempbatch8 from #tempbatch4 a
left outer join dva..manifestDCS b
on a.dcsid = b.dcsid
left outer join dva..manifestdcs c
on b.rmn = c.rmn
select distinct c.rmn,c.dcsid as Missing,a.dcsid,d.BoxNO,d.boxtype,e.palette,e.PaletteWithinShipment,e.shipmentno into #tempbatch9 from #tempbatch8 a
left outer join #tempbatch4 c
on a.rmn = c.rmn
left outer join dva..boxmapping d
on b.dcsid = d.dcsid
left outer join dva..boxmapping e
on d.boxno = e.boxno
delete from #tempbatch9 where dcsID is null
delete from #tempbatch9 where boxno is null
delete from #tempbatch9 where palette is null
select distinct rmn,missing,boxno,boxtype,PaletteWithinShipment,shipmentno from #tempbatch9
order by rmn,PaletteWithinShipment
Wrap the whole of your query in a stored procedure, i.e.
CREATE PROCEDURE dbo.MyData
AS
SET NOCOUNT ON;
BEGIN
<insert your SQL here>
END;
Back in Visual Studio you will need to open a connection to your database, then open a command that you will use in conjunction with a data reader. There should be plenty of examples of how to do this, but I will give you a very short sample (sorry it's in C#, I don't touch VB):
using (var con = new SqlConnection("Server=WHATEVERSERVER;Database=WHATEVERDBS;Trusted_Connection=True;"))
{
con.Open();
using (var com = con.CreateCommand())
{
com.CommantText = "EXECUTE <MyDatabase>.dbo.MyData;";
using (var dr = com.ExecuteReader())
{
while (dr.Read())
{
var row = new string[7];
row[0] = dr["rmn"].ToString();
row[1] = dr["missing"].ToString();
etc...
MyDataGrid.Rows.Add(row);
}
}
}
}
Finally, you can just pull back a row count from your data grid, to get the number of rows of data that you want to display in a window.
If you can't create a stored procedure then you could type the entire SQL script into the command text bit, but you would have to be very careful with the carriage returns, spacing, literals, quotes, etc.
Again, apologies that my example code is in C#, I was just wanting to give you a very basic framework, so at the very least you know what to go and type into Google...

SQL update with count gets only nulls

I am using SQL Server 2008 R2 and SSRS 2008 to generate the report. I am trying to update a table to get a count of the results from another table.
UPDATE r
SET r.Race = d2.Race,
r.RaceNum = d2.count
FROM #results r
INNER JOIN
(SELECT d.Race,
COUNT(d.race) AS 'count'
FROM #Demographic d
GROUP BY d.Race)d2 ON r.Race = d2.Race
An insert works perfectly but I am inserting several times into the results table to create a demographics report. This is creating a lot Null data at the table of the results. If anyone has an idea of what I am going wrong the help would be appreciated.
Not certain, but first guess is that count is a reserved word... change it to something else such as...
update r
set r.Race = d2.Race, r.RaceNum = d2.cnt
from #results r
inner join
(select d.Race, COUNT(d.race) as 'cnt' from #Demographic d group by d.Race)d2
on r.Race = d2.Race
or alternatively try delimiting the column identifier as [count] or if you have quoted identifiers on "count"
Your query is:
update r
set r.Race = d2.Race, /*<---------*/
r.RaceNum = d2.count
from #results r inner join
(select d.Race, COUNT(d.race) as 'count'
from #Demographic d
group by d.Race
) d2
on r.Race = d2.Race /*<-------- */
The two marked lines stand out. You are joining on values using =, then you are setting one equal to the other. This may not be the problem, but it is suspicious. If you are joining on them, why are you setting them equal? My suspicion is that the join is failing, because nothing matches it.
You don't need to update the race column, since that's the equality portion of your join.
As written, any race not in #demographic would have its racenum set to null. If you want 0 for those try:
UDPATE
r
SET
r.RaceNum = ISNULL(d2.ct, 0)
FROM #results r
LEFT JOIN
(
SELECT
d.Race
, COUNT(d.race) as ct
FROM
#Demographic d
GROUP BY
d.Race
) d2
ON
r.Race = d2.Race

How can I select a subset of columns from a table when relevant in an outer join?

select a.cust_xref_id, a.est_hour, a.phone_nbr as number, a.credit_calls, a.credit_rpcs, b.sdp_calls
from #temp0 a
full outer join #temp2 b
on a.cust_xref_id = b.sdp_cust_xref_id
and a.est_hour = b.sdp_hour
and a.phone_nbr = b.sdp_phone
Is there a way to get the data from table b with regard to sdp_cust_xref_id, sdp_hour, and sdp_phone when the data does not exist in both tables via the join? If b.sdp_calls does exist, the column values are null.
I read it a few more times and I think I know what you want. Try this. It will give you the values from table b if they are NULL in a:
select COALESCE(a.cust_xref_id, b.sdp_cust_xref_id) as cust_xref_id,
COALESCE(a.est_hour, b.spd_hour) as est_hour,
COALESCE(a.phone_nbr, b.spd_phone) as number,
a.credit_calls,
a.credit_rpcs,
b.sdp_calls
from #temp0 a
full outer join #temp2 b
on a.cust_xref_id = b.sdp_cust_xref_id
and a.est_hour = b.sdp_hour
and a.phone_nbr = b.sdp_phone