SQL Server Transaction

BEGIN TRANSACTION BEGIN TRY — put SQL commands here INSERT/UPDATE/Delete — if successful – COMMIT the work COMMIT TRANSACTION END TRY BEGIN CATCH — handle the error case (here by displaying the error) SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage — in case …

Dynamically create columns in a temp table and generate pivot report

Sample Data download: http://goo.gl/lcOf8z Generate excel report as below. Username, Address, City, State, Zip, Question 1, Question 2, Question 3, ….. Prakash, test, anand, gujarat, 388001, Answer 1, Answer 2, Answer 3,.. Chirag, test, anand, gujarat, 388001, Answer 1, Answer 2, Answer 3,.. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO — ============================================= — Author: …

How to schedule and automate backups of SQL Server databases in SQL Server Express

Create store procedure. // Copyright © Microsoft Corporation. All Rights Reserved. // This code released under the terms of the // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) USE [master] GO /****** Object: StoredProcedure [dbo].[sp_BackupDatabases] ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO — ============================================= — Author: Microsoft — Create date: 2010-02-06 — Description: Backup Databases …

User Dynamic query for search records and use uniqueidentifier

Declare @SearchText nvarchar(max) set @SearchText = ‘sah.CheckInCheckOutDateTime>=”2011-09-28 10:00:00.000”’ Declare @SchoolYearId uniqueidentifier set @SchoolYearId=’25511F7F-8F87-4085-A242-2004F4419D36′ Declare @Str as nvarchar(max) set @Str=’ select s.LastName +”, ” + s.FirstName as ”Name”,CheckInCheckOut,sah.CheckInCheckOutDateTime as ”ClockOut” from StaffAttendenceHistory sah join Staffschoolyear ssy on sah.staffschoolyearid = ssy.id join Staff s on ssy.staffid = s.id where ssy.SchoolYearId=@i’ If(LEN(ISNULL(@SearchText,0))>0) set @Str = @Str + ‘ …