As requested, edited to provide more details
I have a stored procedure (lets call it spOuter) along the following lines:
ALTER PROCEDURE [dbo].[spOuter]
(#SelFromDateUTC smalldatetime
,#SelToDateUTC smalldatetime
,#SelDriverId int = null
) AS
DECLARE #SelDriverName varchar(40)
Set Nocount on
exec dbo.spInner --<<<<<<<<<<<<<<<<<<<<<<<<<<<
Select #SelDriverName = DriverName
From dbo.tblDrivers
Where DriverID = #SelDriverId
Set Nocount off
Select #SelToDateUTC as #SelShiftDateUTC
,#SelDriverName as SelDriverName
, *
From dbo.vwRptDriverProgress
Where ActionTimeUTC between #SelFromDateUTC and #SelToDateUTC
and DriverId = coalesce(#SelDriverId, DriverId)
Order by DriverName,ActionTimeUTC,DriverLogId
When I run spInner from SSMS it does not return any result sets.
However, when I run it from spOuter I get two result sets, one from spInner and one from spOuter (commenting out the call to spInner removes the excess result set).
spInner is as follows:
ALTER PROCEDURE [dbo].[spInner] AS
set nocount on
Declare #CutoffDate smalldatetime
Set #CutoffDate = DateAdd(Month, -1, getUTCDate()) -- no need to keep reprocessing the whole table.
if #CutoffDate < '11/1/2008'
Set #CutoffDate = '11/1/2008'
Insert dbo.tblADIShifts (PU.DriverId, PU.PunchInTimeUTC, PU.PunchInLocationId)
Select DriverId, PunchInTimeUTC, PunchInLocationId
From vwUpdDriverLogsAddShifts PU -- Distinct Driver,PunchInTimeUTC combinations.
Where PU.PunchInTimeUTC > #CutoffDate
and not exists (Select *
from dbo.tblADIShifts SH
Where SH.DriverId=PU.DriverId
and SH.PunchInTimeUTc = PU.PunchInTimeUtC)
Order By PU.DriverId, PU.PunchInTimeUTC
Update dbo.tblADIShifts Set
PunchOutTimeUTC = PU.PunchOutTimeUTC
,PunchOutLocationId = PU.PunchOutLocationId
From vwUpdDriverLogsNewPunchOuts PU
Where dbo.tblADIShifts.ShiftId = PU.ShiftId and PU.PunchInTimeUTC > #CutoffDate
Insert dbo.tblDriverLogs (DriverId, ActionId, ActionTimeUTC, ShiftId, LocationId)
Select DriverId, ActionId, PunchInTimeUTC, ShiftId, PunchInLocationId
From dbo.vwUPDDriverLogsSP
Insert dbo.tblDriverLogs (DriverId, ActionId, ActionTimeUTC, ShiftId, LocationId)
Select DriverId, ActionId, PunchOutTimeUTC, ShiftId, PunchOutLocationId
From dbo.vwUPDDriverLogsFP
Update dbo.tblDriverLogs Set
ShiftId = SH.ShiftId
From dbo.vwUpdDriverLogsAssignShifts SH
Where SH.PunchInTimeUTC > #CutoffDate
and dbo.tblDriverLogs.DriverLogId = SH.DriverLogId
Update dbo.tblDriverLogs Set
ShiftId = PrevShiftId
From dbo.vwUpdDriverLogsShiftless3 SH
Where dbo.tblDriverLogs.DriverLogId = SH.DriverLogId
--<<<<<<<<< The bogus (and empty) result set has the columns
--<<<<<<<<< of tblMovementLocations which is only referenced here:
Update dbo.tblMovementLocations Set
Distance = CalcDistance
From vwExcessiveOrderDistances vw
Where dbo.tblMovementLocations.MovementLocationId = vw.MovementLocationId
Update dbo.tblDriverLogs Set
DriveTimeMinutes = VW.DriveTimeMinutes
,BreakTimeMinutes = VW.BreakTimeMinutes
,DelayTimeMinutes = VW.DelayTimeMinutes
,LocationId = VW.LocationId
From dbo.vwUpdDriverLogs VW
Where dbo.tblDriverLogs.DriverLogId = VW.DriverLogId
and VW.ActionTimeUTC > #CutoffDate
and (dbo.tblDriverLogs.DriveTimeMinutes <> VW.DriveTimeMinutes
or dbo.tblDriverLogs.BreakTimeMinutes <> VW.BreakTimeMinutes
or dbo.tblDriverLogs.DelayTimeMinutes <> VW.DelayTimeMinutes
or coalesce(dbo.tblDriverLogs.LocationId,-1) <> VW.LocationId)
Surely, these selects are part of the insert/update statements and should not return result sets?
Is there any way around this?
SQLServer 2005 SP2 Version 9.00.4035.00
Perhaps changing the syntax of your update statement(s) in spInner to use an alias (see Good way to use table alias in Update statement?) and a JOIN might change the behavior - something like this, for example:
UPDATE [locations]
SET Distance = CalcDistance
FROM dbo.tblMovementLocations AS [locations]
INNER JOIN vwExcessiveOrderDistances AS [vw]
ON [locations].MovementLocationId = [vw].MovementLocationId;
The thought process being that possibly there aren't any corresponding record(s) in vwExcessiveOrderDistances to be matched with in dbo.tblMovementLocations, and maybe this is causing the database engine to return an empty result set by treating the statement like a SELECT statement on dbo.tblMovementLocations. It would certainly be strange if this is the case!
This is just my speculation though...
Related
I've a simple stored procedure to update a table as follows:
This sp is updating the table properly. But when I execute select query on po_tran table, its hanging.
Is there any mistake in the stored procedure..?
alter procedure po_tran_upd #locid char(3)
as
SET NOCOUNT ON;
begin
update t
set t.lastndaysale = (select isnull(sum( qty)*-1, 0)
from exp_tran
where exp_tran.loc_id =h.loc_id and
item_code = t.item_code and
exp_tran.doc_date > dateadd(dd,-30,getdate() )
and exp_tran.doc_type in ('PI', 'IN', 'SR')),
t.stk_qty = (select isnull(sum( qty), 0)
from exp_tran
where exp_tran.loc_id =h.loc_id and
item_code = t.item_code )
from po_tran t, po_hd h
where t.entry_no=h.entry_no and
h.loc_id=#locid and
h.entry_date> getdate()-35
end
;
Try the following possible ways to optimize your procedure.
Read this article, where I have explained the same example using CURSOR, Here I also have updated a field of the table using CURSOR.
Important: Remove Subquery, As I can see you have used a subquery to update the field.
You can use Join or Save the result of your query in the temp variable and you can use that variable while update.
i.g
DECLARE #lastndaysale AS FLOAT
DECLARE #stk_qty AS INT
select #lastndaysale = isnull(sum( qty)*-1, 0) from exp_tran where exp_tran.loc_id =h.loc_id and
item_code = t.item_code and exp_tran.doc_date > dateadd(dd,-30,getdate() ) and exp_tran.doc_type in ('PI', 'IN', 'SR')
select #stk_qty = isnull(sum( qty), 0) from exp_tran where exp_tran.loc_id =h.loc_id and item_code = t.item_code
update t set t.lastndaysale =#lastndaysale,
t.stk_qty = #stk_qty
from po_tran t, po_hd h where t.entry_no=h.entry_no and h.loc_id=#locid and h.entry_date> getdate()-35
This is just a sample example you can do need full changes in that.
I added a possibly more performant update, however, I do not fully understand your question. If "any" query is running slow against the po_tran, then I suggest you examine the indexing on that table and ensure it has a proper clustered index. If "this" query is running slow then I suggest you look into "covering indexes". The two fields entry_no and item_code seem like good candidates to include in a covering index.
update t
set t.lastndaysale =
CASE WHEN e.doc_date > dateadd(dd,-30,getdate() AND e.doc_type in ('PI', 'IN', 'SR') THEN
isnull(sum(qty) OVER (PARTITION BY e.loc_id, t.item_code) *-1, 0)
ELSE 0
END,
t.stk_qty = isnull(SUM(qty) OVER (PARTITION BY e.loc_id, t.item_code),0)
from
po_tran t
INNER JOIN po_hd h ON h.entry_no=t.entry_no AND h.entry_date> getdate()-35
INNER JOIN exp_tran e ON e.loc_id = h.loc_id AND e.itesm_code = t.item_code
where
h.loc_id=#locid
After executing a stored procedure, I get the following error:
Msg 512, Level 16, State 1, Procedure sp_ActFTC, Line 64
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I have two tables in the database, FTC_Alt and FichaTecnicaComp, and I need to update the FichaTecnicaComp table on a given date.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_ActFTC]
AS
DECLARE #Codigo NVARCHAR(20),
#DataAlteracao DATE,
#Categoria NVARCHAR(20),
#catord INT,
#SubCategoria NVARCHAR(255),
#subcatord INT,
#Ordem INT,
#CodigoComp NVARCHAR(10),
#DesignacaoComp NVARCHAR(50),
#QuantidadeComp NVARCHAR(25),
#UnidadeComp NVARCHAR(5),
#intRowCount INT,
#upAction NVARCHAR(255);
SELECT #Codigo = ft_alt.codigo
FROM ft_alt;
SELECT #DataAlteracao = ft_alt.dataalteracao
FROM ft_alt;
SELECT Categoria = ftc_alt.categoria
FROM ftc_alt;
SELECT catord = ftc_alt.catord
FROM ftc_alt;
SELECT SubCategoria = ftc_alt.subcategoria
FROM ftc_alt;
SELECT subcatord = ftc_alt.subcatord
FROM ftc_alt;
SELECT Ordem = ftc_alt.ordem
FROM ftc_alt;
SELECT CodigoComp = ftc_alt.codigocomp
FROM ftc_alt;
SELECT DesignacaoComp = ftc_alt.designacaocomp
FROM ftc_alt;
SELECT QuantidadeComp = ftc_alt.quantidadecomp
FROM ftc_alt;
SELECT UnidadeComp = ftc_alt.unidadecomp
FROM ftc_alt;
SELECT #intRowCount = ##RowCount;
SET #upAction = 'Composição nutricional actualizada/alterada'
BEGIN
IF (#DataAlteracao = (SELECT CONVERT(DATE, GETDATE())))
BEGIN
SET NOCOUNT ON
UPDATE [dbo].[FichaTecnicaComp]
SET [Codigo] = #Codigo,
[DataAlteracao] = #DataAlteracao,
categoria = ftc_alt.categoria,
catord = ftc_alt.catord,
subcategoria = ftc_alt.subcategoria,
subcatord = ftc_alt.subcatord,
ordem = ftc_alt.ordem,
codigocomp = ftc_alt.codigocomp,
designacaocomp = ftc_alt.designacaocomp,
quantidadecomp = ftc_alt.quantidadecomp,
unidadecomp = ftc_alt.unidadecomp
FROM [dbo].[FichaTecnicaComp]
JOIN ftc_alt ON [dbo].[FichaTecnicaComp].[Codigo] = (SELECT ft_alt.codigo
FROM ft_alt)
AND [dbo].[FichaTecnicaComp].Ordem = (SELECT FTC_Alt.Ordem
FROM FTC_Alt)
END
END
he expected result is that data in FichaTecnicaComp is updated from FTC_Alt.
Which doesn't happen.
It should be noted that the FichaTecnicaComp has the following working triggers: insertion, update and delete.
If you need the code of those triggers just ask.
Sub queries used in this context can only return a single value, whereas your sub queries are just returning all values of the Ordem and codigo columns. Use the columns directly in the ON clause instead of as sub-selects. You will also want use aliases instead of the full table names. Using only the keyword JOIN will default to an INNER JOIN, which is what I'm assuming you intend to use, however explicitly stating this will help with readability. The first sub query in your post uses ft_alt, instead of ftc_alt, but since this is the only reference to this table I'm guessing this is a typo?
BEGIN
SET NOCOUNT ON
UPDATE FTC
SET
FTC.[Codigo] = FT.Codigo,
FTC.[DataAlteracao] = FT.dataalteracao,
FTC.categoria = ALT.categoria,
FTC.catord = ALT.catord,
FTC.subcategoria = ALT.subcategoria,
FTC.subcatord = ALT.subcatord,
FTC.ordem = ALT.ordem,
FTC.codigocomp = ALT.codigocomp,
FTC.designacaocomp = ALT.designacaocomp,
FTC.quantidadecomp = ALT.quantidadecomp,
FTC.unidadecomp = ALT.unidadecomp
FROM [dbo].[FichaTecnicaComp] FTC
INNER JOIN ft_alt FT ON FTC.Codig = FT.Codigo
INNER JOIN ftc_alt ALT ON FTC.Ordem = ALT.Ordem
END
The error message itself says that one of the sub queries used in this stored procedure returns more than one record, while your update statement can handle only one returned row the way it is written. If you run below queries one by one, you will know where is the problem. Either you need to fix the subquery or may need to use IN instead of = in main query. I hope that helps.
(SELECT ft_alt.codigo
FROM ft_alt)
AND
[dbo].[FichaTecnicaComp].Ordem =
(SELECT FTC_Alt.Ordem
FROM FTC_Alt
And also run below independently.
SELECT FTC_Alt.Ordem
FROM FTC_Alt
I have a trigger which adds a log entry into a table upon a field change in another table. it works when one row is changed but errors when multiple rows re changed. Anyone out there able to explain what I have to do to get my trigger working also for multi row updates?
Many thanks,
Derek
Declare #PropertyID uniqueidentifier
Set #PropertyID = (Select CONVERT(VARCHAR( 36 ), ISNULL(i.[PropertyPK], d.[PropertyPK]))
FROM
INSERTED i
FULL OUTER JOIN DELETED d ON ( d.[PropertyPK] = i.[PropertyPK] )
WHERE
( d.[strManagingOfficeName] <> i.[strManagingOfficeName] ) OR
( d.[strManagingOfficeName] IS NULL AND i.[strManagingOfficeName] IS NOT NULL ) OR
( i.[strManagingOfficeName] IS NULL AND d.[strManagingOfficeName] IS NOT NULL ))
Declare #CompanyID uniqueidentifier
Set #CompanyID = (Select CompanyFK From Property Where PropertyPK = #PropertyID)
--Deleted Old Ones
Delete From TDSAPILog Where ObjectFK = #PropertyID And strObject = 'Branch Change'
--Insert New Log
INSERT dbo.TDSAPILog(TDSAPILogPK, ObjectFK, strObject, strStatus, CompanyFK, dteDateLogged)
SELECT
NewID(),
#PropertyID,
'Branch Change',
'Active',
#CompanyID ,
GetDate()
This error occur when you return more than 1 value from a query and save in a variable or compare with a value in where clause.
In your example I think the error occur at this line
SET #CompanyID = (SELECT CompanyFK FROM Property WHERE PropertyPK = #PropertyID)
To resolve the reported error just put "TOP 1" in your query. Example is shown here:
SET #CompanyID = (SELECT TOP 1 CompanyFK FROM Property WHERE PropertyPK = #PropertyID)
Subquery returned more than 1 value error may occur at the following scenarios:
SET #YouVariable = (SELECT ColumnID FROM yourTable WHERE Identity = #SomeValue)
-- if the above query return more than 1 value the same error will be occurred
-- to resolve this problem just put "TOP 1" before ColumnID
SELECT *
FROM OtherTable
WHERE OtherIdentity = ((SELECT ColumnID FROM yourTable
WHERE Identity = #SomeValue))
-- if the above query return more than 1 value the same error will be occurred
-- to resolve this problem just replace "= with IN()". Example give below
SELECT *
FROM OtherTable
WHERE OtherIdentity IN ((SELECT ColumnID FROM yourTable
WHERE Identity = #SomeValue))
I want to start off by saying that I am brand new to Stored Procedures, and am basically teaching myself how to do them. Any suggestions or advice will be greatly appreciated. I would mail you chocolate if I could.
The Gist: My organization's clients take a survey on their initial visit and on each 6th subsequent visits. We need to know if the individual has shown improvement over time. The way we decided to do this is compare the 1st to the most recent. So if they have been to 18 sessions, it would be the 1st and 3rd surveys that are compared (because they would have completed the survey 3 times over 18 sessions).
I have been able to obtain the "first" score and the "recent" score with two complex, multiple layered-nested select statements inside of one stored procedure. The "first" one is a TOP(1) linking on unique id (DOCID) and then ordered by date. The "recent" one is a TOP(1) linking on unique id (DOCID) and then ordered by date descending. This gets me exactly what I need within each statement, but it does not output what I need correctly which is obviously to the ordering in the statements.
The end result will be to create a Crystal Report with it for grant reporting purposes.
Declare
#StartDate Date,
#EndDate Date,
#First_DOCID Int,
#First_Clientkey Int,
#First_Date_Screening Date,
#First_Composite_Score Float,
#First_Depression_Score Float,
#First_Emotional_Score Float,
#First_Relationship_Score Float,
#Recent_DOCID Int,
#Recent_Clientkey Int,
#Recent_Date_Screening Date,
#Recent_Composite_Score Float,
#Recent_Depression_Score Float,
#Recent_Emotional_Score Float,
#Recent_Relationship_Score Float,
#Difference_Composit_Score Float,
#Difference_Depression_Score Float,
#Difference_Emotional_Score Float,
#Difference_Relationship_Score Float
SET #StartDate = '1/1/2016'
SET #EndDate = '6/1/2016'
BEGIN
SELECT #First_DOCID = CB24_1.OP__DOCID, #First_Date_Screening = CB24_1.Date_Screening, #First_Clientkey = CB24_1.ClientKey, #First_Composite_Score = CB24_1.Composite_score, #First_Depression_Score = CB24_1.Depression_Results, #First_Emotional_Score = CB24_1.Emotional_Results, #First_Relationship_Score = CB24_1.Relationships_Results
FROM FD__CNSLG_BASIS24 AS CB24_1
WHERE (CB24_1.OP__DOCID =
(Select TOP(1) CB24_2.OP__DOCID
...
ORDER BY CB24_2.Date_Screening))
ORDER BY ClientKey DESC
END
BEGIN
SELECT #Recent_DOCID = CB24_1.OP__DOCID, #Recent_Date_Screening = CB24_1.Date_Screening, #Recent_Clientkey = CB24_1.ClientKey, #Recent_Composite_Score = CB24_1.Composite_score, #Recent_Depression_Score = CB24_1.Depression_Results, #Recent_Emotional_Score = CB24_1.Emotional_Results, #Recent_Relationship_Score = CB24_1.Relationships_Results
FROM FD__CNSLG_BASIS24 AS CB24_1
WHERE (CB24_1.OP__DOCID =
(Select TOP(1) CB24_2.OP__DOCID
...
ORDER BY CB24_2.Date_Screening DESC))
ORDER BY ClientKey
END
SET #Difference_Composit_Score = (#Recent_Composite_Score - #First_Composite_Score)
SET #Difference_Depression_Score = (#Recent_Depression_Score - #First_Depression_Score)
SET #Difference_Emotional_Score = (#Recent_Emotional_Score - #First_Emotional_Score)
SET #Difference_Relationship_Score = (#Recent_Relationship_Score - #First_Relationship_Score)
SELECT
#First_DOCID AS First_Docid,
#First_Clientkey AS First_Clientkey,
#First_Date_Screening AS First_Date_Screening,
#First_Composite_Score AS First_Composite_Score,
#First_Depression_Score AS First_Depression_Score,
#First_Emotional_Score AS First_Emotional_Score,
#First_Relationship_Score AS First_Relationship_Score,
#Recent_DOCID AS Recent_DOCID,
#Recent_Clientkey AS Recent_Clientkey,
#Recent_Date_Screening AS Recent_Date_Screening,
#Recent_Composite_Score AS Recent_Composite_Score,
#Recent_Depression_Score AS Recent_Depression_Score,
#Recent_Emotional_Score AS Recent_Emotional_Score,
#Recent_Relationship_Score AS Recent_Relationship_Score,
#Difference_Composit_Score AS Difference_Composit_Score,
#Difference_Depression_Score AS Difference_Depression_Score,
#Difference_Emotional_Score AS Difference_Emotional_Score,
#Difference_Relationship_Score AS Difference_Relationship_Score
In SQL you don't want unnecessary declared variables.
Here's a contrived but reproducible example which utilizes common table expressions and window functions that should get you in the right direction. I created the stored procedure from the template with the necessary input parameters (which in real life you'd like to avoid).
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.Client_Improvement_Results
(#StartDate DATETIME, #EndDate DATETIME)
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
-- You would never do this in real-life but for a simple reproducible example...
DECLARE #Survey TABLE
(
Clientkey INT,
Date_Screening DATE,
Composite_Score FLOAT
)
INSERT INTO #Survey
VALUES
(1, '2014-04-01', 42.1),
(1, '2014-04-10', 46.1),
(1, '2014-04-20', 48.1),
(2, '2014-05-10', 40.1),
(2, '2014-05-20', 30.1),
(2, '2014-05-30', 10.1)
;
--Use Common Table Expression & Window Functions to ID first/recent visit by client
WITH CTE AS (
SELECT
S.Clientkey
,S.Composite_Score
,S.Date_Screening
,First_Date_Screening = MIN(S.Date_Screening) OVER(PARTITION BY S.Clientkey)
,Recent_Date_Screening = MAX(S.Date_Screening) OVER(PARTITION BY S.Clientkey)
FROM #Survey AS S
)
--Self join of CTE with proper filters
--applied allows you to return differences in one row
SELECT
f.Clientkey
,f.First_Date_Screening
,f.Recent_Date_Screening
,Difference_Score = r.Composite_Score - f.Composite_Score
FROM
CTE AS f --first
INNER JOIN CTE AS r --recent
ON f.Clientkey = r.Clientkey
WHERE
f.Date_Screening = f.First_Date_Screening
AND r.Date_Screening = r.Recent_Date_Screening
END
GO
Here is the solution I came up with after everyone amazing advice.
I want to go back and replace the TOP(1) with another new thing I learned at some point:
select pc.*
from (select pc.*, row_number() over (partition by Clientkey, ProgramAdmitKey order by Date_Screening) as seqnum
from FD__CNSLG_BASIS24 PC) pc
where seqnum = 1
I will have to play with the above script a bit first, however. It doesn't like to be inserted into the larger script below.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
BEGIN
SET NOCOUNT ON;
Declare
#StartDate Date,
#EndDate Date
SET #StartDate = '1/1/2016'
SET #EndDate = '6/1/2016'
WITH CNSL_Clients AS (
SELECT PC_CNT.Clientkey, PC_Cnt.ProgramAdmitKey, PC_Cnt.OP__DOCID
FROM FD__Primary_Client as PC_Cnt
INNER JOIN VW__Cnsl_Session_Count_IndvFamOnly as cnt
ON PC_Cnt.Clientkey = CNT.Clientkey AND PC_Cnt.ProgramAdmitKey = CNT.ProgramAdmitKey
WHERE ((pc_CNT.StartDate between #StartDate AND #EndDate) OR (pc_CNT.StartDate <= #StartDate AND pc_CNT.ENDDate >= #StartDate) OR (pc_CNT.StartDate <= #StartDate AND pc_CNT.ENDDate is null))
AND CNT.SessionCount>=6
),
FIRST_BASIS AS (
SELECT CB24_1.OP__DOCID, CB24_1.Date_Screening, CB24_1.ClientKey, CB24_1.ProgramAdmitKey, CB24_1.Composite_score, CB24_1.Depression_Results,CB24_1.Emotional_Results, CB24_1.Relationships_Results
FROM FD__CNSLG_BASIS24 AS CB24_1
WHERE (CB24_1.OP__DOCID =
(Select TOP(1) CB24_2.OP__DOCID
FROM FD__CNSLG_BASIS24 AS CB24_2
Inner JOIN CNSL_Clients
ON CB24_2.ClientKey = CNSL_Clients.ClientKey AND CB24_2.ProgramAdmitKey = CNSL_Clients.ProgramAdmitKey
WHERE (CB24_1.ClientKey = CB24_2.ClientKey) AND (CB24_1.ProgramAdmitKey = CB24_2.ProgramAdmitKey)
ORDER BY CB24_2.Date_Screening))
),
RECENT_BASIS AS (
SELECT CB24_1.OP__DOCID, CB24_1.Date_Screening, CB24_1.ClientKey, CB24_1.ProgramAdmitKey, CB24_1.Composite_score, CB24_1.Depression_Results,CB24_1.Emotional_Results, CB24_1.Relationships_Results
FROM FD__CNSLG_BASIS24 AS CB24_1
WHERE (CB24_1.OP__DOCID =
(Select TOP(1) CB24_2.OP__DOCID
FROM FD__CNSLG_BASIS24 AS CB24_2
Inner JOIN CNSL_Clients
ON CB24_2.ClientKey = CNSL_Clients.ClientKey AND CB24_2.ProgramAdmitKey = CNSL_Clients.ProgramAdmitKey
WHERE (CB24_1.ClientKey = CB24_2.ClientKey) AND (CB24_1.ProgramAdmitKey = CB24_2.ProgramAdmitKey)
ORDER BY CB24_2.Date_Screening DESC))
)
SELECT F.OP__DOCID AS First_DOCID,R.OP__DOCID as Recent_DOCID,F.ClientKey, F.ProgramAdmitKey, F.Composite_Score AS FComposite_Score, R.Composite_Score as RComposite_Score, Composite_Change = R.Composite_Score - F.Composite_Score, F.Depression_Results AS FDepression_Results, R.Depression_Results AS RDepression_Resluts, Depression_Change = R.Depression_Results - F.Depression_Results, F.Emotional_Results AS FEmotional_Resluts, R.Emotional_Results AS REmotionall_Reslu, Emotional_Change = R.Emotional_Results - F.Emotional_Results, F.Relationships_Results AS FRelationships_Resluts, R.Relationships_Results AS RRelationships_Resluts, Relationship_Change = R.Relationships_Results - F.Relationships_Results
FROM First_basis AS F
FULL Outer JOIN RECENT_BASIS AS R
ON F.ClientKey = R.ClientKey AND F.ProgramAdmitKey = R.ProgramAdmitKey
ORDER BY F.ClientKey
END
GO
MyTableA has several million records. On regular occasions every row in MyTableA needs to be updated with values from TheirTableA.
Unfortunately I have no control over TheirTableA and there is no field to indicate if anything in TheirTableA has changed so I either just update everything or I update based on comparing every field which could be different (not really feasible as this is a long and wide table).
Unfortunately the transaction log is ballooning doing a straight update so I wanted to chunk it by using UPDATE TOP, however, as I understand it I need some field to determine if the records in MyTableA have been updated yet or not otherwise I'll end up in an infinite loop:
declare #again as bit;
set #again = 1;
while #again = 1
begin
update top (10000) MyTableA
set my.A1 = their.A1, my.A2 = their.A2, my.A3 = their.A3
from MyTableA my
join TheirTableA their on my.Id = their.Id
if ##ROWCOUNT > 0
set #again = 1
else
set #again = 0
end
is the only way this will work if I add in a
where my.A1 <> their.A1 and my.A2 <> their.A2 and my.A3 <> their.A3
this seems like it will be horribly inefficient with many columns to compare
I'm sure I'm missing an obvious alternative?
Assuming both tables are the same structure, you can get a resultset of rows that are different using
SELECT * into #different_rows from MyTable EXCEPT select * from TheirTable and then update from that using whatever key fields are available.
Well, the first, and simplest solution, would obviously be if you could change the schema to include a timestamp for last update - and then only update the rows with a timestamp newer than your last change.
But if that is not possible, another way to go could be to use the HashBytes function, perhaps by concatenating the fields into an xml that you then compare. The caveat here is an 8kb limit (https://connect.microsoft.com/SQLServer/feedback/details/273429/hashbytes-function-should-support-large-data-types) EDIT: Once again, I have stolen code, this time from:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2009/10/21/detecting-changed-rows-in-a-trigger-using-hashbytes-and-without-eventdata-and-or-s.aspx
His example is:
select batch_id
from (
select distinct batch_id, hash_combined = hashbytes( 'sha1', combined )
from ( select batch_id,
combined =( select batch_id, batch_name, some_parm, some_parm2
from deleted c -- need old values
where c.batch_id = d.batch_id
for xml path( '' ) )
from deleted d
union all
select batch_id,
combined =( select batch_id, batch_name, some_parm, some_parm2
from some_base_table c -- need current values (could use inserted here)
where c.batch_id = d.batch_id
for xml path( '' ) )
from deleted d
) as r
) as c
group by batch_id
having count(*) > 1
A last resort (and my original suggestion) is to try Binary_Checksum? As noted in the comment, this does open the risk for a rather high collision rate.
http://msdn.microsoft.com/en-us/library/ms173784.aspx
I have stolen the following example from lessthandot.com - link to the full SQL (and other cool functions) is below.
--Data Mismatch
SELECT 'Data Mismatch', t1.au_id
FROM( SELECT BINARY_CHECKSUM(*) AS CheckSum1 ,au_id FROM pubs..authors) t1
JOIN(SELECT BINARY_CHECKSUM(*) AS CheckSum2,au_id FROM tempdb..authors2) t2 ON t1.au_id =t2.au_id
WHERE CheckSum1 <> CheckSum2
Example taken from http://wiki.lessthandot.com/index.php/Ten_SQL_Server_Functions_That_You_Have_Ignored_Until_Now
I don't know if this is better than adding where my.A1 <> their.A1 and my.A2 <> their.A2 and my.A3 <> their.A3, but I would definitely give it a try (assuming SQL Server 2005+):
declare #again as bit;
set #again = 1;
declare #idlist table (Id int);
while #again = 1
begin
update top (10000) MyTableA
set my.A1 = their.A1, my.A2 = their.A2, my.A3 = their.A3
output inserted.Id into #idlist (Id)
from MyTableA my
join TheirTableA their on my.Id = their.Id
left join #idlist i on my.Id = i.Id
where i.Id is null
/* alternatively (instead of left join + where):
where not exists (select * from #idlist where Id = my.Id) */
if ##ROWCOUNT > 0
set #again = 1
else
set #again = 0
end
That is, declare a table variable for collecting the IDs of the rows being updated and use that table for looking up (and omitting) IDs that have already been updated.
A slight variation on the method would be to use a local temporary table instead of a table variable. That way you would be able to create an index on the ID lookup table, which might result in better performance.
If schema change is not possible. How about using trigger to save off the Ids that have changed. And only import/export those rows.
Or use trigger to export it immediately.