Tag - SQL Server

Some tips for improving the performance of SQL Server queries

When I observe performance optimizations for online apps, I often see them done at the application layer or by validating the index existence on a database table field column (s). The optimization of SQL queries receives very little attention. Even experienced architects and developers have a tendency to overlook the need of understanding how...

How to apply or execute a T-SQL command to every database in your server

We talked before on How to  apply or execute a T-SQL command to every tables in your database. Now we are going to have a quick look on the same but for databases not tables … There is a handy undocumented stored procedure that allows you to do this without needing to set up a cursor...

How to execute a T-SQL command to every table in SQL Server database

sp_MSforeachtable is a stored procedure that is mostly used to apply a T-SQL command to every table, iteratively, that exists in the current database. Examples on how to use it: 1-Perform an unconditional reindex over all tables in the database: EXEC sp_MSforeachtable 'DBCC DBREINDEX(''?'')' 2-Truncate all tables in the database: EXEC sp_MSforeachtable 'TRUNCATE TABLE ?' 3-Get the information about the...

How to format DateTime in SQL Server

When expression is a date or time data type, style can be one of the values shown in the below Text: 101 >>>>>>>> mm/dd/yy102 >>>>>>>> yy.mm.dd103 >>>>>>>> dd/mm/yy104 >>>>>>>> dd.mm.yy111 >>>>>>>> yy/mm/dd114 >>>>>>>> hh:mi:ss:mmm(24h) we will assume that we have a table contain a DateTime data type field, the result of the below query will be...

How to use SQL Membership Provider in ASP.NET

The ASP.NET membership feature provides secure credential storage for application users. We will use the following: Web Page Named Login.aspx and another Web Page named CreateUser.aspxLogin Control.Create User Wizard Control. Steps: 1- Configure Forms Authentication in Web.config File To configure forms authentication, set the <authentication> element's mode attribute to "Forms" and then configure your application's Web.config file as shown...

Adding the specified count to the semaphore would cause it to exceed its maximum count

I got this unusual exception while working on an ASP.NET project:"Adding the specified count to the semaphore would cause it to exceed its maximum count error". I thought it is something related to SQL Server Database Connection String.After using Query Profiler I was able to run SQL queries directly without any problems.So this means that...

How to select from Datatable in C#.NET

DataTable Object has a Select Method which acts like Where in SQL Server. So if you want to write filter query you write the column name directly without Where and the filter expression you want. I made a simple example that creates a DataTable and fill it with some data and use Select method: DataTable dt...

How to get year, month and say out of SQL Date in SQL Server

Let is say that we have the following Date 2007-04-05 10:13:14.109 and we want to get Year, Month and Day out of this date. There is a function named DATEPART() that can be used to archive this. DECLARE @MyDate AS DATETIME SET @MyDate = '2007-04-05 10:13:14.109' SELECT DATEPART(DAY, @MyDate) AS MyDay, DATEPART(MONTH, @MyDate) AS MyMonth, DATEPART(YEAR, @MyDate) AS MyYear The result...

Difference between inner and outer join in SQL

Joins are used to combine the data from two tables, with the result being a new, temporary table.The temporary table is created based on column(s) that the two tables share, which represent meaningful column(s) of comparison.The goal is to extract meaningful data from the resulting temporary table.Joins are performed based on something called a...