The first question ever on here.
I have an update statement, which seems to have been working perfectly fine, previously.
Out of nowhere, the update statement seems to update a single row multiple times, but not for all rows, if that makes sense?
The statement is run via SQL agent and scheduled every 10 seconds (along with other steps)
Sorry if it's big or messy, I'm self-trained!
The first step, is to insert data into a table that needs to be updated on the main table, using a view.
For the audit trail, I then insert IDs into another table, to track what's being updated.
The problematic part of this statement is the update main table (DT_V_POTATTENDANCE).
Below that is an insert into the main table where an ID cannot be found in the view.
The rest of the script again is all part of the audit trail which is how I've found it updating a single row multiple times for whatever reason (but not every time if that makes sense?) and setting the records as updated so they don't keep updating.
Thanks for your help, really appreciate it.
SELECT DISTINCT * INTO _TEMPTABLEAPPROVEDATTENDANCE
from BE_RPT_PA_ATTENDANCE_TO_UPDATE
SET IDENTITY_INSERT _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS ON
INSERT INTO _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS (SIGNID)
SELECT SIGNID FROM DT_PA_POTATTENDANCE WHERE APPROVED = 1 AND UPDATED IS NULL
SET IDENTITY_INSERT _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS OFF
declare #time datetime
set #time = (select getdate())
UPDATE DT_V_POTATTENDANCE
SET
DT_V_POTATTENDANCE.CB_H_MON = DT_V_POTATTENDANCE.CB_H_MON + Y.CB_H_MON,
DT_V_POTATTENDANCE.CB_M_MON = DT_V_POTATTENDANCE.CB_M_MON + Y.CB_M_MON,
DT_V_POTATTENDANCE.CB_H_TUE = DT_V_POTATTENDANCE.CB_H_TUE + Y.CB_H_TUE,
DT_V_POTATTENDANCE.CB_M_TUE = DT_V_POTATTENDANCE.CB_M_TUE + Y.CB_M_TUE,
DT_V_POTATTENDANCE.CB_H_WED = DT_V_POTATTENDANCE.CB_H_WED + Y.CB_H_WED,
DT_V_POTATTENDANCE.CB_M_WED = DT_V_POTATTENDANCE.CB_M_WED + Y.CB_M_WED,
DT_V_POTATTENDANCE.CB_H_THU = DT_V_POTATTENDANCE.CB_H_THU + Y.CB_H_THU,
DT_V_POTATTENDANCE.CB_M_THU = DT_V_POTATTENDANCE.CB_M_THU + Y.CB_M_THU,
DT_V_POTATTENDANCE.CB_H_FRI = DT_V_POTATTENDANCE.CB_H_FRI + Y.CB_H_FRI,
DT_V_POTATTENDANCE.CB_M_FRI = DT_V_POTATTENDANCE.CB_M_FRI + Y.CB_M_FRI,
DT_V_POTATTENDANCE.H_H_MON = DT_V_POTATTENDANCE.H_H_MON + Y.H_H_MON,
DT_V_POTATTENDANCE.H_M_MON = DT_V_POTATTENDANCE.H_M_MON + Y.H_M_MON,
DT_V_POTATTENDANCE.H_H_TUE = DT_V_POTATTENDANCE.H_H_TUE + Y.H_H_TUE,
DT_V_POTATTENDANCE.H_M_TUE = DT_V_POTATTENDANCE.H_M_TUE + Y.H_M_TUE,
DT_V_POTATTENDANCE.H_H_WED = DT_V_POTATTENDANCE.H_H_WED + Y.H_H_WED,
DT_V_POTATTENDANCE.H_M_WED = DT_V_POTATTENDANCE.H_M_WED + Y.H_M_WED,
DT_V_POTATTENDANCE.H_H_THU = DT_V_POTATTENDANCE.H_H_THU + Y.H_H_THU,
DT_V_POTATTENDANCE.H_M_THU = DT_V_POTATTENDANCE.H_M_THU + Y.H_M_THU,
DT_V_POTATTENDANCE.H_H_FRI = DT_V_POTATTENDANCE.H_H_FRI + Y.H_H_FRI,
DT_V_POTATTENDANCE.H_M_FRI = DT_V_POTATTENDANCE.H_M_FRI + Y.H_M_FRI,
DT_V_POTATTENDANCE.AA_H_MON = DT_V_POTATTENDANCE.AA_H_MON + Y.AA_H_MON,
DT_V_POTATTENDANCE.AA_M_MON = DT_V_POTATTENDANCE.AA_M_MON + Y.AA_M_MON,
DT_V_POTATTENDANCE.AA_H_TUE = DT_V_POTATTENDANCE.AA_H_TUE + Y.AA_H_TUE,
DT_V_POTATTENDANCE.AA_M_TUE = DT_V_POTATTENDANCE.AA_M_TUE + Y.AA_M_TUE,
DT_V_POTATTENDANCE.AA_H_WED = DT_V_POTATTENDANCE.AA_H_WED + Y.AA_H_WED,
DT_V_POTATTENDANCE.AA_M_WED = DT_V_POTATTENDANCE.AA_M_WED + Y.AA_M_WED,
DT_V_POTATTENDANCE.AA_H_THU = DT_V_POTATTENDANCE.AA_H_THU + Y.AA_H_THU,
DT_V_POTATTENDANCE.AA_M_THU = DT_V_POTATTENDANCE.AA_M_THU + Y.AA_M_THU,
DT_V_POTATTENDANCE.AA_H_FRI = DT_V_POTATTENDANCE.AA_H_FRI + Y.AA_H_FRI,
DT_V_POTATTENDANCE.AA_M_FRI = DT_V_POTATTENDANCE.AA_M_FRI + Y.AA_M_FRI
FROM _TEMPTABLEAPPROVEDATTENDANCE Y
WHERE DT_V_POTATTENDANCE.ATTENDANCEWEEKID = Y.ATTENDANCEWEEKID
AND Y.ATTENDANCEWEEKID IS NOT NULL
AND Y.TRAINEEID <> '0683-0001-107827'
INSERT INTO DT_V_POTATTENDANCE
([TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI])
SELECT [TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI]
FROM _TEMPTABLEAPPROVEDATTENDANCE
WHERE ATTENDANCEWEEKID IS NULL
AND TRAINEEID <> '0683-0001-107827'
UPDATE DT_PA_POTATTENDANCE
SET UPDATED = 1
FROM DT_PA_POTATTENDANCE
WHERE APPROVEDTIMESTAMP < #time and
TRAINEEID <> '0683-0001-107827' AND
APPROVED = 1 AND UPDATED IS NULL AND SIGNID IN (SELECT SIGNID FROM _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS)
INSERT INTO _TEMPTABLEAPPROVEDATTENDANCEUPDATED
([ATTENDANCEWEEKID],[TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI])
SELECT [ATTENDANCEWEEKID],[TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI]
FROM _TEMPTABLEAPPROVEDATTENDANCE
WHERE TRAINEEID <> '0683-0001-107827'
drop table _TEMPTABLEAPPROVEDATTENDANCE
I found the reason why it was doing it!
Below part of my script was making it run and run again.
The user in question, we have an MIS that sets the field "Approved" to 1, the script above then updates the master table DT_V_POTATTENDANCE, but it wasn't setting the field "Updated" to 1 because the users time was 3 minutes out!
UPDATE DT_PA_POTATTENDANCE
SET UPDATED = 1
FROM DT_PA_POTATTENDANCE
WHERE APPROVEDTIMESTAMP < #time and
TRAINEEID <> '0683-0001-107827' AND
APPROVED = 1 AND UPDATED IS NULL AND SIGNID IN
(SELECT SIGNID FROM _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS)
Thanks for all your help guys, appreciate it!
I have SSIS package, which retrieves all records including duplicates. My question is how to add an incremental value for the duplicate records (only the ID and PropertyID).
Eg
Records from a Merge Join
ID Name PropertyID Value
1 A 1 123
1 A 1 223
2 B 2 334
3 C 1 22
3 C 1 45
Now I need to append an incremental value at the end of the each record as
ID Name PropertyID Value RID
1 A 1 123 1
1 A 1 223 2
2 B 2 334 1
3 C 1 22 1
3 C 1 45 2
Since ID 1 & 3 are returned twice, the first record has RID as 1 and the second record as 2.
ID and PropertyID need to be considered to generate the Repeating ID i.e RID.
How can I do it in SSIS or using SQL command?
Update #1:
Please correct me if I'm wrong, since the data is not stored in any table yet, I'm unable to use the select query using rownumber(). Any way I can do it from the Merge Join?
You could use ROW_NUMBER:
SELECT ID,
Name,
PropertyID,
Value,
ROW_NUMBER() OVER(PARTITION BY ID, PropertyID ORDER BY Value) As RID
FROM TableName
This will do the job for you: https://paultebraak.wordpress.com/2013/02/25/rank-partitioning-in-etl-using-ssis/
You will need to write a custom script, something like this:
public
class
ScriptMain : UserComponent
{
string _sub_category = “”;
int _row_rank = 1;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.subcategory != _sub_category)
{
_row_rank = 1;
Row.rowrank = _row_rank;
_sub_category = Row.subcategory;
}
else
{
_row_rank++;
Row.rowrank = _row_rank;
}
}
}
Is it possible to display selected data from 8 different tables to a datagridview in visual basic? I am using MS SQL. I have 1 table for president, 1 table for vice president, 1 table for secretary, 1 table for treasurer. 1 table for auditor, and 1 table for the business manager. Each table has five rows ID, PARTY LIST, NAME, GRADE, and VoteCount.
What I need to display to datagridview is party list, the pres. name, vice. pres. name, sec. name, tre. name and so on.
I would like to know if it is possible for datagridview.
Thanks.
You need to add your column manually. I took this sample code from one of my app. it queries the format for each column from SQL and stores it in xgrid1 and xgrid2. one is the name of the column the other one is the Display name.
xgrid3 is the size of my column and xgrid4 is the alignment of the column. use whatever suits you. Once you have your columns setup then just runs you query and add rows to the grid using a loop. You cannot use datasource in your case.
use this to setup your grid
DG_Clients.Columns.Clear()
DG_Clients.RowHeadersVisible = False
For xcount = 0 To xgrid1.Length - 1
DG_Clients.Columns.Add(xgrid2(xcount), xgrid1(xcount))
DG_Clients.Columns(DG_Clients.Columns.Count - 1).Width = xgrid3(xcount)
Select Case xgrid4(xcount)
Case "L"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
Case "R"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
Case "C"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End Select
Next
and use this to add to it
Dim xRow As DataRow
Dim xProvince As String
For Each dRow As DataRow In DBAcc.DBDT.Rows
Dim xlist(DG_Clients.Columns.Count - 1) As String
For xcount = 0 To xgrid1.Length - 1
Select Case xgrid2(xcount)
Case "Sex"
If dRow(xgrid2(xcount)) = 0 Then xlist(xcount) = "Male" Else xlist(xcount) = "Female"
Case "Province"
Select Case dRow(xgrid2(xcount))
Case 0
xProvince = "QC"
Case 1
xProvince = "On"
Case 2
xProvince = "NS"
Case 3
xProvince = "PEI"
Case 4
xProvince = "NF"
Case 5
xProvince = "MB"
Case 6
xProvince = "SK"
Case 7
xProvince = "AB"
Case 8
xProvince = "BC"
Case 9
xProvince = "NU"
End Select
xlist(xcount) = xProvince
Case Else
xlist(xcount) = dRow(xgrid2(xcount))
End Select
Next
DG_Clients.Rows.Add(xlist)
Next