Skip to main content

Posts

Showing posts with the label SQL Server

Exit a T-SQL Cursor When Condition is met

Have you ever wanted to exit from a cursor when a condition is met? I wanted to do it. So this is how I did it. DECLARE @Field1 AS INT DECLARE @Field2 AS INT DECLARE CursorName CURSOR READ_ONLY FOR SELECT Field1, Field2 FROM TableName OPEN CursorName FETCH NEXT FROM CursorName INTO @Field1, @Field2 WHILE @@FETCH_STATUS = 0 BEGIN IF @Field1 = 1 BEGIN GOTO ENDCURSOR END FETCH NEXT FROM CursorName INTO @Field1, @Field2 END ENDCURSOR: CLOSE CursorName DEALLOCATE CursorName I have set my fonts to bold where you want to notice. So that's all I hope you will get something out of it and it is true that this is not a big deal. :)

Simulate Table Lock situation using a Select statement - SQL Server 2005

Today I wanted simulate a table lock situation in my database using a select statement. It was bit hard for me to do it. I was searching it in web, but couldn't find useful thing. May be I didn't do it in right way. ;) Anyway I could do it. This is how I did it. You can write your SQL statement with (TABLOCKX) at the end of SQL statement. ex: Select * From table_name (TABLOCKX) I assume you all know how to do the rest of it.