Pages

Tuesday, March 31, 2009

SQL Server Articles -- March

DEFAULT value in SQL Server
http://www.dotnetvj.com/2009/03/default-value-in-sql-server.html

MERGE in SQL Server 2005
http://www.dotnetvj.com/2009/03/merge-in-sql-server-2005.html

Create Table Using Select in Sql Server
http://www.dotnetvj.com/2009/03/create-table-using-select-in-sql-server.html

OUTPUT in SQL Server 2005
http://www.dotnetvj.com/2009/03/output-in-sql-server-2005.html

Load text or csv file data into SQL Server
http://www.dotnetvj.com/2009/03/load-text-or-csv-file-data-into-sql.html

Change Data Type in SQL Server
http://www.dotnetvj.com/2009/03/change-data-type-in-sql-server.html

Drop table in SQL Server
http://www.dotnetvj.com/2009/03/drop-table-in-sql-server.html

Computed Columns in SQL Server
http://www.dotnetvj.com/2009/03/computed-columns-in-sql-server.html

Foreign Key and Primary Key in SQL Server
http://www.dotnetvj.com/2009/03/foreign-key-and-primary-key-in-sql.html

List all the Foreign Keys in a SQL Server
http://www.dotnetvj.com/2009/03/find-out-foreign-keys-in-sql-server.html

List all the Primary Keys in SQL Server Database
http://www.dotnetvj.com/2009/03/list-all-primary-keys-in-sql-server.html

Add Foreign Key in SQL Server 2005
http://www.dotnetvj.com/2009/03/adding-foreign-key-to-table-in-sql.html

Add Primary Key in SQL Server
http://www.dotnetvj.com/2009/03/add-primary-key-in-sql-server.html

Add NOT NULL Constraint in SQL Server 2005
http://www.dotnetvj.com/2009/03/add-not-null-constraint-in-sql-server.html

DEFAULT value in SQL Server

In this section, I would like to tell you about adding a column to the existing table with a default value.

Default value is very useful in the sense if you don’t supply any value to that column SQL Server will automatically insert the default value into that column.

Let us create a DEPT table.

CREATE TABLE [dbo].[DEPT](
[DEPTNO] [int] NOT NULL,
[DNAME] [varchar](30) NULL
)

Run the below insert statements

INSERT INTO DEPT VALUES(1,'Human Resource');
INSERT INTO DEPT VALUES(2,'Project Management');
INSERT INTO DEPT VALUES(3,'Business Analysis');
INSERT INTO DEPT VALUES(4,'IT Analyst');
INSERT INTO DEPT VALUES(5,'Software Developer');

Now let's say I want to add CATEGORY column to this table and store the default value as “IT”.


ALTER TABLE DEPT
ADD CATEGORY VARCHAR(3) NOT NULL DEFAULT ('IT')

When you run the above statement it will not only add a new column with default value but it will update the existing values to “IT”.

Now if you look at the table definition, you can see the new column with DEFAULT clause is set to “IT”.

CREATE TABLE [dbo].[DEPT](
[DEPTNO] [int] NOT NULL,
[DNAME] [varchar](30) NULL,
[CATEGORY] [varchar](3) NOT NULL DEFAULT ('IT')
)


This is perfect when you are adding a new column. What if you want to modify the definition of a column to remove the default value??

Wait for my next post or tell me if you have the answer……

Monday, March 30, 2009

MERGE in SQL Server 2005

In my previous section I explained about OUTPUT feature in SQL Server. In this section I will go one step ahead and will tell you, how you can use this feature to implement MERGE in SQL Server 2005.

MERGE is a powerful feature in SQL Server 2008. If you want to know what it is check out the link http://www.sqlservercentral.com/articles/Advanced+Querying/3122/

Now coming back to the SQL Server 2005, with couple of SQL Statements you can easily implement this feature.

Before you continue further you need to check out my first post on the OUTPUT feature in SQL Server.

Quickly create the table:

CREATE TABLE EMPLOYEE_WORKING_HRS(
EMP_ID int NULL,
ENAME varchar(30) NULL,
YR varchar(4) NULL,
MON varchar(3) NULL,
WORKING_HRS int NULL,
RATE int NULL,
SALARY AS (WORKING_HRS*RATE)
)


Let’s insert few records into this table.
INSERT INTO EMPLOYEE_WORKING_HRS VALUES(100,'SAM',2009,'JAN',176,100);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (101,'LIMO',2009,'JAN',176,110);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES(102,'NAT',2009,'JAN',176,120);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (103,'BECK',2009,'JAN',176,130);

Let’s create another table with the same structure but with out any data.

SELECT * INTO EMPLOYEE_WORKING_HRS_STG FROM EMPLOYEE_WORKING_HRS WHERE 1=2

Now insert the below data into this staging table.

INSERT INTO EMPLOYEE_WORKING_HRS_STG
([EMP_ID],[ENAME],[YR],[MON],[WORKING_HRS],[RATE],[SALARY])
VALUES
(105,'NICK',2009,'JAN',176,100,17600);
INSERT INTO EMPLOYEE_WORKING_HRS_STG
([EMP_ID],[ENAME],[YR],[MON],[WORKING_HRS],[RATE],[SALARY])
VALUES
(101,'LIMO',2009,'JAN',176,130,15840);


There are two records in this table. Now the task update EMPLOYEE_WORKING_HRS table based on the EMPLOYEE_WORKING_HRS_STG. If a record is not updated then INSERT that record into EMPLOYEE_WORKING_HRS table.

In this case 105 is new record so we have to insert this record into EMPLOYEE_WORKING_HRS table and update the 101 record with new Hourly Rate.

Now execute the below query

declare @Emp_ids table(ID int)
update EMPLOYEE_WORKING_HRS set RATE = s.RATE
output inserted.EMP_ID into @Emp_ids
from EMPLOYEE_WORKING_HRS m, EMPLOYEE_WORKING_HRS_STG s
WHERE s.EMP_ID = m.EMP_ID

INSERT INTO EMPLOYEE_WORKING_HRS
([EMP_ID],[ENAME],[YR],[MON],[WORKING_HRS],[RATE])
SELECT [EMP_ID],[ENAME],[YR],[MON],[WORKING_HRS],[RATE]
FROM EMPLOYEE_WORKING_HRS_STG WHERE EMP_ID NOT IN ( SELECT ID FROM @EMP_IDS)


This will update the EMPLOYEE_WORKING_HRS table where ever it finds the match in EMPLOYEE_WORKING_HRS_STG table and puts that EMP_ID that is the record which is affected with UPDATE statement and use this to copy data from EMPLOYEE_WORKING_HRS_STG table to EMPLOYEE_WORKING_HRS where EMP_ID is not in this updated IDs list.

That’s it. Its very simple isn’t it.

Some More articles related to this are:
OUTPUT in SQL Server 2005
Create Table Using Select in Sql Server

Create Table Using Select in Sql Server

In this section i would like to give you a small tip about creating a table in SQL Server with out using CREATE TABLE statement.


SELECT * INTO EMPLOYEE_WORKING_HRS_STG
FROM EMPLOYEE_WORKING_HRS

If you look at the query I am trying to create a replica of the EMPLOYEE_WORKING_HRS table with a new name. Or you can even say I am creating a new table with the same structure as EMPLOYEE_WORKING_HRS and then inserting data into it.

Now let's say you don't want to copy the data, just you want to create another table then execute the below query.


SELECT * INTO EMPLOYEE_WORKING_HRS_STG
FROM EMPLOYEE_WORKING_HRS
where 1=2



The only difference between this and the previous one is WHERE condition.

OUTPUT in SQL Server 2005

In this section, I would like to talk about one interesting feature of SQL Server 2005. Before I say anything about this feature let me ask you a simple question.

Can u write a single query to display all the records that are affected with an UPDATE statement?

It is impossible to find out this information with the UPDATE statement.

In most of the cases what we do is after the UPDATE we will go and query the table to see whether UPDATE ran successfully or not. I mean whether it updated the records which we are expecting.

This is two step process. What if there is a simple SQL statement that tells you what records are affected with the UPDATE statement? You will save lot of time with this.

Instead of waiting for some more time, the feature which I am talking about is OUTPUT keyword.

Let’s get straight into the example to understand what you need to do to get the updated records.

Quickly create the table:
CREATE TABLE EMPLOYEE_WORKING_HRS(
EMP_ID int NULL,
ENAME varchar(30) NULL,
YR varchar(4) NULL,
MON varchar(3) NULL,
WORKING_HRS int NULL,
RATE int NULL,
SALARY AS (WORKING_HRS*RATE)
)

Let’s insert few records into this table.
INSERT INTO EMPLOYEE_WORKING_HRS VALUES(100,'SAM',2009,'JAN',176,100);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (101,'LIMO',2009,'JAN',176,110);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES(102,'NAT',2009,'JAN',176,120);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (103,'BECK',2009,'JAN',176,130);

Due to the financial crisis I want to Add the Hourly to -20 if it is greater than 100.

declare @Emp_ids table(ID int)
update EMPLOYEE_WORKING_HRS set RATE = RATE - 20
output inserted.EMP_ID into @Emp_ids
from EMPLOYEE_WORKING_HRS
WHERE RATE > 100
SELECT * FROM @Emp_ids

In this piece of code I declared a variable called @emp_ids which is of table type.
Then I have used UPDATE clause with INSERTED emp_id of table into @Emp_Ids table.

Finally selecting the data from @Emp_Ids table.

Isn’t it kool!!!!!

Next section I will talk about how to mimic the MERGE functionality in SQL SERVER 2005.

Load text or csv file data into SQL Server

Today I will talk about loading a CSV or comma or txt file into SQL Server using BULK INSERT.

Using BULK INSERT, in few steps you can load the file into database.

Let’s go with an example

Let’s say I have following data in a file.

CustomerID,Customer_Name, Customer_Location
1,SAM,NY
2,BEN,NJ
3,HARY,HYD

Now the task is to load this file into SQL Server table. To do this, create one table in SQL Server with the similar structure.

CREATE TABLE Cusomer
(
CustomerID INT,
Customer_Name VARCHAR(50),
Customer_location VARCHAR(10)
)


Structure is exactly the same as the file. Use the below query to load the data.

BULK INSERT Cusomer
FROM 'c:\vijay_sql\Customer.txt'
WITH
(
FIRSTROW = 2,
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Important Parameters:
1) FROM ==> this is to specify the File Location from which we want to load the data.
2) FIRSTROW ==> This is to specify which row we want to load the data. This field is not about skipping the header.
3) MAXERRORS ==> This is to specify the maximum number of syntax errors allowed in the data before the bulk-import operation is canceled. Each row that cannot be imported by the bulk-import operation is ignored and counted as one error. If this field is not specified at all, then default is 10. In this case this value is set to 0 which means if there is a single error then, don’t load the data.
4) FILEDTERMINATOR ==> this is to specify how fields are separated. In this case field separator is “,”.
5) ROWTERMINATOR ==> this is to specify how to differentiate the different rows. In our case it’s the new line character.

There are various options available with the BULK INSERT like Fire the Triggers or Keep the Identity etc. I will show you some flavors/options of BULK INSERT in my next postings.

Sunday, March 29, 2009

Change Data Type in SQL Server

Today, I will talk about how to change the data type of a column which has data.

Let’s create one simple table

CREATE TABLE EMPLOYEE_WORKING_HRS(EMP_ID INT, ENAME VARCHAR(30), YR INT, MON VARCHAR(3), WORKING_HRS INT, RATE INT);

Let’s insert few records into this table.

INSERT INTO EMPLOYEE_WORKING_HRS VALUES (100,'SAM',2009,'JAN',176,100);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (101,'LIMO',2009,'JAN',176,110);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (102,'NAT',2009,'JAN',176,120);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (103,'BECK',2009,'JAN',176,130);

Now I want to change the data type of YR column from INT to VARCHAR.

SP_HELP 'EMPLOYEE_WORKING_HRS'



Normally for people who are coming from different database backgrounds, it’s really a painful task. That is you need to add a column with this new data type and then update this column and rename the columns. This is really a lengthy process. With the SQL Server you don’t have to do all these. Just one simple ALTER STATEMENT can change this.

ALTER TABLE EMPLOYEE_WORKING_HRS ALTER COLUMN YR VARCHAR(4)

Simple isn’t it.

SP_HELP 'EMPLOYEE_WORKING_HRS'


Drop table in SQL Server

One of common query that we execute on day-to-day to basis is Dropping and re-creating a table when you are as beginner.

You can use DROP TABLE to drop table from database. But the table which you are trying to drop has child references then you can’t drop this table.

Let’s try to drop DEPT table.

By looking at the message you can’t really say what are all the other tables that have relationship with this table. So now the BIG task is to find out the tables which have relationship with this table.

The below SQL query will help you to find out the child tables associated with this DEPT table.

SELECT

TC.CONSTRAINT_NAME AS FOREIGN_KEY_CONSTRAINT_NAME,
TC.TABLE_NAME AS FOREIGN_KEY_TABLE_NAME,
KCU.COLUMN_NAME,
ORDINAL_POSITION AS COLUMN_POSITION,
RC.UNIQUE_CONSTRAINT_NAME AS PARENT_PRIMARY_KEY_NAME,
TC_PK.TABLE_NAME AS PARENT_TABLE_NAME

FROM

INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU,
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC_PK

WHERE TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
AND TC.TABLE_NAME = KCU.TABLE_NAME
AND TC.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND RC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND RC.UNIQUE_CONSTRAINT_NAME = TC_PK.CONSTRAINT_NAME
AND TC_PK.TABLE_NAME = 'DEPT'


Computed Columns in SQL Server

Today I will talk about one of the very interesting feature called "Computed Columns" in SQL Server. This is a column in table but you will not assign any specific data type to this as you do it in regular CREATE TABLE statement.

You can ask this question, if there is no data type associated with this column then what kind of data i can store? And how to insert the data into this column?

Before I answer these questions, let's look at the basic syntax of table creation.

Let's take an example; I want to create a table to store Employee Working hours and Monthly Rate.

CREATE TABLE EMPLOYEE_WORKING_HRS(EMP_ID INT, ENAME VARCHAR(30), YR INT, MON VARCHAR(3), WORKING_HRS INT, RATE INT);



Let’s insert few records into this table.

INSERT INTO EMPLOYEE_WORKING_HRS VALUES
(100,'SAM',2009,'JAN',176,100);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (101,'LIMO',2009,'JAN',176,110);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES
(102,'NAT',2009,'JAN',176,120);INSERT INTO EMPLOYEE_WORKING_HRS VALUES (103,'BECK',2009,'JAN',176,130);





Now if you want to find out what is the salary of each of these employees then you need to multiply WORKING_HRS with RATE.

select EWR.*, EWR.WORKING_HRS*EWR.RATE AS SALARY from EMPLOYEE_WORKING_HRS EWR



When ever we want to find out the salary then we have to multiply RATE column with WORKING_HRS. It’s an extra overhead on the developer to have this conversion. Instead you can have this column in table itself but its violation to the normal forms.

So now you are in mixed state, you don’t want to store this information in the table and you don’t want to have this Multiplication in the query each time. The solution for this is “Computed Columns”.

CREATE TABLE EMPLOYEE_WORKING_HRS
(EMP_ID INT,
ENAME VARCHAR(30),
YR INT,
MON VARCHAR(3),
WORKING_HRS INT,
RATE INT,
SALARY as WORKING_HRS*RATE);


Created a column SALARY and the definition of the column is WORKING_HRS*RATE.

Let’s insert same old records into this table.

INSERT INTO EMPLOYEE_WORKING_HRS VALUES (100,'SAM',2009,'JAN',176,100);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (101,'LIMO',2009,'JAN',176,110);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (102,'NAT',2009,'JAN',176,120);
INSERT INTO EMPLOYEE_WORKING_HRS VALUES (103,'BECK',2009,'JAN',176,130);


Now Lets query the table.



The fundamental principle behind this is when the data is returned; the computation is applied to return the result.

However, you can force a computed column to physically store data by using PERSISTED keyword. With this keyword, this will be just like any other column and gets modified whenever there is an UPDATE/INSERT on the underlying columns.

Saturday, March 28, 2009

Foreign Key and Primary Key in SQL Server

Check out the below script to find out the Foreign Key information like Name of the Foreign Key , Name of the Table which has foreign Key and List of columns in that Foreign Key definition. In addition to this the foreign key which is referencing the Primary Key and Primary key table or Parent table.

SELECT

TC.CONSTRAINT_NAME AS FOREIGN_KEY_CONSTRAINT_NAME,
TC.TABLE_NAME AS FOREIGN_KEY_TABLE_NAME,
KCU.COLUMN_NAME,
ORDINAL_POSITION AS COLUMN_POSITION,
RC.UNIQUE_CONSTRAINT_NAME AS PARENT_PRIMARY_KEY_NAME,
TC_PK.TABLE_NAME AS PARENT_TABLE_NAME

FROM

INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU,
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC_PK

WHERE TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
AND TC.TABLE_NAME = KCU.TABLE_NAME
AND TC.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND RC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND RC.UNIQUE_CONSTRAINT_NAME = TC_PK.CONSTRAINT_NAME

List all the Foreign Keys in a SQL Server

The below script will give you list of Foreign Keys in a database and columns which are part of the Foreign key definition.

If you don't know how to add a Foreign key on a table then you need to check out the "Adding Foreign Key on a Table" first.

SELECT

TC.CONSTRAINT_NAME,
TC.TABLE_NAME ,
KCU.COLUMN_NAME,
ORDINAL_POSITION AS COLUMN_POSITION

FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC, INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
WHERE 1=1
AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
AND TC.TABLE_NAME = KCU.TABLE_NAME
AND TC.CONSTRAINT_TYPE = 'FOREIGN KEY'

There could be several ways to get the same kind of information but from my point of view this is most simple way to do it.

Please let me know if you have a query which is much simple than this.....

List all the Primary Keys in SQL Server Database

The below script will give you list of Primary Keys in a database and columns which are part of the primary key definition.

If you don't know how to add a primary key on a table then you need to check out the "Adding Primary Key on a Table" first.


SELECT

TC.CONSTRAINT_NAME,
TC.TABLE_NAME ,
KCU.COLUMN_NAME,
ORDINAL_POSITION AS COLUMN_POSITION

FROM

INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU

WHERE 1=1
AND TC.TABLE_NAME = 'DEPT'
AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
AND TC.TABLE_NAME = KCU.TABLE_NAME
AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'


There could be several ways to get the same kind of information but from my point of view this is most simple way to do it.

If you do not want to limit your result set to a table then remove the TABLE_NAME condition.

Please let me know if you have a query which is much simple than this.....


Add Foreign Key in SQL Server 2005

In this section we will learn on defining a Foreign Key Constraint. In simple words lets try to define a relationship between two tables.

Lets create a Employee table with below structure.


CREATE TABLE Employee(
EMP_ID int IDENTITY(1,1) NOT NULL,
ENAME varchar(50) NOT NULL,
DOB datetime NOT NULL,
SALARY decimal(18, 0) NOT NULL,
DEPTNO int NOT NULL
)

Now lets define Primary Key on EMP_ID column, though this is not mandatory but it is a good practise. Check out the link on Adding a Primary Key on the table.

Lets look at the syntax:


ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_01 FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)

It is very simple isn't it. Similar to Primary Key syntax with a little change.

Add Primary Key in SQL Server

One of the cirtical factor in database/table designing is to prevent duplicate records. There are several ways to do this. You can place Unique Index or Unique Constraint or add a primary key. Well there is only slight difference between these things.

I will go one by one to describe about these but for now lets concentrate on adding a primary key to a table.

There are two ways to add a primary key constraint:

1) While Creating the Table using CREATE TABLE command.
2) Adding Primary key after table creation using ALTER TABLE statement.

The simple thing is to create the table first and define the Primary key later. Again each one of us will have different opinion but to me this is simple step. Though you will end up in having multiple SQL statements but you will have more control on what you are doing.

The pre-requisite to define a Primary Key on a column is, that column should be NOT Nullable.
Check out Adding NOT NULL constraint article to meet the pre-requisite of Primary key.

To verify whether the column is NOT NULL or not, run the below query:

SELECT TABLE_NAME,COLUMN_NAME,IS_NULLABLE FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = 'DEPT'


If IS_NULLABLE is "NO" means column has Not Null Constraint.


Lets say I want to add Primary Key constraint on DEPTNO Column.

ALTER TABLE:

ALTER TABLE DEPT ADD CONSTRAINT PK_DEPT PRIMARY KEY (DEPTNO);

We are trying to add a Primary Key Constraint on DEPT table and the column is DEPTNO.
The Name of the Constraint is PK_DEPT. Its very important to give a name to the constraint.
As the general standards Primary key constraint name should always start with PK_ and followed by the table Name Or you can have _PK.

CREATE TABLE:

CREATE TABLE DEPT(DEPTNO INT PRIMARY KEY, DNAME VARCHAR(30))

As you can see i didn't specify constraint name to Primary key. So Tell me is there any way to put a constraint name in the CREATE TABLE statement??

kick it on DotNetKicks.com

Add NOT NULL Constraint in SQL Server 2005

Today I will explain about the different ways to add a NOT NULL constraint on a column.

Advantages of NOT NULL constraint:
1) You can prevent users to enter null values into this column.
2) The pre-requisite to enforce Primary Key on a Column.

Now Lets get into the details:
There are two ways which i can think of
1) While creating the table using CREATE TABLE statement.
2) Modifiying the table definition using ALTER TABLE statement.

CREATE TABLE:

CREATE TABLE DEPT(DEPTNO INT NOT NULL, DNAME VARCHAR(30))

When you are creating a table as part the Column Definition you can specify the Constraint. This is very easy. But most of the cases we get into a position where we have a table in PROD or some where and want to define a Not Null constraint on the column. So the next section is about defining the constraint on existing table.

ALTER TABLE:

Created a table called Dept to store Department information.

CREATE TABLE DEPT(DEPTNO INT, DNAME VARCHAR(30));

ALTER TABLE DEPT ALTER COLUMN DEPTNO INT NOT NULL;

That's it. Now if you use any of following queries you can see the column is now not nullable.

SELECT * FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = 'DEPT'

or

sp_columns 'dept'

One question here, When you are adding NOT NULL constraint with ALTER TABLE you need to specify the column Data Type. Do you know why??? If you put different datatype then it will not accept!!!!

If you know the answer please post your comment.

kick it on DotNetKicks.com

Thursday, March 26, 2009

XML Schema Definition Tool

XML Schema Definition Tool (Xsd.exe)

In many projects where Xml files are used, there can be a need to generate Xml schema
From an xml file and classes from schema file. We can use XML Schema Definition Tool (Xsd.exe) to generate Xml schema from XML file.

xsd Test.xml /outputdir:testOutputDir

where option "/outputdir:directory" specifies the directory for output files.

For Eg: xsd C:\Test.xml /outputdir:C:\Test which generates xsd schema file in C:\Test

To generate classes or dataset from an XSD schema file.

xsd file.xsd {/classes /dataset} [/element:element]
[/language:language] [/namespace:namespace]
[/outputdir:directory] [URI:uri]

Where
1) option "/classes" generates classes from an XSD schema file.
2) option "/dataset" generates dataset corresponding to XSD schema file.
3) option "/element" specifies the element in the schema for which code is generated.
4) option "/language" is used for specifying the language(C#, Visual Basic, Jscript, Visual J#)
5)option "/namespace" specifies the runtime namespace for the generated types. The default namespace is Schemas.
6) option "/outputdir:directory" specifies the directory for output files.
7) option "/URI:uri" specifies the URI for the elements in the schema to generate code for.

For Eg: xsd C:\Test\Test.xsd /classes /language:vb /outputdir:C:\Test which generates classes from an XSD schema file.



kick it on DotNetKicks.com

Saturday, March 21, 2009

Identity in SQL Server - Alter Identity Column in SQL Server

Identity Column plays very major role in SQL Server.

Please check out some of my articles on Identity.

From my Own Blog:
-----------------
identity key columns in sql server
Identity in SQL Server
get your basics on identity columns

Full length and detailed articles from Jacob Sebastian:(SQL Server MVP)
------------------------------------------------------------
All you need to know about Identity in SQL Server

Saturday, March 7, 2009

My MVP Summit 2009 Experience: DAY 3

Just like DAY 2, started with Non-Veg Break fast and headed to Microsoft to get more insight on technology.

Some more snaps:







Standing from Left to Right: Nikhil Kothari, Vijaya Kadiyala, Suprotim, Ravi (the one in Blue Jacket).

Once again i met the 3M(s) of C-SharpCorner.



With this last picture my Tour ends.



I am really looking FWD to be the part of MVP Summit again.

My MVP Summit 2009 Experience: DAY 2

DAY 2, Brand new day with lots of energy and lots of enthusiasm to visit Microsoft Campus and to attend various training sessions. Wow…I can’t believe it.
Had good non-vegetarian break fast. It may seem bit odd to you but I love Non-vegetarian food.

Boarded the bus to go to Microsoft Campus.

Here are some pictures, which I took when I was in bus: In Microsoft Campus





After getting down the bus I saw almost all country flags.



Some more pictures:









Unfortunately i can't say anything about what sessions i attended and what's in there!!!!

My MVP Summit 2009 Experience: DAY 1 Part 3

On the same day i met some more people from various technology domains.

I am fortunate to meet Limno from WWW.ASP.NET. He is one of the top and best contributor in this forum and is now part of Hall-Of-Fame. He told me a few tips in ASP.Net and in SQL Server. He also shared his experiences of his journey of becoming MVP. He is really nice. I am really looking forward to meet him once again in the next MVP summit.



Standing from Left to Right: Pinal, Limno and Vijaya Kadiyala

We had long party at the Convention center and its time to go back to hotel and take some rest and get up for the next BIG thing i.e. Microsoft Campus Redmond. Wow.. isn't it great to say I am going to visit Microsoft Campus!!!

On the way back to hotel, I heard that there is open forum discussion from the people who are behind MSDN Forums. isn't it really kool!!! On-and-Off I was also contributing in this forum.
I met Sam, he is one of the Dev Lead of MSDN Forums and he told me the improvements his team made to these forums. Just now I visited these forums and UI looks great. As expected Microsoft kills everybody with looks:)



Standing from Left to Right: Sam, Vijaya Kadiyala

After long hour discussions with Sam, its time to take rest. So i slowly moved to my hotel room. I was about to sleep, got a call from Mahesh Chand from C-Sharpcorner.com
He called me for a dinner with MVPs who are from C-Sharpcorner.com. I had very good time with all of them.



Starting form Left to Right Front Row: Vijaya Kadiyala, Mahesh Chand
Starting form Left to Right Back Row: Mike Gold, Mathew Cochran

My MVP Summit 2009 Experience: DAY 1 Part 2

After all the initial key notes from MVP leaders, we headed to the dinner. I happen to see many authors of various books which I read to learn these technologies. Wow it’s truly amazing. As you know I regularly contribute to the SQLServerCentral.com and I happen to meet Steve Jones whom I always admired. What can I say it’s truly world class experience; I even told him about my contributions in SQLServerCentral.com and my position in it. When I said my rank in this community is less than 150 he was very happy and impressed by me.
You know what, when he kept his hand on my shoulders I can’t express how happy I was at that point of time. What can I say about him, I am like a little kid with candy in front of him.


Standing from Left to Right: Vijaya Kadiyala and Steve Jones.

Some more snaps with some of the Finest Technical people in SQL Server.


Standing from Left to Right: Vijaya Kadiyala, Andy Warren

Once again hand on my shoulders from one of the best SQL MVP.

Standing from Left to Right: Itzik Ben-Gan, Vijaya Kadiyala


Standing from Left to Right: Vijaya Kadiyala, Satya, Glenn Berry, Ross Mistry

My MVP Summit 2009 Experience: DAY 1 Part 1

My MVP Summit 2009 Experience: DAY 1

Again all 5 MVPs gathered in Pinal's Room and went to register for MVP summit at Washington Sate Convention Center.
Check out the below pics.







After our registration we were looking for a place to have our breakfast. Where else we can go!!!!!! With Egg-and-Chese Biscuit from Mc Donalds and then visited a few places in Downtown and then went to visit Seattle's world famous Space Needle.

Here are some of the snaps:







Its Lunch time and we were hungry. We found one good place "Bombay Wala" in Downtown. While coming back from Space Needle, I met an MVP. His name is Guilherme Mendonça and he is from Brazil. He is MVP in "Expression" technical discipline. There are very few MVPs around the world may be you can count them on your fingers with this technical skill set.



After our Lunch, we went back to hotel Sheraton to attend some training sessions. Please dont ask me what sessions I attended coz I can't disclose it.

Now the big event of the day: MVP's Welcome reception. Its amazing. For the very first time in my life I felt like I am one of the best.

Here are some of the snaps:








All South Asia MVPs with MVP Lead Abishek Kant(the one who is in Brown Jacket standing First from Right to Left) and Howard Lo (Regional Manager Asia Pacific Greater China).

My MVP Summit 2009 Experience: DAY 0

My MVP Summit 2009 Experience: DAY 0

On DAY 0, I met few MVPs from India who were staying in the same hotel where I was staying. In the evening,we were roaming in Downtown. During the course of time we became good friends.Guess what, out of these 5 MVPs, 4 of them are new i.e. for the very first time to the Summit.One of them shared his last year MVP Summit experience. We tried to find a good place for our dinner; it was too late because all the shops and restaurants were closed by that time. Don’t ask me the time … its just 8:45 PST. After going here and there, we finally ended up in Mc Donalds. When we came out of this, we went to visit Washington state convention and trade center. Some of the pics at this place:



Standing from Left to Right: Vijay, Mahesh, Pinal, Suprotim and Ravi. Let me tell you something about MVPs.

Suprotim Agarwal I think this time I spelled his name correctly otherwise I always used to mispronounce his name. I will share my experience on his name when I am writing about Mahesh Patil. He is a MVP in ASP/ASP.Net Architecture. In addition to this he has so many certifications in his pocket. He regularly contributes at ASP.NET forums and he regularly blogs at http://www.dotnetcurry.com/ and http://www.sqlservercurry.com/.
There are very few MVPs in India who got this award in ASP/ASP.Net Architecture discipline. You want to know the number then it is less than 5 and all around the world its just 54. I am fortunate to be the part of this Technical discipline i.e. I am also into ASP/ASP.NET Architecture.
He really inspired me in various ways. The best line what he told me is “ When you are doing something your focus should be on Quality not on the Quantity” this is very true. Apart from this he also told me what to work on and how to improve your knowledge.
I forgot to tell you something. He is working as a Independent consultant in INDIA. There are very few people in INDIA who are Independent consultants. One has to see his knowledge in various technologies. You can take any topic in Microsoft, he can speak about that and he also suggests process improvement steps. It’s really amazing.

Ravi Patel
is working in Infosys as Project Manager in UK. He is a MVP in ASP/ASP.NET. After talking to him I came to know that he is not just a project manager but also technical consultant in his projects. In today’s world, people either go to Management side or Technical side. But Ravi is good in both the areas. I am sure people who are working for him are lucky enough to get both management and technical tips.

Pinal dave,
As far I can think of him,he is a well known TABLE in SQL Server database. One must see his blog, he is so consistent in writing articles in his blog that he even published few lines when the day he was getting married. It’s really amazing. Can you believe it. Well I am big fan of his blog. He gives/shares very detailed information about basics that we deal in day-to-day life. For me its dream come true when I met him. He is MVP in SQL Server. I also came to know that other fellow MVPs in SQL Server all around the world nominated his name under TOP 5most influential people in SQL Server Community. I think this is the Biggest and Best compliment any one can get. Check out his BLOG on his MVP summit experience.
The best line what he told to me is “ Share your day-to-day experience in blog” this is very true. I am sure most of us are dealing with same set of problems in our day-to-day work.

Mahesh Patil is working in system side programming. He is MVP in Visual C++. Its very rare in INDIA to find people working on VC++ but who ever is working in this are one of the finest technical people and Mahesh is in one of them. He is into core system software design and development in Microsoft technologies using VC++/ATL/COM. He is one of the top contributors in experts-exchange. Check out his profile. Apart from his technical skills he is very friendly. He tells lot of jokes; one can’t stand with out laughing. He is full entertainment TV (Technical Veteran). He is the one who renamed Suprotim Agarwal to Support Team. Mahesh is very funny.