Venkatesan Prabu MCITP,MCAD,MCTS,CCNA. Worked as a ProjectLead(Senior .Net developer,SQL DBA). Now, Managing Director of KAASHIV INFO TECH, Chennai This Blog aims in serving the community in a better way. This blog is read by developers in 159 countries with average of 400 hits per day. Please post your valuable suggestions and hold my hand to serve the community. Lets make a new world with good thoughts and good minds....... This blog serves the SQL server community all over the world.
31.7.08
Drop all tables in a Database
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
The above command will traverse each table and drop it.
sp_MSforeachtable is an undocumented stored procedure.
We can delete single table too.
EXEC sp_MSforeachtable @command1 = "drop table aa"
This sp will take a sql query as parameter and execute it.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
Database Log file size in SQL Server
Regards,
Venkatesan Prabu .J
Substring in a string in SQL Server
Charindex is a nice function used to check the existence of a substring in a string. Below is the sample code to check the existence of string.
declare @str varchar(10)
declare @substr varchar(5)
set @str = 'Venkat'
set @substr='nak'
if charindex(@substr,@str) >0
Print 'String available'
else
Print 'String not available'
Regards,
Venkatesan Prabu .J
Change row name in SQL Server
CREATE TABLE VENKAT1(ID INT,NAM VARCHAR(10))
INSERT INTO VENKAT1 VALUES(1,'VENKAT')
INSERT INTO VENKAT1 VALUES(2,'ARUN')
SELECT * FROM VENKAT1
SELECT
CASE WHEN NAM='VENKAT' THEN 'SANTHI'
ELSE NAM
END
FROM VENKAT1 Happy Learning!!!
Regards,
Venkatesan Prabu .J
Compare two tables in SQL Server
Simple and Spectacular article and wish to write the same in my blog.
Below code will be used to compare two tables in SQL Server
CREATE TABLE VENKAT1(ID INT,NAM VARCHAR(10))
CREATE TABLE VENKAT2(ID INT,NAM VARCHAR(10))
INSERT INTO VENKAT1 VALUES(1,'VENKAT')
INSERT INTO VENKAT1 VALUES(2,'ARUN')
INSERT INTO VENKAT2 VALUES(1,'VENKAT1')
INSERT INTO VENKAT2 VALUES(2,'ARUN')
SELECT * FROM VENKAT1
SELECT * FROM VENKAT2
SELECT
CASE WHEN COUNT(*) = 0 THEN 'Same' ELSE 'Different' END
FROM (
(
SELECT * FROM VENKAT1
EXCEPT
SELECT * FROM VENKAT2
)
UNION (
SELECT * FROM VENKAT2
EXCEPT
SELECT * FROM VENKAT1
)
) dv
Happy Learning!!!
Regards,
Venkatesan Prabu .J
30.7.08
Collation in SQL Server
Collation on the server can be forced in two ways either,
1. While installation or
2. During our operation or business logic execution.
You can check the collation details by right clicking your server properties.
If we want to assign a specific collation for your database. In that case, you can achieve it using the below query,
ALTER DATABASE VenkatDB COLLATE Latin1_General_CI_AS
To know the collation types in your server. You can run the below query,
SELECT * FROM fn_helpcollations()
Microsoft have provided a nice function which will fetch all the collations available in your server.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
27.7.08
Disable trigger to fire other triggers
1. Right click server properties->Advanced->Allow triggers to fire others property to false.
Else we can achieve it using T-SQL statement too. Lets see the second method to achieve the same.
Execute the below queries to achieve the same,
Code Snippet |
EXEC sp_configure 'nested triggers', 0 |
Code Snippet |
RECONFIGURE |
Happy Learning!!!
Regards,
Venkatesan Prabu. J
T-SQL Challenges in SQL Server
Solution:
Code Snippet |
create table #a (nam varchar(10)) insert into #a values('aa') insert into #a values('ba') insert into #a values('ca') with cte as ( select 1 as id ) update #a set nam = (select * from cte) select * from #a |
Happy Learning!!!
Regards,
Venkatesan Prabu. J
Column filteration based on Data type
Code Snippet |
select * from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE not in ('varchar') and table_name='venkat' |
Regards,
Venkatesan Prabu. J
26.7.08
Columns into rows in sql server
Code Snippet |
create table aaaa(id int,nam varchar(10),city varchar(10)) insert into aaaa values(1,'aa','sydney') insert into aaaa values(2,'bb','delhi') insert into aaaa values(3,'cc','chennai') select * from aaaa create table #temp1(id int, nam varchar(10)) insert into #temp1 select id,nam from aaaa create table #temp2(id int, nam varchar(10)) insert into #temp2 select id,city from aaaa |
Below is the ouput we are getting for the table aaaa
We are trying to convert it into rows, so the output should resemble similar to the below screenshot.
Code Snippet |
select id,nam from #temp1 union all select * from #temp2 order by #temp1.id |
Happy Learning!!!.
Regards,
Venkatesan Prabu. J
Rows to Columns in SQL Server
Code Snippet |
create table VenkatTable (id int, question varchar(20),answer varchar(20)) insert into VenkatTable values(1,'Name','Manoj') insert into VenkatTable values(1,'Qualification','BE') insert into VenkatTable values(1,'Nationality','Indian') insert into VenkatTable values(2,'Name','Venkat') insert into VenkatTable values(2,'Qualification','BE') insert into VenkatTable values(3,'Qualification','BE') select * from VenkatTable |
The above queries will create a table as below,
I need a result set as below screen shot. It can be achieved using pivot operator in SQL Server 2005. I have tried with some other logic to achieve the same. Lets see how to achieve the below output.
Code Snippet |
SELECT id, [Name]= (SELECT answer FROM VenkatTable WHERE question = 'Name' and id=m.id), [Qualification]= (SELECT answer FROM VenkatTable WHERE question = 'Qualification' and id=m.id) , [Nationality]= (SELECT answer FROM VenkatTable WHERE question = 'Nationality' and id=m.id) FROM VenkatTable m GROUP BY id |
Happy Learning!!!
Regards,
Venkatesan Prabu .J
DDL Triggers in SQL Server 2005
Code Snippet |
CREATE TRIGGER DDLTriggertoSaveTable ON DATABASE FOR DROP_TABLE |
AS PRINT 'M No way to delete the tables in the Database?' |
Now, execute the below statement,
Code Snippet |
Drop Table VenkatTestDLLTriggers |
You will get a message indicating "Cannot drop the tables in the database.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
Installation problem in SQL Server
I am having SQL Server installed in my machine and i need to uninstall the same for some of the following reasons,
1. To move to an upgraded versions of sql server
2. Problem in SQL Server
In that case, we will find some problem in upgrading or repairing the existing server. In that case, Microsoft have given a nice article to resolve our problems. Please check the below KB URL,
http://support.microsoft.com/kb/909967
Happy Learning!!!Regards,
Venkatesan prabu. J
To fetch all the constraints in SQL Server
Its a cool feature available in SQL Server 2005. Inorder to avoid direct access on the system tables. Microsoft have intoduced a layered structure to access the system related information. They have used views to achieve the same.
In the below query, I am trying to fetch all the Primary key and foreign key constriants available in the database.
Code Snippet |
select CONSTRAINT_NAME,TABLE_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS |
Happy Learning!!!
Regards,
Venkatesan prabu .J
Retrieve Owner of the database
1. Right click database properties and -> General -> owner you can view the database owner of this particular datbase.
2. Else we can write a T-SQL Statement to acheive the same.
Code Snippet |
select suser_sname(owner_sid) from sys.databases |
Happy Learning!!!
Regards,
Venkatesan Prabu .J
20.7.08
SSIS error - Part 8
Description: "Unspecified error".
[OLE DB Destination [70]] Error: There was an error with input column "id" (410) on input "OLE DB Destination Input" (83). The column status returned was: "The value
violated the integrity constraints for the column.".
[OLE DB Destination [70]] Error: The "input "OLE DB Destination Input" (83)" failed because error code 0xC020907D occurred, and the error row disposition on "input "OLE DB
Destination Input" (83)" specifies failure on error. An error occurred on the specified object of the specified component.
[DTS.Pipeline] Error: The ProcessInput method on component "OLE DB Destination" (70) failed with error code 0xC0209029. The identified component returned an error from the
ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.
Solution : The problem is due to insertion of already existing data into the primary key column.
Please delete those existing data or insert some valid data to resolve this problem
Regards,
Venkatesan Prabu .J
SSIS error - Part 7
0x80004005 Description: "The statement has been terminated.". An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Cannot
insert the value NULL into column 'create_date', table 'Master.dbo.VenkatTable'; column does not allow nulls. INSERT fails.".
Problem: The problem is due to insertion of invalid data into the column.
Solution: Since, the column wont allow null value, either you have to change the type of the column by using alter statement else provide some valid integer/character during data insertion.
Happy learning!!!
Regards,
Venkatesan prabu .J
SSIS Error - Part 6
"Multiple identity columns specified for table 'Tmp_VenkatTable'. Only one identity column per table is allowed.". Possible failure reasons: Problems with the query,
"ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
The error is due to the identity column. SQL wont allow more than one identity column in the table.
Solution:
Please remove the excessive identity column from the table.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
SSIS Errors - Part 5
[DTS.Pipeline] Warning: The output column "A" (6643) on output "Union All Output 1" (2177) and component "Joining the Table" (2175) is not subsequently used in the Data Flow
task. Removing this unused output column can increase Data Flow task performance.
Solution: I have used a "Union All" task which will fetch more data and am not using all the data in the destination inturn, am using some selected columns in the destination. SSIS indicating us to remove the unused output column from Union All task. Inturn, this will increase the performance of our SSIS package.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
19.7.08
SSIS Errors - Part 4
[OLE DB Source [1]] Warning: Cannot retrieve the column code page info from the OLE DB provider. If the component supports the "DefaultCodePage" property, the code page from that property will be used. Change the value of the property if the current string code page values are incorrect. If the component does not support the property, the code page from the component's locale ID will be used.
Its because of default code page file which should be available in "C:\Program Files\Microsoft SQL Server\90\DTS\MappingFiles" for that particular server.
If the code page file is not available then, your SSIS package will show the warning "Unable to load the file.
Below screen shot shows the default code page available in your machine.
To resolve this problem, Right click your OLEDB Source or Detination(Sybase) and enable the property "AlwaysuseDefaultcodepage" to true.
Happy Learning!!!
Regards,
Venkatesan prabu .J
SSIS errors - Part 3
[OLE DB Destination [2357]] Error: An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "The statement has been terminated.". An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object
Reason and Solution:
The problem is due to invalid insertion of data which affect the integrity property of the data. Suppose, If the user tries to insert the data which already exists in the column(primary key column). Then we will get the error.
To resolve this, either we need to remove the existing or new data or we need to modify the data before inserting into the primary key column.
Happy Learning!!!
Regards,
Venkatesan prabu .J
Unknown things in Identity_insert statement
Unknown things about Identity Insert:
Considering, am having a requirement to insert the data in the identity column as below,
Code Snippet |
create table VenkatTable (id int identity,[name] varchar(100)) |
insert into VenkatTable values (10,'Santhi') |
You will get an error,
"An explicit value for the identity column in table 'VenkatTable' can only be specified when a column list is used and IDENTITY_INSERT is ON."
Solution:
The problem is with the identity column. It won't allow us to insert the data explicitly inturn we need to switch off the identity property to implement our requirement.
Code Snippet |
set identity_insert VenkatTable on |
insert into VenkatTable values (10,'Santhi') |
On Executing the above statement, am getting the same error.. OOOOPS!!!
"An explicit value for the identity column in table 'VenkatTable' can only be specified when a column list is used and IDENTITY_INSERT is ON."
Let's analyze the problem once again. I've tried with the below statement.
Code Snippet |
insert into VenkatTable(id,[name]) values(10,'Santhi') |
Yahoooo, its inserting the data now. SQL request us to specifically pointout the column names in the insert statement.
After our requirement, we need to Switch off the identity_insert property by using the below statement.
set identity_insert VenkatTable off
Happy Learning!!!
Regards,
Venkatesan prabu .J
SSIS Errors - Part 2
Messages
* Error 0xc0202009: Data Flow Task: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Invalid character value for cast specification".
The problem is with the matching columns between source and destination
9.7.08
SSIS Error
"The query has been cancelled because the estimated cost of this query (Integer) exceeds the configured threshold of (Integer). Contact the system administrator.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly."
Reason:
It's because the current query execution exceeds the threshold server execution time.
Solution:
You can opt different solutions for this problem. Let me explain two types of solutions among them,
1. Setting the Query governor value to higher limit.
2. Right click the database properties - >connections -> use query governor to prevent long running queries.
3. Increase the value if already exists.
4. Else, you can write a T-SQL query to achieve the same,
SET QUERY_GOVERNOR_COST_LIMIT 1000(I have given 1000 as sample)
2. If you dont have administrative right to achieve the same you can opt less number of queries or less number of accessing tables.
Regards,
Venkatesan prabu .J
4.7.08
Microsoft MVP
On July 1, I have got a nice news from microsoft regarding my MVP award. I have been selected as Microsoft MVP for the year 2008-2009 on SQL Server.
It's really shocking and i am wondering on seeing myself as a great MVP.
Happy learning!!!
Sybase to SQL Server using SQL Server Integration services(SSIS)
"Error at Data Flow Task [OLE DB Source [211]]: An OLE DB error has occurred. Error code: 0x80040E37.An OLE DB record is available. Source: "ASE OLE DB Provider" Hresult: 0x80040E37 Description: "[Native Error code: 102][DataDirect ADO Sybase Provider] Incorrect syntax near '.'.".
Error at Data Flow Task [OLE DB Source [211]]: Opening a rowset for ""dbo"."TableA"" failed. Check that the object exists in the database. "
My T-SQL Gallery @code.msdn.microsoft
Created my own T-SQL Gallery in Microsoft site. Do visit the same and share your feedback,
http://code.msdn.microsoft.com/VenkatSQLSample/Thread/List.aspx
Thanks and Regards,
Venkatesan Prabu .J
SQL Server Interview questions - Part 1
Null means no entry has been made. It implies that the value is either unknown or undefined.We should avoid permitting null values because Column with NULL values can't have PRIMARY KEY constraints. Certain calculations can be inaccurate if NULL columns are involved.
What is SQL whats its uses and its component ?
The Structured Query Language (SQL) is foundation for all relational database systems. Most of the large-scale databases use the SQL to define all user and administrator interactions. It enable us to retrieve the data from based on our exact requirement. We will be given a flexibility to store the data in our own format.
The DML component of SQL comprises four basic statements:
* SELECT to get rows from tables
* UPDATE to update the rows of tables
* DELETE to remove rows from tables
* INSERT to add new rows to tables
What is DTS in SQL Server ?
Data Transformation Services is used to transfer the data from one source to our required destination. Considering am having some data in sql server and I need to transfer the data to Excel destination. Its highly possible with dialogue based tool called Data Transformation services. More customization can be achieved using SSIS. A specialized tool used to do such migration works.
What is the difference between SQL and Pl/Sql ?
Straight forward. SQL is a single statement to finish up our work.Considering, I need some data from a particular table. “Select * from table” will fetch the necessary information. Where as I need to do some row by row processing. In that case, we need to go for Procedural Logic / SQL.
What is the significance of NULL value and why should we avoid permitting null values?
Null means no entry has been made. It implies that the value is either unknown or undefined.We should avoid permitting null values because Column with NULL values can't have PRIMARY KEY constraints. Certain calculations can be inaccurate if NULL columns are involved.
Difference between primary key and Unique key?
Both constraints will share a common property called uniqueness. The data in the column should be unique. The basic difference is,
· Primary key won’t allow null value. Whereas, unique key will accept null value but only one null value.
· On creating primary key, it will automatically format the data inturn creates clustered index on the table. Whereas, this characteristics is not associated with unique key.
· Only one primary key can be created for the table. Any number of Unique key can be created for the table.
Select Statement in SQL Server
String Functions in sql server
SQL Server Interview Question - Part 2
What is normalization?
Normalization is the basic concept used in designing a database. Its nothing but, an advise given to the database to have minimal repetition of data, highly structured, highly secured, easy to retrieve. In high level definition, the Process of organizing data into tables is referred to as normalization.
What is a stored procedure:
Stored procedures are precompiled T-SQL statements combined to perform a single task of several tasks. Its basically like a Macro so when you invoke the Stored procedure, you actually run a set of statements. As, its precompiled statement, execution of Stored procedure is compatatively high when compared to an ordinary T-SQL statement.
What is the difference between UNION ALL Statement and UNION ?
The main difference between UNION ALL statement and UNION is UNION All statement is much faster than UNION,the reason behind this is that because UNION ALL statement does not look for duplicate rows, but on the other hand UNION statement does look for duplicate rows, whether or not they exist.
Example for Stored Procedure?
They are three kinds of stored procedures,1.System stored procedure – Start with sp_2. User defined stored procedure – SP created by the user.3. Extended stored procedure – SP used to invoke a process in the external systems.Example for system stored proceduresp_helpdb - Database and its propertiessp_who2 – Gives details about the current user connected to your system. sp_renamedb – Enable you to rename your database
What is a trigger?
Triggers are precompiled statements similar to Stored Procedure. It will automatically invoke for a particular operation. Triggers are basically used to implement business rules.
What is a view?
If we have several tables in a db and we want to view only specific columns from specific tables we can go for views. It would also suffice the needs of security some times allowing specfic users to see only specific columns based on the permission that we can configure on the view. Views also reduce the effort that is required for writing queries to access specific columns every time.
What is an Index?
When queries are run against a db, an index on that db basically helps in the way the data is sorted to process the query for faster and data retrievals are much faster when we have an index.
What are the types of indexes available with SQL Server?
There are basically two types of indexes that we use with the SQL ServerClustered -
1. It will format the entire table, inturn physically sort the table.
2. Only one clustered index can be created for a table.
3. Data will be located in the leaf level.
4. By default, primary key will create clustered index on the table.
Non-Clustered Index
1. It wont touch the structure of the table.
2. It forms an index table as reference to the exact data.
3. A reference to the data will be located in the leaf level.
4. For a table, we can create 249 non clustered index.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
SQL Interview question
Extent Vs Page?
Pages are low level unit to store the exact data in sql server. Basically, the data will be stored in the mdf, ldf, ndf files. Inturn, pages are logical units available in sql server.The size of the page is 8KB.
Eight consecutive pages will form an extent 8 * 8KB = 64KB.
Thus I/O level operation will be happening at pages level.The pages will hold a template information at the start of each page (header of the page).
They are,
1. page number,
2. page type,
3. the amount of free space on the page,
4. the allocation unit ID of the object that owns the page.
Extents will be classifed into two types,
1. Uniform extents
2. Mixed extents
Uniform Extents:It occupied or used by a single object. Inturn, a single object will hold the entire 8 pages.Mixed
Extents:Mulitple objects will use the same extent. SQL Server will allow a max of eight objects to use a shared extent.
Property of SQL Server :Initally if an object is created, sql server will allocate the object to the mixed extent and once if the size reaches 8 pages and more... immediately, a new uniform extent will be provided for that particular object.
Herecomes, our fragmentation and reindexing concepts.