background preloader

Sql

Facebook Twitter

MongoDb

Sql Server Reference. Retrieve – Select Only Date Part From DateTime – Best Practice. 2008 – Introduction to Table-Valued Parameters with Example. Table-Valued Parameters is a new feature introduced in SQL SERVER 2008. In earlier versions of SQL SERVER it is not possible to pass a table variable in stored procedure as a parameter, but now in SQL SERVER 2008 we can use Table-Valued Parameter to send multiple rows of data to a stored procedure or a function without creating a temporary table or passing so many parameters.

Table-valued parameters are declared using user-defined table types. To use a Table Valued Parameters we need follow steps shown below: Create a table type and define the table structureDeclare a stored procedure that has a parameter of table type.Declare a table type variable and reference the table type.Using the INSERT statement and occupy the variable.We can now pass the variable to the procedure. For Example, Let’s create a Department Table and pass the table variable to insert data using procedure. Department: CREATE TABLE Department(DepartmentID INT PRIMARY KEY,DepartmentName VARCHAR(30) )GO 1. 2. 3. 4. 5.

Failed to generate a user instance of SQL Server. Why audit logging with triggers in MySQL is bad for replication. September 29, 2008 by Ewen Fortune10 Comments Recently I was tasked with investigating slippage between master and slave in a standard replication setup. The client was using Maatkit’s mk-table-checksum to check his slave data was indeed a fair copy of that of the master. mk-table-checksum --algorithm=BIT_XOR h=hostname.local,u=root,p=xxx --replicate=checksum.checksum --emptyrepltbl --chunksize=500000 --databases mydb --sleep 1 He could then examine the checksum.checksum table and see all was well, however there were various tables with different crc values. db: mydb tbl: Foo_History chunk: 0 boundaries: 1=1 this_crc: 30627c76fe658fd9b77eaddf1ea8c03a this_cnt: 2593 master_crc: bdbadd7dae2636a8cf515bb886fb1295 master_cnt: 2593 ts: 2008-09-24 04:50:05 So, now I needed to find out what was updating the table.

Mysqlbinlog bin_log.000001 | mysqlsla -lt binary - Check out for how to filter by statement. So whats the problem with that? Here is an example: Master: Sql Server. Triggers 2 - Creating Audit Trails - Author Nigel Rivett. CREATE INDEX (Transact-SQL) The CREATE INDEX statement is optimized like any other query. To save on I/O operations, the query processor may choose to scan another index instead of performing a table scan. The sort operation may be eliminated in some situations.

On multiprocessor computers CREATE INDEX can use more processors to perform the scan and sort operations associated with creating the index, in the same way as other queries do. For more information, see Configure Parallel Index Operations. The create index operation can be minimally logged if the database recovery model is set to either bulk-logged or simple. Indexes can be created on a temporary table. Indexes support extended properties.

Clustered Indexes Creating a clustered index on a table (heap) or dropping and re-creating an existing clustered index requires additional workspace to be available in the database to accommodate data sorting and a temporary copy of the original table or existing clustered index data. Unique Indexes Partitioned Indexes. Index (database) Usage[edit] Support for fast lookup[edit] Most database software includes indexing technology that enables sub-linear time lookup to improve performance, as linear search is inefficient for large databases. Suppose a database contains N data items and it is desired to retrieve one of them based on the value of one of the fields. A naïve implementation would retrieve and examine each item until a match was not found. A successful lookup would retrieve half the objects on average; an unsuccessful lookup all of them for each attempt.

This means that the number of operations in the worst case is O(N) or linear time. Since databases commonly contain millions of objects and since lookup is a common operation, it is often desirable to improve on this performance. An index is any data structure that improves the performance of lookup. Policing the database constraints[edit] Indexes are used to police database constraints, such as UNIQUE, EXCLUSION, PRIMARY KEY and FOREIGN KEY. Non-clustered[edit] James Gregory » SQL Server Object Exists Function. Update: Added separate versions for SQL Server 2000 and SQL Server 2005, due to the differences in the system objects tables.It may just be me, but when writing migration/create scripts for use with SQL Server I get quite agitated at having to write an ugly, long-winded, drop statement at the start of every object definition.

The support for dropping objects is one of the few things I would say MySQL has SQL Server over the barrel for.Baring in mind that if you try to drop an object that doesn’t exist, you’ll get an execution error; here’s how to drop a table in MySQL: Here’s how to drop the same table, if you’re using SQL Server: As always, when something annoys you enough and you’re in the middle of something else, it’s about time you wrote that solution.

So I’ve created a simple user-defined function that checks if an object exists then returns a BIT 0 or 1 depending. Common Object Types: P - Stored Procedure U - User Table FN - User-Defined Function SQL Server 2000 SQL Server 2005. Sql - How to SELECT * INTO [temp table] FROM [Stored Procedure] Select from a Stored Procedure. Thank you for your quick reply visakh, Yep, I did not explain myself very well did I lol - sorry! :( Okay, my situation:I have a stored procedure that I need to pass a number of parameters to which can return a different table structure depending on the parameters. I am creating another stored procedure to update some tables based on some of the information from columns from that original stored procedure.

Hope I have not lost you yet :) So, what I need is to have OpenQuery call a stored procedure with multiple parameters and have the output (in whatever structure it comes) from the stored procedure put into a temporary table. Ideally this would be the line but SQL Server Mgmt Studio complains about the '+' symbols: SELECT * INTO #Temp FROM OpenQuery(LinkedServer, 'EXEC spMyStoredProc ' + @iValue1 + ', ' + @iValue2); Please ask me if something is still not clear.

Rewriting Stored Procedures as Functions. This topic describes how to determine whether to rewrite existing stored procedure logic as user-defined functions. For example, if you want to invoke a stored procedure directly from a query, repackage the code as a user-defined function. In general, if the stored procedure returns a, single, result set, define a table-valued function.

If the stored procedure computes a scalar value, define a scalar function. If a stored procedure meets the following criteria, it is a good candidate for being rewritten as a table-valued function: The logic is expressible in a single SELECT statement but is a stored procedure, rather than a view, only because of the need for parameters. Sql server - How to call Stored Procedure in a View. Table-Valued Parameters in SQL Server 2008. Table-valued parameters provide an easy way to marshal multiple rows of data from a client application to SQL Server without requiring multiple round trips or special server-side logic for processing the data. You can use table-valued parameters to encapsulate rows of data in a client application and send the data to the server in a single parameterized command. The incoming data rows are stored in a table variable that can then be operated on by using Transact-SQL. Column values in table-valued parameters can be accessed using standard Transact-SQL SELECT statements.

Table-valued parameters are strongly typed and their structure is automatically validated. For more information about table-valued parameters, see the following resources. Before table-valued parameters were introduced to SQL Server 2008, the options for passing multiple rows of data to a stored procedure or a parameterized SQL command were limited. There are several limitations to table-valued parameters:

Transactions and Locks in SQL Server « SQLDBPOOL.COM. I 32 Votes A database transaction is a unit of work performed against a database management system or similar system that is treated in a coherent and reliable way independent of other transactions. A database transaction, by definition, must be atomic, consistent, isolated and durable. These properties of database transactions are often referred to by the acronym ACID. Transactions provide an “all-or-nothing” proposition stating that work units performed in a database must be completed in their entirety or take no effect whatsoever.

In some systems, transactions are also called LUWs for Logical Units of Work. The ACID model is one of the oldest and most important concepts of database theory. Let’s take a moment to examine each one of these characteristics in detail: states that database modifications must follow an “all or nothing” rule. Requires that multiple transactions occurring at the same time not impact each other’s execution. . * Begin Transaction * Rollback Transaction Syntax Database. T-SQL Enhancements in SQL Server 2005 > SNAPSHOT Isolation. SQL Server changes the state of a database by performing a transaction on it.

Each transaction is a unit of work consisting of one or more steps. A "perfect" transaction is ACID, meaning it is atomic, consistent, isolated, and durable. In short, this means that the result of performing two transactions on a database, even if they are performed simultaneously by interleaving some of the steps that make them up, will not corrupt the database. Atomic means that a transaction will perform all of its steps or fail and perform none of its steps. Consistent means that the transaction must not leave the results of a partial calculation in the database; for example, if a transaction is to move money from one account to another, it must not terminate after having subtracted money from one account but not having added it to another.

A transaction need not always be perfect. The isolation level of a transaction determines how close to perfect it is. Figure 7-1: Snapshot Versioning. SELECT FOR UPDATE with SQL Server. Transact-SQL User's Guide. Using temporary tables Temporary tables are created in the tempdb database. To create a temporary table, you must have create table permission in tempdb. create table permission defaults to the Database Owner. To make a table temporary, use the pound sign (#) or "tempdb.. " before the table name in the create table statement.

There are two kinds of temporary tables: Tables that can be shared among Adaptive Server sessionsYou create a shareable temporary table by specifying tempdb as part of the table name in the create table statement. If you do not use the pound sign or "tempdb.. " before the table name, and you are not currently using tempdb, the table is created as a permanent table. This statement creates a nonshareable temporary table: create table #myjobs (task char(30), start datetime, stop datetime, notes varchar(200)) You can use this table to keep a list of today's chores and errands, along with a record of when you start and finish, and any comments you may have. Warning! Microsoft SQL Server Product Samples: Database - Download: SQL Server 2008 SR4. How to determine the version and edition of SQL Server and its components. Step-by-Step: Installing SQL Server Management Studio 2008 Express after Visual Studio 2010 - Beth Massi - Sharing the goodness that is VB. UPDATE 7/31/2012 – Since this post new versions of SQL Server & Visual Studio have been released.

To install the latest version of SQL Server see SQL Server Install. For more information on working with SQL Server Local DB in Visual Studio 2012 see Working with SQL Server LocalDB in Visual Studio 2012 and the MSDN Library SQL Server 2012 Express LocalDB. One of the first things I always do after installing Visual Studio is to install SQL Server Management Studio (SSMS). Visual Studio 2010 installs SQL Server 2008 Express on your machine but doesn’t include SSMS. Although you can use Visual Studio to create/connect/design databases, I like having SSMS around for advanced management. I recall SSMS for SQL Server 2005 was a simple install, unfortunately they threw the kitchen sink into the SSMS 2008 installer and I’m always confused at which buttons I need to push to get it to do what I want. Okay the first thing you need is to make sure you get the right version of SSMS. Enjoy! A co-Relational Model of Data for Large Shared Data Banks.

Erik Meijer and Gavin Bierman, Microsoft Contrary to popular belief, SQL and noSQL are really just two sides of the same coin. Editor's note: Some of the characters in this article will not display properly in some browsers. If you have trouble, try using another browser or download the PDF file. Fueled by their promise to solve the problem of distilling valuable information and business insight from big data in a scalable and programmer-friendly way, noSQL databases have been one of the hottest topics in our field recently. The current noSQL market satisfies the three characteristics of a monopolistically competitive market: the barriers to entry and exit are low; there are many small suppliers; and these suppliers produce technically heterogeneous, highly differentiated products. 12 Monopolistically competitive markets are inconsistent with the conditions for perfect competition.

Today, the relational database market is a classic example of an oligopoly. Objects versus Tables ! Joe Celko The SQL Apprentice. Sql - Recursive CTE to find parent records. Recursive Queries Using Common Table Expressions. A common table expression (CTE) provides the significant advantage of being able to reference itself, thereby creating a recursive CTE. A recursive CTE is one in which an initial CTE is repeatedly executed to return subsets of data until the complete result set is obtained. A query is referred to as a recursive query when it references a recursive CTE. Returning hierarchical data is a common use of recursive queries, for example: Displaying employees in an organizational chart, or data in a bill of materials scenario in which a parent product has one or more components and those components may, in turn, have subcomponents or may be components of other parents.

The structure of the recursive CTE in Transact-SQL is similar to recursive routines in other programming languages. A recursive CTE consists of three elements: Invocation of the routine.The first invocation of the recursive CTE consists of one or more CTE_query_definitions joined by UNION ALL, UNION, EXCEPT, or INTERSECT operators. Sql server 2005 - How to self JOIN recursively in SQL.