I have 3 tables ticket_addresses,tickets,['2014nosec add']. I want to update this ticket_addresses table but unfortunately i have run this query and it updated the entire table where the column ta_address_2 with '.'.
my doubt is my query is wrong because the from table ['2014nosec add'] is different from the update table and it does not have ta-address-2 column on it should give me an error because the from table is not the in the list.
is there any way to rollback the update query as i have not used it as transaction . I am using sql server managament studio.
update
ticket_addresses set ta_address_2 = '.'
FROM ['2014nosec add'] inner join tickets ------> I think this is wrong here.. it should be ticket_addresses table(right)
on ['2014nosec add'].[PCN] = tickets.t_reference
where ta_address_2 = ''
and ta_address_1 <> ' ' and t_camera_ticket = '-1'
and
convert (datetime,t_date_time_issued,101) between convert(datetime,'2014/04/15',101) and convert (datetime,'2014/06/06',101)
By default SQL Server using "Autocommit" mode for transaction management. So you can't rollback this query because it already commited.
Related
I am trying to use a simple IF ELSE query to test a feature with DB2 SQL. However when I attempt to execute it, I run into an error stating that I am not allowed to execute DDL statements.
What is throwing me off is that as far as I know, only database structure altering statements are considered DDL statements.
What gives?
Code:
IF 'True' = 'True' THEN
SELECT * FROM RM_TRANSACTION
FETCH FIRST 2 ROWS ONLY
FOR READ ONLY WITH UR
ELSE
SELECT * FROM RM_TRANSACTION
FETCH FIRST 4 ROWS ONLY
FOR READ ONLY WITH UR
END IF
https://imgur.com/a/58RYjpu
The problem is that you can’t ‘select to nowhere’ in a compound statement in DB2. Db2 CLP can return you the result set of a single sql statement, but it doesn’t try to do the same for select statements in a compound statement.
If you want to print the result set from a select statement in a compound statement, you can, for example, declare a cursor, fetch it in a loop, and use dbms_output.put_line calls to print the values of variables.
I am facing issue with ever-running query in one stored procedure (using SQL Server 2005).
In this stored procedure there are many UPDATE statements and few INSERT statements running under one Transaction.
When debugging the stored procedure, I found that there is one specific UPDATE query which is running indefinitely as shown below.
BEGIN TRAN
...
...-- some INSERT & UPDATE statements
UPDATE PIH
SET PIH.RemittanceHeaderID = RD.RemittanceHeaderID
FROM TempPayroll TP
INNER JOIN RemittanceDetails RD ON TP.UniqueID = RD.PLInvoiceDetailID
INNER JOIN PLInvoiceHeader PIH ON RD.PLInvoiceUniqueID = PIH.InvoiceUniqueID AND PIH.CompanyCode = TP.CompanyCode
WHERE TP.PLSelfBill = 1
AND TP.UsePLPaymentTerms = 1
AND RD.PLInvoiceDetailID IS NOT NULL
AND TP.RecordType LIKE 'INVOICE%'
AND RD.RecordType LIKE 'INVOICE%'
... --some UPDATE statements
COMMIT TRAN
However, when I remove the CompanyCode condition the stored procedure is working fast with no issues. Also if I execute just this statement alone under a transaction separately it is running fine.
There are around 240 matching records for this query. Each table contains just 30k - 50k records.
How do I debug the query and how to fix it?
Why just that JOIN condition (CompanyCode) is creating this issue?
Thanks in advance.
How does SQL behave and what does it return when I try updating or deleting a on existent record in the table. I checked running an update on a non existent record and the query basically runs, doesn't do anything and returns saying a no records were affected. However I wanted to understand how it works internally and what would the result variable hold after such queries.
It depends on the database but usually you can get the affected rows after update or insert.
If nothing is updated you will get 0 for the rowcount.
Pl SQL example:
declare
i number;
begin
update MyTable set status = 'someStatus' where name = 'someName';
i := sql%rowcount;
end;
T-SQL example:
UPDATE Table Set Column = 0 WHERE Column IS NULL
SELECT #RowCount = ##ROWCOUNT
How it works internaly depends on the database vendor.
I have a SQL Server Stored Procedure (using SQL Server 2008 R2) where it performs several different table updates. When rows have been updated I want to record information in an Audit table.
Here is my pseudo code:
UPDATE tblName SET flag = 'Y' WHERE flag = 'N'
IF ##ROWCOUNT > 0
BEGIN
INSERT INTO auditTable...etc
END
Unfortunately, even when zero rows are updated it still records the action in the audit table.
Note: There are no related triggers on the table being updated.
Any ideas why this could be happening?
Any statement that is executed in T-SQL will set the ##rowcount, even the if statement, so the general rule is to capture the value in the statement following the statement you're interested in.
So after
update table set ....
you want
Select #mycount = ##Rowcount
Then you use this value to do your flow control or messages.
As the docs state, even a simple variable assignment will set the ##rowcount to 1.
This is why it's important in this case that if you want people to diagnose the problem then you need to provide the actual code, not pseudo code.
I am just at starting levels in DB usage and have 2 basic questions
I have a generic UPDATE stored proc which updates all columns of a table.
But i need to make it conditional wherein it does not SET when the parameter is NULL.
Usage: I want to use this as a single SP to UPDATE any subset of columns, the caller from C# will fill in corresponding parameter values and leave other parameters NULL.
2
In case of , "UPDATE selected records" do i need to use locking inside stored proc ?
Why ? Isn't the operation in itself locked and transactional ?
I find the same question come up when i need to UPDATE selected(condition) records and then Return updated records.
UPDATE table
SET a = case when #a is null then a else #a end
WHERE id = #id
OR
EXEC 'update table set ' + #update + ' where id = ' + #id
OR
Conditionally update a column at a time
First option to me would usually be preferrable as it is usually efficient enough and you do not need to worry about string escaping
If I have understood the question properly, Why can't you build a query on the fly from sql server SP, and use sp_sqlexecute. So when you build query you can ensure only columns that have value has got updated.
Does this answer your question?