background preloader

SQLServer

Facebook Twitter

Using Multiple Active Result Sets (MARS) SQL Server 2005 introduced support for multiple active result sets (MARS) in applications accessing the Database Engine. In earlier versions of SQL Server, database applications could not maintain multiple active statements on a connection. When using SQL Server default result sets, the application had to process or cancel all result sets from one batch before it could execute any other batch on that connection. SQL Server 2005 introduced a new connection attribute that allows applications to have more than one pending request per connection, and in particular, to have more than one active default result set per connection. MARS simplifies application design with the following new capabilities: Applications can have multiple default result sets open and can interleave reading from them.

Applications can execute other statements (for example, INSERT, UPDATE, DELETE, and stored procedure calls) while default result sets are open. SQL Server Native Client OLE DB Provider Example. Database System | Performance & Scalability | SQL Server Express Edition. Things you can do Build web and mobile applications for multiple data types Support structured and unstructured data while storing business data with native support for relational data, XML, and spatial data.

Add geographical information to business applications and build location-aware applications. Increase granularity of temporal data with date and time data types. Easily manage SQL Server instances Manage SQL Server Express databases with SQL Server Management Studio Express. Connect to local SQL Server Express databases and manage objects with full Object Explorer integration. Basic reporting services Visualize data through basic Reporting Services (available with SQL Server Express with Advanced Services) and create readable reports that answer complex user questions. Simplify and speed development with rich developer tools Take advantage of existing Transact-SQL skills, and incorporate technologies like Microsoft ADO.NET Entity Framework and LINQ. Tibor Karaszi's SQL Server pages. Overview The purpose of this article is to explain how the datetime types work in SQL Server, including common pitfalls and general recommendations. Thanks to SQL Server MVP Frank Kalis, this article is translated to German.

There is also a Russian version, thanks to Evgeny Vinchyk from Ukraine, available as a word document. Acknowledgements I like to thank the following persons who provided valuable suggestions and input for this article: Steve Kass, Aaron Bertrand, Jacco Schalkwijk, Klaus Oberdalhoff, Hugo Kornelis, Dan Guzman and Erland Sommarskog. Versions of SQL Server This article applies to SQL Server 7.0, 2000, 2005 and 2008, where not noted otherwise. Date and time types in SQL Server Prior to SQL Server 2008, there are two types in SQL Server to handle date and time.

Note that there is no type to store only date or only time. SELECT CAST('20041223' AS datetime) ----------------------- 2004-12-23 00:00:00.000 As you can see, we have finally a date-only and a time-only type. What? Sql server - SQL Service Broker and .NET Windows Service - Best practices. Sql - Tinyint vs Bit. Accessing Data with ADO. For performance and reliability reasons, it is strongly recommended that you use a client-server database engine for the deployment of data driven Web applications that require high-demand access from more than approximately 10 concurrent users. Although ADO works with any OLE DB compliant data source, it has been extensively tested and is designed to work with client server databases such as SQL Server. ASP supports shared file databases (Access or FoxPro) as valid data sources. Although some ASP example code use a shared file database, it is recommended that these types of database engines be used only for development purposes or limited deployment scenarios.

Shared file databases may not be as well suited as client-server databases for very high-demand, production-quality Web applications. If you are developing an ASP database application intended to connect to a remote SQL Server database you should also be aware of the following issues: Choosing Connection Scheme for SQL Server. Peter DeBetta's SQL Programming Blog : T-SQL UrlEncode. The curse and blessings of dynamic SQL. An SQL text by Erland Sommarskog, SQL Server MVP.

Latest revision: 2011-06-23. An earlier version of this article is also available in German. Translations provided by SQL Server MVP Frank Kalis. Introduction If you follow the various newsgroups on Microsoft SQL Server, you often see people asking why they can't do: SELECT * FROM @tablename SELECT @colname FROM tbl SELECT * FROM tbl WHERE x IN (@list) For all three examples you can expect someone to answer Use dynamic SQL and give a quick example on how to do it.

In this article I will discuss the use of dynamic SQL in stored procedures and to a minor extent from client languages. The article covers all versions of SQL Server from SQL 6.5 to SQL 2008, with emphasis on SQL 2000 and later versions. Contents: Note: many of the code samples in this text works against the pubs and Northwind databases that ship with SQL 2000 and SQL 7, but not with SQL 2005 and later. Accessing Data from an Application Introducing Dynamic SQL A First Encounter. SSMS Tools Pack Features. How to export data from SQL Server 2008.2010 in DML (SQL script) Query - How to read a Text file using T-SQL. Sql server - T-SQL: Opposite to string concatenation - how to split string into multiple records. Peter DeBetta's SQL Programming Blog : T-SQL UrlDecode. INSERT (Transact-SQL) Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) Adds one or more rows to a table or a view in SQL Server.

Transact-SQL Syntax Conventions Syntax -- External tool only syntax INSERT { [BULK] { database_name.schema_name.table_or_view_name | schema_name.table_or_view_name | table_or_view_name } ( <column_definition> ) [ WITH ( [ [ , ] CHECK_CONSTRAINTS ] [ [ , ] FIRE_TRIGGERS ] [ [ , ] KEEP_NULLS ] [ [ , ] KILOBYTES_PER_BATCH = kilobytes_per_batch ] [ [ , ] ROWS_PER_BATCH = rows_per_batch ] [ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ] [ [ , ] TABLOCK ] ) ] } [; ] <column_definition> ::= column_name <data_type> [ COLLATE collation_name ] [ NULL | NOT NULL ] <data type> ::= [ type_schema_name . ] type_name [ ( precision [ , scale ] | max ] Arguments INTO Is an optional keyword that can be used between INSERT and the target table. server_nameApplies to: SQL Server 2008 and later.

Important A. SELECT @local_variable (Transact-SQL) Storing Variable Values in Temporary Array or Temporary List. SQL Server does not support arrays or a dynamic length storage mechanism like list. Absolutely there are some clever workarounds and few extra-ordinary solutions but everybody can;t come up with such solution. Additionally, sometime the requirements are very simple that doing extraordinary coding is not required. Here is the simple case. Let us say here are the values: a, 10, 20, c, 30, d. Now the requirement is to store them in a array or list.

For SQL Server 2012:DECLARE @ListofIDs TABLE(IDs VARCHAR(100));INSERT INTO @ListofIDsVALUES('a'),('10'),('20'),('c'),('30'),('d');SELECT IDs FROM @ListofIDs;GO When executed above script it will give following resultset. Above script will work in SQL Server 2012 only for SQL Server 2008 and earlier version run following code.

DECLARE @ListofIDs TABLE(IDs VARCHAR(100), ID INT IDENTITY(1,1));INSERT INTO @ListofIDsSELECT 'a'UNION ALLSELECT '10'UNION ALLSELECT '20'UNION ALLSELECT 'c'UNION ALLSELECT '30'UNION ALLSELECT 'd';SELECT IDs FROM @ListofIDs;GO. Declare a variable list in SQL Server stored procedure. Database Programming: TOP Without ORDER BY? - Ward Pond's SQL Server blog. Slow in the Application, Fast in SSMS? An SQL text by Erland Sommarskog, SQL Server MVP. Last revision: 2013-08-30. This article is also available in Russian, translated by Dima Piliugin. Introduction When I read various forums about SQL Server, I frequently see questions from deeply mystified posters. No, SQL Server is not about magic. To understand how to address that performance problem in your application, you need to read on. Table of Contents Presumptions The essence of this article applies to all versions of SQL Server, but the focus is on SQL 2005 and later versions.

For the examples in this article I use the Northwind sample database. This is not a beginner-level article, but I assume that the reader has a working experience of SQL programming. Caveat: In some places I give links to the online version of Books Online. How SQL Server Compiles a Stored Procedure In this chapter we will look at how SQL Server compiles a stored procedure and uses the plan cache. What is a Stored Procedure? Stored procedures. Overview. Using Large CLR UDTs in SQL Server 2008 | Development content from SQL Server Pro. New in SQL Server 2008 is support for large user-defined types (UDTs). We’re going to build a Common Language Runtime (CLR) UDT that allows us to store, compress, and decompress binary data (up to 2GB in size) inside SQL Server 2008. This technique works only in SQL Server 2008 because only SQL Server 2008 allows CLR UDTs larger than 8KB. The UDT we’ll build, called VarBinaryComp, accepts both uncompressed data (which it will compress before storing) and compressed data as input.

It also has properties to determine the length of the compressed and uncompressed data. To implement this UDT, we’ll leverage the compression/decompression logic covered in “Zip Your Data,” InstantDoc ID 49065. For background on compression fundamentals, you might want to review this article and its accompanying sample code before proceeding. CLR UDTs A little refresher course on CLR UDTs might be helpful as well. CLR UDTs aren’t intended to turn SQL Server into a full-blown object-oriented database. MD5 Hash SQL Server Extended Stored Procedure.

Fix : Error 1205 : Transaction (Process ID) was deadlocked on resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Fix : Error 1205 : Transaction (Process ID) was deadlocked on resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Deadlock occurs when two users have locks on separate objects and each user wants a lock on the other’s object. When this happens, SQL Server ends the deadlock by automatically choosing one and aborting the process, allowing the other process to continue. The aborted transaction is rolled back and an error message is sent to the user of the aborted process. Generally, the transaction that requires the least amount of overhead to rollback is the transaction that is aborted. Fix/Workaround/Solution: Deadlock priority can be set by user. To reduce the chance of a deadlock: Minimize the size of transaction and transaction times. SQL SERVER 2005 has new priority HIGH as well as numeric-priority.

Like this: Like Loading... DB2 Universal Database. Sql server - 900 byte index size limit in character length. SQL Server CREATE TABLE syntax diagrams. Sql - Are there any disadvantages to always using nvarchar(MAX) Maximum Capacity Specifications for SQL Server. The following table specifies the maximum sizes and numbers of various objects defined in SQL Server databases or referenced in Transact-SQL statements. 1Network Packet Size is the size of the tabular data stream (TDS) packets used to communicate between applications and the relational Database Engine.

The default packet size is 4 KB, and is controlled by the network packet size configuration option. 2The maximum number of bytes in any index key cannot exceed 900 in SQL Server. You can define a key using variable-length columns whose maximum sizes add up to more than 900, provided no row is ever inserted with more than 900 bytes of data in those columns.

In SQL Server, you can include nonkey columns in a nonclustered index to avoid the maximum index key size of 900 bytes. 3Database objects include objects such as tables, views, stored procedures, user-defined functions, triggers, rules, defaults, and constraints. 5This value is for static lock allocation. Indexing for Sort Performance | Data Access content from SQL Server Pro. Defining indexes on sort columns can yield dramatic performance results The right index can dramatically improve SQL Server's sort performance. Defining a clustered index on a sort column, for example, forces the database to store data in sorted order, letting you retrieve data without additional sorting. Note that SQL Server 7.0 and earlier releases let you create indexes only in ascending order, so if your query requires data in descending order, you'll probably have to perform additional sorting and use internal worktables to get data in the right order.

However, SQL Server 2000 lets you create indexes in ascending or descending order. SQL Server 7.0 performs a sort operation when you use the ORDER BY clause. SQL Server's query optimizer also might use a sort operation to process a query that uses GROUP BY, DISTINCT, or UNION. In contrast, you can use the FAST index hint to avoid sorting the data. Sorting with GROUP BY SELECT customerid FROM orders GROUP BY customerid DISTINCT and UNION. Clustered Index and Non-Clustered Index in SQL Server. Here we are going to discuss clustered and non-clustered indexes in SQL Server; to understand these indexes clearly we should first know what database indexes are.

So let's have a look at database indexes. Database indexes are copies of your data in a table that is sorted a certain way. Database administrators can gain potential performance benefits through the judicious use of indexes on database tables. Indexes allow you to speed query performance on commonly used columns and improve the overall processing speed of your database.

Also we can say that an index is used to fetch the data in a reliable and fast manner. SQL Server supports two main types of indexes: Clustered and Non-Clustered. In SQL Server the clustered indexes are a critical consideration in the overall architecture of the database. Only 1 clustered index is allowed per table so choose wisely and we should consider choosing the columns on which this index will be created carefully. Structure of Clustered Index: Full-text: Immediate deadlock notifications without changing existing code. In my previous post about immediate deadlock notifications in SQL Server 2005 I've shown a way to use a try catch block to get the deadlock error. The con of this method, although it uses best practice for error handling in SQL Server 2005, is that you have to change existing code and it doesn't work for non stored procedure code. And that IS a pretty BIG con! As is customary in this blog there is a solution to this. :) SQL Server 2005 Event notifications Event notifications are a special kind of database object that send information about server and database events to a Service Broker service.

They execute in response to a variety of Transact-SQL data definition language (DDL) statements and SQL Trace events by sending information about these events to a Service Broker service. We of course want a Server wide deadlock notification so that we can be notified of all deadlocks on the entire server I have to point out that event notification are an awesome use of Service Broker functionality. SQL String User Function Workbench: part 2. /* In which Robyn and Phil pull together the themes from their TSQL String Array Workbench and String User Function workbench, to provide a simple TSQL string-handling package. */ This workbench finishes of what has been a three-part series of string functions.

In it, we introduce the idea of using XML to provide a very simple array for doing string handling. This allows us to use functions for searching and splitting strings that will be familiar to users of procedural languages such as PHP and Python. The first part, TSQL String Array Workbench showed how the basics worked and then demonstrated how it could be used with a PHP-style string function. What inspired us to write this workshop was when Phil had to endure a PHP programmer sounding off about how much better PHPs string handling as than TSQL. Of course, this principle could be extended to arrays and matrices. -- If @Delimiter is not specified or is None, a different -- splitting algorithm is applied. P.s. TSQL String Array Workbench.

SQL Server Backup, Integrity Check, Index and Statistics Maintenance. SourceForge.net: SQL-Viewer - Project Web Hosting - Open Source Software. SourceForge.net: Remote MySQL Viewer - Project Web Hosting - Open Source Software. BasicQuery SourceForge Project Homepage. T-SQL Developer project home page. SQL Buddy Sourceforge Site. Transact-SQL Analyzer | Free Development software downloads. C# - SQL: Update a row and returning a column value with 1 query.

LINQ to XSD - Home. URLEncode. User Defined Functions in Microsoft SQL Server 2000. Export data from SQL Server to MS Access. Statistics in SQL Server. Named Pipe in ADODB Connection. How to Connect to a SQL Server Named Instance. Timeout error when trying to delete records. Overview of the SQL Server Browser service. Andy Leonard : Quick Database Connectivity Testing. Named Pipes Provider, error: 40 - Could not open a connection to SQL Server - SQL Protocols. Sql server - How to get a list of users for all instance's databases. SQL Server Named Pipe Connection... Asp classic - Server-side Javascript in production fails to open connection to a named instance of SQL2008. How To Set the SQL Server Network Library in an ADO Connection String. Aaron Bertrand : Bad habits to kick : choosing the wrong data type. Chastised for recommending use of bit data type. SQL Fiddle.

CONQUEST SOFTWARE SOLUTIONS - Tools for Oracle development, database administration, SQL and PL/SQL formatting, analysis and flowcharting, oracle development tools, pl/sql gui, plsql ide, pl/sql tools, pl/sql ide, pl/sql environment, pl/sql development, p. SQL Developers - Mimer SQL-2003 Validator. Subqueries with NOT EXISTS. Sys.objects (Transact-SQL) Meta Data Functions. IF...ELSE. Sql server - How do you perform an IF...THEN in an SQL SELECT.

CAST and CONVERT. Sql server 2000 - SQL2000 safely cast a VARCHAR(256) to INT. Datetime (Transact-SQL) Int, bigint, smallint, and tinyint. Decimal and numeric. Float and real. Data Types. Ntext, text, and image.