Thursday, February 26, 2015

SQL Query to Backup all the databases in SQL 2008


 DECLARE @name VARCHAR(50) -- database name   
 DECLARE @path VARCHAR(256) -- path for backup files   
 DECLARE @fileName VARCHAR(256) -- filename for backup   
 DECLARE @fileDate VARCHAR(20) -- used for file name   
 SET @path = 'C:\Backup\'   
 SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)   
 DECLARE db_cursor CURSOR FOR   
 SELECT name   
 FROM master.dbo.sysdatabases   
 WHERE name NOT IN ('master','model','msdb','tempdb')   
 OPEN db_cursor   
 FETCH NEXT FROM db_cursor INTO @name   
 WHILE @@FETCH_STATUS = 0   
 BEGIN   
 SET @fileName = @path + @name + '_' + @fileDate + '.BAK'   
 BACKUP DATABASE @name TO DISK = @fileName WITH COMPRESSION  
 FETCH NEXT FROM db_cursor INTO @name   
 END   
 CLOSE db_cursor   
 DEALLOCATE db_cursor   

Thursday, February 19, 2015

Disable and Enable all the constraints in a SQL database


 Disable all the constraint in database

 EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"  


Enable all the constraint in database

 EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"  

Wednesday, February 18, 2015

How to set a default button for div


Using this code


 onkeypress="return WebForm_FireDefaultButton(event, '<%=ButtonName.ClientID %>')"  

Example


 <div style="float: left" onkeypress="return WebForm_FireDefaultButton(event, '<%=btnCheckPin.ClientID %>')">  
       <asp:TextBox ID="txtPincode" runat="server" Style="padding: 5px;"></asp:TextBox>  
       <input type="button" class="common_button" onclick="checkDelivery();" value="Check" ID="btnCheckPin" runat="server" />  
  </div>  



Sunday, February 8, 2015

Add a column, with a default value, to an existing table in SQL Server



Syntax


 ALTER TABLE {TABLENAME}   
 ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}   
 CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}  
 [WITH VALUES]  

Example


 ALTER TABLE table  
 ADD column BIT   
 CONSTRAINT Constraint_name DEFAULT 0 WITH VALUES  


Note : WITH VALUES handles the NOT NULL part...