Thursday, December 29, 2011

Script to calculate the Median value for SQL Server data

--Script to calculate the Median value for SQL Server dataDECLARE
SET
WITH
AS(SELECT ROW_NUMBER() OVER (ORDER BY PRODUCTID) AS ROW ,* FROM SALESLT.PRODUCT --its important to speciafy actuall column name in order by caluse as for the result)SELECT
WHERE ROW = @MIDDLE
* FROM RESULT
RESULT
@MIDDLE = (SELECT ROUND(COUNT(*)*0.5,0) FROM SALESLT.PRODUCT);
@MIDDLE INT

Creting User Define DataType

--How to create User Define DataType with Default and Rules

--Creating Default
CREATE
AS 'UnknownNumber' -- this is default value when no value is specifiedGO

--Creating RuleCreate
AS
(@phone='UnknownNumber') or (len(@phone) = 14 and substring(@phone,1,1) = '+' and substring(@phone,5,1) = '-')--Rule says when there is no value it place 'UnknownNumber'
--or lenght should be of 14 number and number should begin with '+' and '-' after 3rd digit to identify the state code

--Creting User Define DataType
EXEC SP_ADDTYPE --Sp to create datatypePhoneNumber, -- Name of new user defined data type that is being created'varchar(25)', -- Base system data type of SQL Server'not null', -- To specify that null values are allowed for this data type or not'dbo' -- Owner of this being created user defined data type
Rule Rule_Phn
DEFAULT Default_Phn

Wednesday, November 30, 2011

GO Statement can also be used to excute batch of T-SQL statement multiple times

Till recently, I was under the impression that GO statement’s sole purpose is to convey to the SQL Server the end of a batch of T-SQL Statements. But recently while searching for some SQL feature details, I landed on to the MSDN page for the GO statement. And to my surprise, I observed that GO statement also has an integer optional parameter, this parameter value signals SQL Server to execute the batch of T-SQL Statement prior to the GO statement to be executed for the specified number of times.

Let us understand this with the following simple example:
PRINT 'Hello'
GO 5
Result of executing the above statement is as follows:
Beginning execution loop
Hello
Hello
Hello
Hello
Hello
Batch execution completed 5 times
This statement is very handy for testing purposes and dev tasks, such as for testing purpose if we need to insert millions of records together, we can achieve this with simple two statements as explained in the above example.

Tuesday, October 11, 2011

End Of Month

We have all been in a situation where we had to get the last day of the month with T-SQL.To calculate if the last day is 31, 30, 28 or maybe even 29 is not that easy. There are many solutions to this problem out there, here is the one that i normally use:

DECLARE @myDate Datetime = '2012-02-12'SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @myDate) +1, 0))
This code finds the difference in months, adds one month (this gives us the first day of the next month) and then substracts one day.

With the introduction of SQL Server Denali, we get a new function called EOMONTH. The EOMONTH takes two parameters the first one being the date and the next optional one being the number of months to add to the start date.
EOMONTH (start_date [, month_to_add]

DECLARE @myDate Datetime = '2012-02-12'SELECT EOMONTH (@myDate) AS Result;
  
DECLARE @myDate Datetime = '2012-02-12'SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @myDate) +1, 0))

Wednesday, September 14, 2011

How to Compare the Data in Two Tables Without Any 3rd-Party Tool?

How to Compare the Data in Two Tables Without Any 3rd-Party Tool?
For comparing data in tables you don't need any 3rd party tool, SQL Server ships with the tablediff utility which can be used to compare the data in two tables. You can use it for troubleshooting replication. This utility can be used from the command prompt or in a batch file to perform the following tasks:
·         A row by row comparison between a source table in an instance of Microsoft SQL Server acting as a replication Publisher and the destination table at one or more instances of SQL Server acting as replication Subscribers.
·         Perform a fast comparison by only comparing row counts and schema.
·         Perform column-level comparisons.
·         Generate a Transact-SQL script to fix discrepancies at the destination server to bring the source and destination tables into convergence.
·         Log results to an output file or into a table in the destination database.
Lets see how to use it...
For testing i've created two tables Table1 and Table2 in DatabaseA and DatabaseB respectively on my local Instance, Then inserted few records in each table.
--On ServerA
create database DatabaseA
go
use DatabaseA
go
create table Table1(
tid int primary key,
tname varchar(20))
go
insert into Table1 values(1,'Sarab')
insert into Table1 values(13,'Amit B')
insert into Table1 values(18,'Amit K')
insert into Table1 values(21,'Sachin')

--ServerB(I've created this on my local instance however, You can create this on another server\Instance)
create database DatabaseB
go
use DatabaseB
go
create table Table2(
tid int primary key,
tname varchar(20))
go
insert into Table2 values(1,'Sarab')
insert into Table2 values(13,'Amit B')
insert into Table2 values(12,'Rishu')
insert into Table2 values(18,'MV Priyank')
The source table in the comparison must contain at least one primary key, identity, or ROWGUID column.
Now if you query both the tables you can see that I've purposely inserted 2 same records in both the tables (tid 1 & tid 13 marked Blue in the screenshot) and inserted same primary key with different values for tname (tid 18 marked Red in the screenshot) to show you how TableDiff Utility consider these scenarios.
Location of the Utility File:
The TableDiff.exe Utility can be found at this location: C:\Program Files\Microsoft SQL Server\100\COM
Note - In case you are using SQL Server 2005 replace 100 with 90.
To start the action launch command prompt and locate the directory having TableDiff.exe utility, then execute the command below:
TableDiff.exe -SourceServer . -SourceDatabase DatabaseA -SourceTable Table1 -DestinationServer . -DestinationDatabase DatabaseB -DestinationTable Table2 -et difft1 -f d:\my_Diff_File.sql
Parameters used:
Most of the parameters are self explanatory, the odd ones I am explaining below:
-et table_name
Specifies the name of the result table to create. If this table already exists, -DT must be used or the operation will fail.

-f [ file_name ]
Generates a Transact-SQL script to bring the table at the destination server into convergence with the table at the source server. You can optionally specify a name and path for the generated Transact-SQL script file. If file_name is not specified, the Transact-SQL script file is generated in the directory where the utility runs.


To get the complete list of all supported parameters visit: http://msdn.microsoft.com/en-us/library/ms162843.aspx

When -et parameter is used, the result table will be created in Destination Database. This table returns all rows where the data got mismatched based on primary (or Unique) Key including the script to resolve the conflict. It also returns all those rows which are only present at source or destination.

Here's a screenshot of the output:



And -f parameter will generate a file with T-SQL Script named my_Diff_File.sql which will have all the commands to resolve all the conflicts. The Output looks like this:

-- Host: .
-- Database: [databaseB]
-- Table: [dbo].[table2]
DELETE FROM [dbo].[table2] WHERE [tid] = 12
UPDATE [dbo].[table2] SET [tname]=N'Amit K' WHERE [tid] = 18
INSERT INTO [dbo].[table2] ([tid],[tname]) VALUES (21,N'Sachin')

Points to consider:
·         The tablediff utility cannot be used with non-SQL Server servers.
·         Tables with sql_variant data type columns are not supported.
Permissions Required:
·         To compare tables, you need SELECT ALL permissions on the table objects being compared.
·         To use the -et option, you must be a member of the db_owner fixed database role, or at least have CREATE TABLE permission in the subscription database and ALTER permission on the destination owner schema at the destination server.
·         To use the -dt option, you must be a member of the db_owner fixed database role, or at least have ALTER permission on the destination owner schema at the destination server.
·         To use the -o or -f options, you must have write permissions to the specified file directory location.
Security Note: When possible, supply security credentials at runtime. If you must store credentials in a script file, you should secure the file to prevent unauthorized access

Wednesday, August 24, 2011

Query for Database Level Permission

SELECT SUSER.UID AS USER_ID,
SUSER.NAME AS USERNAME,
SSUR.name AS SERVERROLENAME,
SCH.NAME AS IS_SCHEMA_OWNER
FROM SYS.SYSUSERS SUSER
Left Outer  JOIN sys.sysmembers smem on smem.memberuid=SUSER.uid
Left Outer JOIN sys.sysusers SSUR ON SSUR.uid=smem.groupuid
Left Outer Join  SYS.SCHEMAS SCH ON SCH.NAME=SUSER.NAME

Data Population Query

DECLARE @I INT --Declare a varialbe for holding starting value
SELECT @I = 1 -- Defining starting value

 WHILE @I < 1000 -- Checking condition for the limit
BEGIN

INSERT INTO TABLE1 -- Inserting table declaration
           (COLUMN1,
           COLUMN2,
           COLUMN3,
           GET_DATE
           .
           .......
           )
                SELECT
                100 + @I, NULL,NULL,GETDATE(),....... -- value passed for the table
               
SELECT @I = @I + 1 -- Incrementing the value for the varialble to check for while condition

END