Removing Transactions from cfquery s - tsql

I had some original code which looked like
<cftransaction>
<cfquery>
UPDATE
...
</cfquery>
<cfquery>
UPDATE
...
</cfquery>
<cfquery>
DELETE
...
</cfquery>
</cftransaction>
I am looking to change the code to
<cftransaction>
<cfscript>
QueryExecute("
UPDATE
...
UPDATE
...
DELETE
...
");
</cfscript>
<cftransaction>
Do I still need to use <cftransaction> ?

Yes.
SQL Server defaults to auto-commit mode. Without the explicit transaction, each individual statement would be committed after it executes. Just as they would if you executed the SQL string in SSMS.
Here's an example to demonstrate. It executes three UPDATE statements in a row (deliberately causing an error on the second one). Without a transaction, the first and last UPDATE succeed, but the middle one is rolled back. So if all three statements should be handled as a single unit, you must use a cftransaction.
Before:
After:
DDL/Sample Data:
CREATE TABLE SomeTable ( Id INT, Col VARCHAR(50) )
INSERT INTO SomeTable
VALUES (1,'Original'),(2,'Original'),(3,'Original')
CF
<cfscript>
before = QueryExecute(" SELECT * FROM SomeTable");
writeDump( before );
// DEMO: Deliberately omits cftransaction and causes error
try {
QueryExecute(" UPDATE SomeTable SET Col = 'Changed' WHERE Id = 1
UPDATE SomeTable SET Col = 'Changed' WHERE Id = 2 AND Id = 1 / 0
UPDATE SomeTable SET Col = 'Changed' WHERE Id = 3
");
}
catch( any e) {
writeDump( e );
}
after = QueryExecute(" SELECT * FROM SomeTable");
writeDump( after );
</cfscript>

Related

Merge - when matched then do nothing

I need to write a MERGE statement to insert data WHEN NOT MATCHED condition, WHEN MATCHED I'd like the query to do nothing but I've got to include this condition because I badly need to catch the source data from both conditions into my output table.
Here's my code:
MERGE dm_data_bps.dbo.akcja AS target
USING (
SELECT *
FROM #CEIDG
WHERE isnull(sp_id, '') <> ''
) AS source
ON target.ak_id = source.ceidg_ak_id
WHEN NOT MATCHED
THEN
INSERT (
ak_akt_id
,ak_sp_id
,ak_kolejnosc
,ak_interwal
,ak_zakonczono
,ak_pr_id
,ak_publiczna
)
VALUES (
1246
,sp_id
,0
,0
,getdate()
,5
,1
)
WHEN MATCHED
THEN
UPDATE
<DO NOTHING>
OUTPUT inserted.ak_id
,source.Firma
,source.AdresPocztyElektronicznej
,source.AdresStronyInternetowej
,source.IdentyfikatorWpisu
,source.DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,source.DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,source.DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,source.DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,source.DataWykresleniaWpisuZRejestru
,source.MalzenskaWspolnoscMajatkowa
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,source.Zakazy
,source.InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,source.Sukcesja
,source.AdresGlownegoMiejscaWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,source.AdresDoDoreczen
,source.STATUS
INTO #ceidg_ak_id;
How can I accomplish my goal?
I'm not sure I'd bother with all of the overhead that comes with a MERGE statement. See Use Caution with SQL Server's MERGE Statement.
You can get everything you need with an explicit transaction.
BEGIN TRANSACTION;
UPDATE
target
SET
ak_akt_id = 1246
,ak_sp_id = sp_id
,ak_kolejnosc = 0
,ak_interwal = 0
,ak_zakonczono = GETDATE()
,ak_pr_id = 5
,ak_publiczna = 1
FROM
dm_data_bps.dbo.akcja AS target
JOIN
(SELECT * FROM #CEIDG WHERE sp_id <> '') AS source
ON
target.ak_id = source.ceidg_ak_id;
SELECT
Firma
,AdresPocztyElektronicznej
,AdresStronyInternetowej
,IdentyfikatorWpisu
,DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,DataWykresleniaWpisuZRejestru
,MalzenskaWspolnoscMajatkowa
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,Zakazy
,InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,Sukcesja
,AdresGlownegoMiejscaWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,AdresDoDoreczen
,STATUS
INTO
#ceidg_ak_id
FROM
#CEIDG
WHERE
sp_id <> '';
COMMIT TRANSACTION;

DB2 Query : insert data in history table if not exists already

I have History table and transaction table.....and reference table...
If status in reference table is CLOSE then take those record verify in History table if not there insert from transaction table..... wiring query like this .... checking better one... please advice.. this query can be used for huge data ?
INSERT INTO LIB1.HIST_TBL
( SELECT R.ACCT, R.STATUS, R.DATE FROM
LIB2.HIST_TBL R JOIN LIB1.REF_TBL C
ON R.ACCT = C.ACCT WHERE C.STATUS = '5'
AND R.ACCT NOT IN
(SELECT ACTNO FROM LIB1.HIST_TBL)) ;
If you're on a current release of DB2 for i, take a look at the MERGE statement
MERGE INTO hist_tbl H
USING (SELECT * FROM ref_tbl R
WHERE r.status = 'S')
ON h.actno = r.actno
WHEN NOT MATCHED THEN
INSERT (actno,histcol2, histcol3) VALUES (r.actno,r.refcol2,r.refcol3)
--if needed
WHEN MATCHED
UPDATE SET (actno,histcol2, histcol3) = (r.actno,r.refcol2,r.refcol3)

Cannot insert NULL value into column error

I have a issue where I want to update a column in a table and with a trigger to update same column but in another table. It says I cannot insert NULL but I can't seem to understand from where it gets that NULL value. This is the trigger:
CREATE TRIGGER Custom_WF_Update_WF_DefinitionSteps_DefinitionId ON WF.Definition
AFTER UPDATE AS BEGIN
IF UPDATE(DefinitionId)
IF TRIGGER_NESTLEVEL() < 2
BEGIN
ALTER TABLE WF.DefinitionSteps NOCHECK CONSTRAINT ALL
UPDATE WF.DefinitionSteps
SET DefinitionId =
(SELECT i.DefinitionId
FROM inserted i,
deleted d
WHERE WF.DefinitionSteps.DefinitionId = d.DefinitionId
AND i.oldPkCol = d.DefinitionId)
WHERE WF.DefinitionSteps.DefinitionId IN
(SELECT DefinitionId FROM deleted)
ALTER TABLE WF.DefinitionSteps CHECK CONSTRAINT ALL
END
END
This update statement works just fine:
UPDATE [CCHMergeIntermediate].[WF].[Definition]
SET DefinitionId = source.DefinitionId + 445
FROM [CCHMergeIntermediate].[WF].[Definition] source
But this one fails:
UPDATE [CCHMergeIntermediate].[WF].[Definition]
SET DefinitionId = target.DefinitionId
FROM [CCHMergeIntermediate].[WF].[Definition] source
INNER JOIN [centralq3].[WF].[Definition] target
ON (((source.Name = target.Name) OR (source.Name IS NULL AND target.Name IS NULL)))
I get the following error:
Msg 515, Level 16, State 2, Procedure Custom_WF_Update_WF_DefinitionSteps_DefinitionId, Line 7
Cannot insert the value NULL into column 'DefinitionId', table 'CCHMergeIntermediate.WF.DefinitionSteps'; column does not allow nulls. UPDATE fails.
If I do a select instead of the update statement, like this:
SELECT source.DefinitionId, target.DefinitionId
FROM [CCHMergeIntermediate].[WF].[Definition] source
INNER JOIN [centralq3].[WF].[Definition] target
ON (((source.Name = target.Name) OR (source.Name IS NULL AND target.Name IS NULL)))
I get this result:
http://i.stack.imgur.com/3cZsM.png (sorry for external link, I don't have enaugh reputation to post image here )
What am I doing wrong? What I don't see? What am I missing..?
The problem was in the trigger at the condition. I modified the second where from i.oldPkCol = d.DefinitionId to i.oldPkCol = **d.oldPkCol** and it worked.
UPDATE WF.DefinitionSteps
SET DefinitionId =
(SELECT i.DefinitionId
FROM inserted i,
deleted d
WHERE WF.DefinitionSteps.DefinitionId = d.DefinitionId
AND i.oldPkCol = **d.oldPkCol**)
WHERE WF.DefinitionSteps.DefinitionId IN
(SELECT DefinitionId FROM deleted)

How to set a bit based on a value existing in a table

I have a table. I have 2 variables, one is a bit, the other is an int.
Table: WorkGroupCollectionDetail
Variables: #WorkgroupID int, #IsFSBP bit
The table has WorkGroupId int PK and WorkGroupCollectionCode varchar PK. That's it.
I can run a query like this:
SELECT WorkGroupId
FROM WorkGroupCollectionDetail
WHERE WorkGroupCollectionCode = 'FSBP'
and it gives me a list of WorkGroupID.
So what I need to do is if the value of #WorkgroupID is inside the results of that query, I need to set the bit variable to true.
select #IsFBSP = case
when exists (
select 42 from WorkGroupDetailCollection
where WorkGroupCollectionCode = 'FSBP' and WorkGroupId = #WorkGroupId ) then 1
else 0 end
which is logically equivalent to:
select #IsFBSP = case
when #WorkGroupId in (
select WorkGroupId from WorkGroupDetailCollection
where WorkGroupCollectionCode = 'FSBP' ) then 1
else 0 end
A query using EXISTS often performs better than a query using IN. You can check the execution plans to see how they compare in your particular case.
Note that these examples include setting the bit value to zero as well as one.
You could modify the SELECT to include the check for the WorkGroupId and update the #IsFSBP accordingly:
IF EXISTS(SELECT WorkGroupId
FROM WorkGroupCollectionDetail
WHERE WorkGroupCollectionCode = 'FSBP'
AND WorkGroupId = #WorkgroupID)
BEGIN
SELECT #IsFSBP = 1;
END
SQL Fiddle example
I'm guessing you're looking for
Set #BitVariable = count(*)
From TestTable
WHERE TestCode = 'TestValue' and TestID = #TestID

oracle sequence question

There are two inserts in my trigger which is fired by an update. My Vendor_Hist table has a field called thID which is the primary key in Task_History table. thID gets its' value from mySeq.nextval.
INSERT INTO TASK_HISTORY
( thID, phId, LABOR, VERSION )
( select mySeq.NEXTVAL, mySeq2.CurrVal, LABOR, tmpVersion
from tasks t
where t.project_id = :new.project_ID );
select mySeq.currval into tmpTHID from dual; -- problem here!
INSERT INTO VENDOR_HIST
( vhID, thID, Amount, Position, version )
( select mySeq3.NEXTVAL, tmpTHID,
Amount, Position, tmpVersion
from vendors v2, tasks t2
where v2.myID = t2.myID
and t2.project_id = :new.project_ID );
Now, my problem is the tmpTHID always the latest value of mySeq.nextVal. So, if thID in task_history is 1,2,3, I get three inserts into vendor_hist table with 3,3,3. It has to be 1,2,3. I also tried
INSERT INTO TASK_HISTORY
( thID, phId, LABOR, VERSION )
( select mySeq.NEXTVAL, mySe2.CurrVal, LABOR, tmpVersion
from tasks t
where t.project_id = :new.project_ID ) returning thID into :tmpTHID;
but then I get a "warning compiled with errors" message when I execute the trigger. How do I make sure that the thID in first insert is also the same in my second insert?
Hope it makes sense.
for i in (select * from tasks t
where t.project_id = :new.project_id)
loop
insert into task_history
( thID, phId, LABOR, VERSION )
values
(mySeq.NEXTVAL, mySeq2.CurrVal, i.LABOR, i.tmpVersion);
for each j in (select * from vendors v
where i.myId = v.myId)
loop
insert into vendor_history
( vhID, thID, Amount, Position, version )
values
(mySeq3.NEXTVAL, mySeq.CURRVAL, j.Amount, j.Position, j.tmpVersion)
end loop;
end loop;
I'm assuming the columns inserted in the second insert are from the VENDORS table; if not, the referencing cursor (i or j) should be used as appropriate.
Instead of the currVal, it works with the following subselect.
( select min(thID) from task_history t3
where t3.project_id = t2.project_id
and t3.myID = t2.myID
and t3.version = tmpVersion ),