|
Can somebody help to correct my SQL statement below with the correct transaction syntax?
<br />
BEGIN TRANSACTION<br />
<br />
DECLARE @TMPKEY varchar(10) <br />
DECLARE @NEWKEY varchar(10)<br />
<br />
SET @TMPKEY = '0707436'<br />
SET @NEWKEY = '070700058'<br />
<br />
Insert Into nnccdtrequestDetail<br />
(Requestno,RequestItemNo,DetailStatus)<br />
select @NEWKEY,RequestItemNo,DetailStatus from nnccdtrequestdetail<br />
Where RequestNo = @TMPKEY<br />
<br />
IF @@TRANCOUNT = 1<br />
COMMIT TRANSACTION<br />
<br />
IF @@TRANCOUNT <> 1<br />
ROLLBACK TRANSACTION
Dabsukol
|
|
|
|
|
The second @@TRANCOUNT statement is a problem. Basically, the @@Trancount tells you the active number of connections that are present on the connection.
COMMIT TRANSACTION decrements the transaction count, so if you have 1 active transaction before the commit, you have 0 active transactions after. As you can see, this is where your problem occurs - you are testing @@TRANCOUNT <> 1 to call rollback transaction when it is now 0. In otherwords, the rollback will always be attempted.
I would be tempted to rewrite this section as:
Insert Into nnccdtrequestDetail
(Requestno,RequestItemNo,DetailStatus)
select @NEWKEY,RequestItemNo,DetailStatus from nnccdtrequestdetail
Where RequestNo = @TMPKEY
IF @@ERROR = 0
BEGIN
-- There is no error here.
IF @@TRANCOUNT = 1
BEGIN
COMMIT TRANSACTION
END
END
ELSE
BEGIN
-- If I reach this point, there has been an error.
IF @@TRANCOUNT = 1
BEGIN
ROLLBACK TRANSACTION
END
END Actually, I would normally not use the @@TRANCOUNT method because I match BEGIN TRANSACTION statements with the relevant closing transaction statements.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
@@TRANCOUNT is useful if you've got nested transactions, because ROLLBACK TRANSACTION does not roll back to the last BEGIN TRANSACTION but to the first BEGIN TRANSACTION. A nested COMMIT TRANSACTION only decrements @@TRANCOUNT, only when @@TRANCOUNT is reduced to zero by a COMMIT TRANSACTION does the overall transaction get committed.
This surprises a lot of people.
If you need to roll back in the middle of a nested procedure and an error has NOT occurred (presumably you've detected some other logical error, not a physical error raised by SQL Server) you should use RAISERROR to set @@ERROR to something. Maybe you test whether @@TRANCOUNT is greater than zero in an error handler, in case the nested procedure you called has already rolled back. IIRC, you get an error if you try to roll back when the nested transaction count is already zero.
|
|
|
|
|
You're right, and I know this, but there seems to be no indication in the OP of a nested transaction - hence my comments and the modified version of the code.
-- modified at 10:45 Wednesday 25th July, 2007
[Edit]Reading my original post I see that I did mention the transaction count of 1 before the commit transaction leading to a transaction count of 0 afterwards.[/Edit]
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
okkishore wrote: how we will recover from database was lost or scatch.
It was deleted? Corrupted? The hard drive was thrown into a vat of acid?
okkishore wrote: when there no is backrecovery
You mean you wern't keeping backups?
If you've completly lost the database and you have no backup then it's gone and your not getting it back. Maybe in future you'll do backups.
If it's not completly gone you might be able to use an undelete program or fix the corruption.
EDIT: I just checked your post below. Did you try and get rid of the duplicates without backing up and then delete the entire thing by accident?
|
|
|
|
|
Well the wording you've used is complete nonsense.
|
|
|
|
|
What is the OP asking? I understand that it was an interview question, but how was the database lost?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
okkishore wrote: how we will recover from database was lost or scatch
Huh? What kind of stupid interview question was that? Without backing up the database, losing it or having the disc being scratched is pretty much a nail in the coffin for the database, unless you have very pricey hardware to pull data bits off hard disk platters.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
how can we delete duplicate rows when we are not using constraints
usings query only
kishore kumar.B
|
|
|
|
|
Select all the field you are concerned about and do a group by with a having
Something like this:
Select col1, col2, col3, col4, col5 from yourTable
group by col1, col2, col3, col4, col5
having count(*) > 1
Hope that helps.
Ben
|
|
|
|
|
it can't deleting it only selecting.
for select we are also
select select DISTINCT * from tablename
i need for deleting not for selecting
kishore
|
|
|
|
|
The query I gave you is to identify the duplicates. Once you have the duplicates there are several things you can do to delete them. If you already know how to identify the duplicates then you should be able to delete them. You can not use a distinct since that removes the duplicates. Do you have any id column or key column to uniquely identify each column?
Ben
|
|
|
|
|
any one i what that means the table that can have duplicate rows or particular rows.i already mension i can't give any constraints .
|
|
|
|
|
Ok well, if you don't have any unique key you should create one. Add a new column that is an identity. Then if you really don't which record you keep you can do a delete statement like this:
delete from yourTable yt
join
(Select max(id),col1, col2, col3, col4, col5 from yourTable
group by col1, col2, col3, col4, col5
having count(*) > 1) temp on yt.id = temp.id
Ben
|
|
|
|
|
Hi all,
I am using SQLServer2000,
I am trying to insert large amount of data into column of a table but i found only part of the data getting inserted into the table.
Then i have tried the following option to set the column size
sp_tableoption 'testmail', 'text in row', '7000'
After the above line get executed its better but still i am lagging behind to store total text .please help me some one
Thanks in advance
Yuva
|
|
|
|
|
the most data you can store in a row is 8000 bytes (so thats 8000 ascii charecters or 4000 unicode). If you need to store more than that you need to chop up the text and store it in multiple rows.
|
|
|
|
|
hi originSH ,
Thanks for your reply but my requiremet is store all the date ito one column i cannot chop up teh thing because i am getting that text dynamically..The text in the form of html
Thanks in Advance
Chandra Gupta
|
|
|
|
|
Change the datatype to text (or ntext for Unicode). Text stores 2^31 characters, and NText stores 2^30 characters.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
hi Pete O'Hanlon,
I have used text data type only but it is not allowing me store whole text(Actually the text in the form of HTml) into my Database
|
|
|
|
|
Hi There,
I need some assistance with a database problem.
I am using Microsoft SQL Server 2005, one of the tables primary key has been deleted. How can I check when the changed was made and by which user as there are multiple users with administrative rights?
All of your help is greatly appreciated.
Regards,
BassRider
|
|
|
|
|
check your transaction log - if someone is messing around with this kind of stuff get triggers or alerts set up on your database when they are modifed, that way you can find out imediately and record user, date, time to get a better iddea of what's going on!
|
|
|
|
|
Hi,
In SQl seerver 2005 we have a table with a coloum as Image . data type In the Stored procedure we are using Insert statement to add the image stream to coloum. All our image files are nearly 400 KB of size. The insert statement is doing its job.Our problem its talke nearly 18 seconds to do the insert. Is there any way we can tune up performance or any alternates for it.
Regard
|
|
|
|
|
does this have to be stored in sql server? it is not a recommended practice, a better solution would be to store them in a location and hold a simple location within your database.
|
|
|
|
|
Its a customer requirement.
Is there an way to solve it.
|
|
|
|