Microsoft SQL ServerImplementation of programming language SQL
Microsoft SQL Server (sometimes referred as just SQL Server) is a relational database management system created by Microsoft Corporation.
SQL Server works only under Windows platform.
SQL Server uses a procedural extension for SQL called T-SQL (Transact-SQL). Pure SQL used in SQL Server differs from other SQL implementations, and is sometimes referred as T-SQL dialect.
T-SQL supports program units (as procedures which can be compiled and stored in the database and executed later and anonymous blocks), control flow statements like conditional statemens and loops and local variables. Besides, T-SQL adds a number of functions for string and date processing, math functions etc.
Tools which support database development and administration for SQL Server provided by Microsoft Corporation include:
- SQLCMD — a command-line tool for executing SQL commands interactively or from a script; is commonly used as a default installation interface.
- Microsoft Visual Studio includes support for data programming with SQL Server.
- SQL Server Management Studio — an IDE for confugiruing and managing SQL Server database.
Hello, World!:Example for versions Microsoft SQL Server 2005, Microsoft SQL Server 2008 R2, Microsoft SQL Server 2012, MySQL 3.23.57, PostgreSQL 9.1
select 'Hello, World!';
Fibonacci numbers:Example for versions Microsoft SQL Server 2005, Microsoft SQL Server 2008 R2, Microsoft SQL Server 2012
This example uses a kind of iterative definition of Fibonacci numbers, implemented with a recursive query. Each row of recursive query contains two consecutive numbers of the sequence, and next row is calculated as (last number, sum of numbers) of previous row. This way most numbers are stored twice, so only first number of each row is included in the result.
with fibonacci(a, b) as ( select 1, 1 union all select b, a+b from fibonacci where b < 1000 ) SELECT cast(a as varchar)+', ' AS [text()] FROM fibonacci FOR XML PATH ('')
Factorial:Example for versions Microsoft SQL Server 2005, Microsoft SQL Server 2008 R2, Microsoft SQL Server 2012
This example uses recursive factorial definition, implemented as a recursive query — a feature included in SQL Server 2005 and higher. Each row of recursive query contains two numeric fields — n and f = n!, and each row is calculated using the data of the previous row.
Note that starting with 2008 version you can calculate factorials only up to 12!. Trying to calculate 13! results in “Data truncation” error, i.e. the resulting value is too large for its datatype.
with factorial(n, f) as ( select 0, 1 union all select n+1, f*(n+1) from factorial where n<16 ) select cast(n as varchar)+'! = '+cast(f as varchar) from factorial
Quadratic equation:Example for versions Microsoft SQL Server 2005, Microsoft SQL Server 2008 R2, Microsoft SQL Server 2012
CREATE FUNCTION quadratic_equation (@a FLOAT ,@b FLOAT ,@c float ) RETURNS varchar (100) AS BEGIN DECLARE @ret varchar(100); declare @delta float ; declare @denom float ; select @delta=(POWER(@b,2)-4*@a*@c); select @denom=(2*@a); if (@delta>=0) BEGIN DECLARE @x12 FLOAT; SELECT @x12=((-@b+SQRT(@delta))/@denom); SELECT @ret='x1='+ CONVERT(varchar(100),@x12 ) +' x2='+ CONVERT(varchar(100),@x12 ) END ELSE BEGIN DECLARE @nat FLOAT; DECLARE @imm FLOAT; SELECT @nat=((-@b)/@denom); SELECT @imm=((SQRT(-@delta))/@denom); SELECT @ret='x1='+ CONVERT(varchar(100),@nat)+'+i'+CONVERT(varchar(100),@imm ) +' x2='+ CONVERT(varchar(100),@nat)+'-i'+CONVERT(varchar(100),@imm ) end RETURN @ret ; END;