The following are a list of good practices I follow when querying large sets of data. SELECT * Avoid selecting all columns from a table. Queries should explicitly list each column it requires Changing column definitions on the underlying tables can have an adverse effect on dependencies such as views, name clashes etc Avoid reading and returning unnecessary data Can… Read more →
Category: sql
Generate POCO’s from Sql Server
DECLARE tableCursor CURSOR LOCAL FOR SELECT name, ‘Foundation’, name + ‘Dto’ FROM sys.tables WHERE 1 = 1 OPEN tableCursor DECLARE @tableName NVARCHAR(MAX), @schemaName NVARCHAR(MAX), @className NVARCHAR(MAX) FETCH NEXT FROM tableCursor INTO @tableName, @schemaName, @className WHILE @@FETCH_STATUS = 0 BEGIN DECLARE tableColumns CURSOR LOCAL FOR SELECT cols.name, cols.system_type_id, cols.is_nullable FROM sys.columns cols JOIN sys.tables tbl ON cols.object_id = tbl.object_id WHERE tbl.name… Read more →
How to test a database connection
Create a blank text file called something along the lines of TestConnection.udl Double click the file Test! Read more →
Sql Maintenance Scripts
I’m adding the links here in the hope that I stop losing them! Backup Integrity Check Index and Statistics Maintenance MaintenanceSolution.sql Read more →
Restore A SQL User After a SQL Database Restore
Find missing users: EXEC sp_change_users_login ‘Report’ Create the user if it does not exist: CREATE LOGIN ‘UserName’ WITH PASSWORD= ‘password’ Restore the user: EXEC sp_change_users_login ‘Update_One’, ‘UserName’, ‘UserName’ Read more →
Sql Server Shrink Log File
Easy with Simple Recovery DBCC SHRINKFILE (“<FileName>_Log”, 1); GO otherwise http://social.technet.microsoft.com/Forums/sqlserver/en-US/0a7a7fc5-d30e-4841-8ed1-84676b575e55/sql-server-2012-how-to-shrink-the-database-transaction-log USE AdventureWorks2008R2; GO — Truncate the log by changing the database recovery model to SIMPLE. ALTER DATABASE AdventureWorks2008R2 SET RECOVERY SIMPLE; GO — Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (AdventureWorks2008R2_Log, 1); GO — Reset the database recovery model. ALTER DATABASE AdventureWorks2008R2 SET RECOVERY FULL; GO Read more →
SQL Server Maintenance
http://www.sqlservercentral.com/scripts/Backup/62380/ Backup Integrity check Index & statistics maintenance Read more →
SQL Server count all rows in all tables
SELECT sysobjects.Name , sysindexes.Rows FROM sysobjects INNER JOIN sysindexes ON sysobjects.id = sysindexes.id WHERE type = ‘U’ AND sysindexes.IndId < 2 ORDER BY sysobjects.Namecode Read more →
How to run code from sql server in c#
How to run code from sql server in c# SqlCommand cmd = new SqlCommand(“select Developer from Stackoverflow where UserID=’786′ and Developer=’Sufiyan'”, con); if (con.State == ConnectionState.Closed) { con.Open(); } SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { string codeSnippet = reader[“CodeSnippet”].ToString(); dynamic script = CSScript.LoadCode(@” using System; using System.Windows.Forms; using System.Collections.Generic; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; public class RunFromSqlServer {… Read more →
My 3 Favorite Connection String Tips
My 3 Favorite Connection String Tips Read more →