SQL Server Admin
T-SQL Articles

November 29, 2011

SQL Server Download Links All Editions

1 comments


With SQL Server 2012 RC0 recently announced and a plethora of previous editions already available, it can get tedious to keep track of all these downloads. In this post, I will attempt to share the download links of all SQL Server versions - SQL Server 2012, SQL Server 2008 & R2 and SQL Server 2005. I will update this post and the links as and when applicable.

SQL Server 2012 Download Links

Microsoft SQL Server 2012 RC0 enables a cloud-ready information platform.


SQL Server 2008 R2 Download Links

SQL Server 2008 R2 VHD

SQL Server 2008 Download Links


SQL Server 2005 Download Links

SQL Server 2005 Evaluation is no longer available. You can use the Express Edition


Other SQL Server Download Links

SQL Server Migration Assistant for Oracle v5.1
SQL Server Driver 2.0 for PHP 5

Any important links that I missed out? Share it in the comments section and I will update the post!


 
  Feedback:

November 23, 2011

SQL Server: Test Linked Server Connection

0 comments


If you want to have access to data stored in a different server, one of the ways to do so is to make that server as a Linked Server to the current server and then query using the linked server name. Let us assume that there is a server named test available over the network and you want to get some data from there. System stored procedure sp_addlinkedserver can be used to create a linked server and fire distributed queries.

The following code adds the server test as Linked server to the current server

EXEC sp_addlinkedserver test

If you want to know if the connection to the linked server is accessible, you can make use of system stored procedure sp_testlinkedserver as shown below

EXEC sp_testlinkedserver test

It simply checks if the connection is successful. If the result is "Command(s) completed successfully.", it means the connection is successful. This is the simplest way to check if the linked server is accessible

linked-server

Also check Viewing Linked Server Information in SQL Server 2005/2008


 
  Feedback:

November 18, 2011

SQL Server: Create Objects Automatically in New Databases

0 comments


Suppose whenever a new database is created, you also want to create a stored procedure automatically (by default) in this new database. Let us see the simplest way to do this.

The Model database is a system database which SQL Server uses as a model to create new databases. So if you create any object in the model database, that object will be created automatically in all newly-created databases. Let us see this with the following examples

Create this test procedure in model database

use model
GO
create procedure test
as
select 100 as number


Now execute this procedure

EXEC test

It displays the value 100.
Now create another database named test

CREATE database test
GO


Now execute the same procedure in test database

use test
GO
EXEC test


sql-create-objects

As you can see, executing the above statement displays the value 100 since this value was taken from the stored procedure in the model database and is now available with test database too.

Feel free to share other approaches to create objects automatically in a database.


 
  Feedback:

November 13, 2011

Generate AlphaNumeric Identity Column in SQL Server

1 comments


Let us suppose you have a requirement where you want to have an alphanumeric identity column that has characters and numbers and you want these numbers to be incremental. Eg: Customer ids like cus0000001, cus0000002, etc. You can use any of the following methods

Method 1 : Store alphanumeric data in column

declare @t table(id char(10), names varchar(100))
insert into @t
select 'cus0000001','ramesh' union all
select 'cus0000002','suresh'


declare @id int
select @id=MAX(substring(id,4,len(id)))*1 from @t


insert into @t
select 'cus'+RIGHT(REPLICATE('0',7)+cast(@id+1 as varchar(7)),7),'nilesh'
select * from @t


In the above code, @id is assigned the maximum value of the number excluding characters "cus", which is used again in the INSERT statement

alphanumeric-sql


Method 2 : Use only identity column of int datatype and append alphabets in select statement

declare @t table(id int identity(1,1), names varchar(100))
insert into @t (names)
select 'ramesh' union all
select 'suresh' union all
select 'nilesh'


select 'cus'+right(replicate('0',7)+cast(id as varchar(7)),7) as id,names from @t

In the above code, an alphanumeric string is built dynamically in the select statement

alphanumeric-sql


 
  Feedback:

November 06, 2011

SQL Server: Find Common Rows in Tables using INTERSECT

0 comments


Suppose you have a SQL Server database with tables having similar structures and you want to find out similar rows among these tables. The most common method is to use a JOIN on all the columns.

Consider the following tables

create table #t1 (id int, names varchar(100))
create table #t2 (id int, names varchar(100))
create table #t3 (id int, names varchar(100))


insert into #t1
select 1,'test1' union all
select 2,'test2'

insert into #t2
select 1,'test1' union all
select 3,'test2'

insert into #t3
select 1,'test1' union all
select 20,'test2'


select t1.* from #t1 as t1
inner join #t2 as t2 on t1.id=t2.id
inner join #t3 as t3 on t1.id=t3.id


OUTPUT

The above select statement joins all these three tables by all columns to get similar rows among these tables. Then these similar rows in all three tables are displayed.

image

The problem with this approach is that if the table has many columns, you will need to specify all the columns in the join statement, which will make maintaining these queries a nightmare.

Another easy method to find common rows without actually specifying any columns is to use the INTERSECT Operator

sql-server-intersect

OUTPUT
Sql Server Intersect


 
  Feedback:

November 02, 2011

SQL Server: Handling Multiple Result sets in a Procedure

1 comments


Suppose you have a SQL Server stored procedure that returns multiple result sets and you want to store all these results into another table. I will show you how to do so. Consider the following stored procedure

create procedure test
as
select 1 as id, 'test1' as name
select 2 as id, 'test2' as name


When you execute this procedure, it returns two result sets. The following code will copy these two result sets in a table variable

declare @t table(id int, names varchar(100))
insert into @t
exec test

select * from @t

sql-proc-multiple






Note: In order to copy multiple result sets from a stored procedure, both result sets should have the same number of columns and datatype which are compatible with the target table. If not, an error message will be thrown for invalid number of columns/data type


 
  Feedback:
 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions