Retrieving single row within transaction hangs - sql-server

I run several SQLs ( select, update and insert ) within single database connection and transaction, one of the SQLs is to retrieve the balance of an account
select top 1 balance from sample_table where account_id={accountId} order by id desc
This SQL hangs only and only if there are no records actually in the table "sample_table" for the account {accountId} , otherwise it works normally.
Hangs means it waits until a timeout exception error occurs, and during this 'wait' , executing the SQL from Sql Server management studio also hangs.
I'm using Sql Server 2008
thanks
ِEDIT:
After multiple attempts to perform same action, it does work and all the records are inserted properly and then the app works like the charm after that.
Restore the database, then the issue is back again.

The issue is fixed after rebuilding the table, basically executing this SQL:
alter table sample_table rebuild

Related

SQL Server locks up on INSERT/SELECT when columns don't match...but only in SQL Server Management Studio

I recently managed to lock up our SQL Server database with a query like this (simplified for clarity):
SELECT *, NULL AS extraColmn
INTO #tempTable
FROM sourceTable
IF ##ROWCOUNT > 0
BEGIN
BEGIN TRANSACTION;
DELETE FROM sourceTable
INSERT INTO sourceTable
SELECT * FROM #tempTable;
COMMIT TRANSACTION;
DROP TABLE #tempTable
END
There are two obvious problems with this. First, the INSERT/SELECT statement should have the columns explicitly listed. Second, when doing that INSERT, the source and destination tables had a different number of columns. As expected, this caused an error:
Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.
And the query simply failed on our production server.
But strangely, when I ran this same query in SQL Server Management Studio, it gave that error, but then locked up the entire database, to the point where I had to restart the database server.
Why? Is it the transaction? I'm a newbie at those, but I understood that if an error is encountered in a transaction, it's simply rolled back—that's the point. (Note that sourceTable is fairly heavily used on this site, so there would probably have been a few concurrent attempts to access it while this was running.)
Why would this only occur in SQL Server Management Studio?
IIRC, the default settings in SSMS mean that an error will cause the rest of the batch (until the next GO or the end of the commands) to be skipped. So the transaction would still be open and bound to your SSMS window's session. So if you don't either close the session window or else explicitly rollback the transaction in that window, it will stay open until you do (or you restart the server).
This is what I assume happened to you. The simple fix is normally to just disconnect or close the SSMS session window where the error was thrown. Alternatively you can manually execute a ROLLBACK command in that session so that you don't lose the window or have to re-connect.
A client program receiving this error will usually end up closing the connection (either through error-handling or because it crashes), thus ending the session and it's transaction.

SQL server -- Table not allowing insert or update of records

I have a Microsoft SQL database that we have been using for several years. Starting this morning a single table in the database is throwing a time-out error whenever we attempt to insert or update any records.
I have tried to insert and update through:
Microsoft Access ODBC
a .Net Program via Entity Framework
a stored procedure run as an automatic job -- that runs each morning
a custom query written this morning to test the database and executed through SQL Server Management Studio
Opening the table directly via 'Edit Top 200 Rows' and typing in the appropriate values
We have restarted the service, then restarted the entire server and continue to get the same problems. The remainder of the database appears to be working fine. All data can be read even from the affected table, and other tables allow updates and inserts to be run just fine.
Looking through the data in the table, I have not found anything that appears out of the ordinary.
I am at a loss as to the next steps on finding the cause or solution.
Its not a space issue is it ? try ...
SELECT volume_mount_point Drive,
cast(sum(available_bytes)*100 / sum(total_bytes) as int) as [Free%],
avg(available_bytes/1024/1024/1024) FreeGB
from sys.master_files f
cross apply sys.dm_os_volume_stats(f.database_id, f.[file_id])
group by volume_mount_point
order by volume_mount_point;

Data visibility between linked server

Please consider the following situation
I have Prod instance of SQL server 2012
Also Archive instance of SQL server express 2012
Prod sees Archive as linked server and is able to write data with similar query from some .net code that creates a transaction and the transaction is committed at the end.
insert into <ArchiveServer>.<database>.<schema>.<table>
Select * from <ProductionServer>.<database>.<schema>.<table> Where <some conditions>
Now after the transaction finishes I am able to execute the following query
select count(1) from <ArchiveServer>.<database>.<schema>.<table>
and it returns correct number of records in the context of production server.
Same query
select count(1) from <database>.<schema>.<table>
in the context of Archive server returned 0 records.
What might be the problem? I am out of clues.
Thanks

Presql and postsql in Informatica is not getting executed

PreSQL and postSQL in Informatica is not getting executed.
ISSUE DESCRIPTION :
I have table in Microsoft SQL server. I am trying to update/insert this table using Informatica powercenter session by calling SP through Stored Procedure Transformation. But its not happening. After further digging up, I got to know that reason behind this are triggers on table that we are trying to update/insert. There are couple of triggers defined on the table and it has got on insert and on update triggers also. So I thought of disabling all the triggers on the table in PreSQL and enable them back again in postSQL of the session that I am running. But its not working.
However when I execute the trigger disable statement directly on DB through Microsoft SQL server client and run the session, session is updating/inserting the records.
Below are the Presql and postSQL commands used by me:
BEGIN TRANSACTION
ALTER TABLE schemaname.tablename DISABLE TRIGGER ALL
commit;
BEGIN TRANSACTION
ALTER TABLE schemaname.tablename ENABLE TRIGGER ALL
commit;
Please let me know if I am going wrong anywhere/if there is any possible resolution for this.
your sql gets parsed by powercenter before going to the db.
Check the server config - there should be some option to send unparsed sql.

Query takes for ever on SQL server to execute

Following is the error message I get when I tried to execute Select * for the table in SQl Server, Any suggestions would be appreciated.
"lock request time out period exceeded
sql server"
Look at SQL hints, in particular WITH (NOLOCK)
http://www.developerfusion.com/article/1688/sql-server-locks/4/
When you do you SELECT you're locking the table. But your SELECT takes so much time, that SQL ends it, in order not to keep the table locked, mainly because SQL server thinks it's a bug.

Resources