Archive for the ‘Notes’ Category

All Articles

How to connect the SQL Server running on the different TCP/IP port?

Posted by Jugal Shah on March 14, 2012

In case you have configured SQL Instance to use the static TCP/IP port number. You can connect SQL Server as below using SSMS.

Posted in OS and SQL, SQL Server 2008, SQL Server 2008 R2, SQL Server 2011 (Denali) | Tagged: , , , , , | 1 Comment »

How to check waits in SQL Server 2000?

Posted by Jugal Shah on March 10, 2012

Today I got a comment, how to check the wait statistics in SQL Server 2000. You can query sysprocesses table and use the DBCC SQLPERF to get the wait statistics in SQL Server 2000.

select top 5* from sysprocesses
dbcc sqlperf(‘waitstats’)

Wait Statistics Image

Posted in SQL Server | Tagged: , , , , , , | Leave a Comment »

How to use RunAs command for SSMS if option does not exist?

Posted by Jugal Shah on March 7, 2012

Problem

As a best practice in the industry, a DBA often has two logins that are used to access SQL Server; one is their normal Windows login and the other is an admin level login account which has sysAdmin rights on the SQL Server boxes. In addition most of the time the SQL Server client tools are only installed on the local desktop and not on the SQL Server Production Box. In order to use the different login to connect to SQL Server using SSMS you need to use the “Run as” feature. What do you do in the case of Windows 7 or Windows Vista where you can’t find the Run As Different User option.

Solution

http://www.mssqltips.com/sqlservertip/2617/how-to-use-runas-command-for-ssms-if-option-does-not-exist/

Posted in SQL Server | Leave a Comment »

Looking for Job Change?

Posted by Jugal Shah on March 5, 2012

Posted in SQL Server | Leave a Comment »

Script to create a Database with different collation

Posted by Jugal Shah on March 4, 2012

One of my blog reader has asked how to create the database with different collation. You can create the database with different collation using below script.

create database SQLDB collate Latin1_General_CS_AS;
go
create database SQLDB1 collate Latin1_General_CI_AS;
go

SELECT DATABASEPROPERTYEX('SQLDB', 'Collation') SQLDB;
SELECT DATABASEPROPERTYEX('SQLDB1', 'Collation') SQLDB1;

Posted in Database, SQL Server, SQL Server 2008, SQL Server 2008 R2, SQL Server 2011 (Denali) | Tagged: , , , , | Leave a Comment »

Online Discussion

Posted by Jugal Shah on March 2, 2012

Posted in SQL Server | 2 Comments »

SQL Server Agent Job Owner

Posted by Jugal Shah on February 28, 2012

It is a best practice to keep the SQL Agent Job owner to SA or Service Account. Please don’t make the job owner to any user’s windows ID. If the employee left the company his ID will be terminated and access will be revoked.
You can find out the jobs owner by executing the below query.

SELECT 
j.name,
l.[name] as [JobOwner]
FROM MSDB.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
ORDER BY j.[name]

You can change the job owner by executing the below query.

EXEC MSDB.dbo.sp_update_job 
@job_name = 'Job Name', 
@owner_login_name = 'sa'

Posted in SQL Server, SQL Server 2008, SQL Server 2008 R2 | Tagged: , , , , , , | Leave a Comment »

How to Check whether SQL Instance Clustered or Standalone

Posted by Jugal Shah on February 25, 2012

You can check the SQL Server is cluster or standalone using one of the below technique.

Technique 1: Go to server properties from SSMS and check the Is Clustered property.

 

 

Technique 2: Execute the below query, it will retun Boolean value. (1 = True and 0=False)

SELECT
CONVERT(char(20),
SERVERPROPERTY(‘IsClustered’))

Posted in SQL Server | Leave a Comment »

Steps to Save/Store file into Database

Posted by Jugal Shah on February 16, 2012

You can use the below scripts to store/save the file into SQL Server database table. Please note it is not recommended to store file into database. You can store the file on file system and path in the database.

use sqlDBPool
--documents table will store files into varbinary field
--drop table documents
create table documents
(
	documentID int identity(1,1),
	doctype char(5),
	document varbinary(max)
)


--script to store/save document into table
insert into documents 
 Select 'xls', (SELECT * FROM OPENROWSET(BULK N'C:\JSpace\book1.xls', SINGLE_BLOB) AS document) document
go

select * from documents

Posted in SQL Scripts, SQL Server, SQL Server 2008, SQL Server 2008 R2, SQL Server 2011 (Denali) | Tagged: , , , , , , | 1 Comment »

@@MICROSOFTVERSION Function

Posted by Jugal Shah on February 12, 2012

Problem: Take an example you are writing a script which is going to be deployed on the all the versions of SQL Servers and you want to check the SQL Server version details using T-SQL code. Below solution will guide you how to check the SQL Server version in stored procedure/t-sql batch.

Solution: You can use the @@MICROSOFTVERSION to get the SQL Server version information. If the output of the below script is 9 than its SQL 2005, if 10 than SQL Server 2008 and if 11 than SQL Server 2011

select @@VERSION

--method - 1
select @@MICROSOFTVERSION as MSVersion, CAST (@@MICROSOFTVERSION as BINARY(5)) as MsVersionInBinary
-- Remove the first non-zero character after 0x0 from binary output here it is A and divide the @@MicrosoftVersion outout 
select substring(cast(@@MICROSOFTVERSION/0x000000640 as varchar(10)),1,2) as MsSQLVersion

--Method 2
select @@MICROSOFTVERSION / POWER(2,24) as usingPowerFunctionMSSQLVersion 

Posted in Database, SQL Scripts, SQL Server, SQL Server 2008, SQL Server 2008 R2, SQL Server 2011 (Denali) | Tagged: , , , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 174 other followers