Saturday, December 15, 2012
Swap Value In A Table Column
Example: Suppose you have a Product table as below. And you have to swap the active field values i.e. True must be False and False must be True.
Tuesday, November 27, 2012
Important Sql Queries
Introduction: In this article i am going to give some of very useful SQL Queries that are required to us in our day to day programming life.
1. Get current Database Name: Select DB_NAME()
2. Get Details Of All Databases Present In SQL Server: Select * from sys.databases
3. Rename A Database: Exec sp_renamedb ‘oldDatabaseName’ , ‘newDatabaseName’
Dataset Vs DataReader
Dataset | DataReader |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tuesday, October 09, 2012
Optimization Of Stored Procedure In SQL Server
This article describes that how we can optimize the Stored Procedures so that we can get better performance while our application is talking to database. I am enlisting some of the points from my personal experience.
1) Always Use Fully Qualified Name For All Database Objects:
While working with stored procedure we need to pass name of database objects (table, view, function, other stored procedure(s) etc.) several times. I recommend every developer to use fully qualified object names instead of using only object name. There is a very strong reason behind this recommendation.
SQL server always has to find supplied database object name from sys object that took some millisecond of processing time and consumption of CPU resources. And if, we pass fully qualified name then that processing time and CPU resource consumption to search the object from sys objects get reduced to significant amount.
Another reason behind the scene is it helps SQL Server to directly finding the Complied Cache Plan if available instead of searching the objects in other possible schema. This process of searching and deciding a schema for a database object leads to COMPILE lock on stored procedure which decreases the performance of a stored procedure.
Thursday, August 30, 2012
How to improve performance of Database Operations in application
Hello Guys.
Today I am going to explain some tips that I have realized to improve the performance of web application or portal in context of database operation. We all know that without any database no dynamic website/application or portal can function. So it’s very important for us to effectively work with database. Following are some of the points which we need to consider while designing and developing our application so that its performance is always better. And we wouldn’t have to worry after the application development that how to optimize this application as its performance is very poor with respect to others.
Thursday, August 16, 2012
Dynamic SQL Statement in SQL Server
Dynamic SQL:
A dynamic sql statement is a set of sql statements which are constructed at execution time.
We may have to face certain condition in our application development where we might have to retrieve records from different table based on different conditions then in that scenario we do use dynamic SQL.
These dynamic Sql statement doesn’t parsed at compile time so it may introduce security vulnerabilities in our databse so we should try to avoid using dynamic sql as much as possible.
There are two ways to execute a dynamic sql statement in sql server:-
sp_executesql
EXECUTE()
Although these two methods produces same results but there might be certain scenario where it may produce different results.
Following is little description about the above two methods:
1. sp_executesql :-
It is a system stored procedure.
It allows parameters to be passed IN or OUT of the dynamic sql statement.
It is less susceptible to SQL Injection.
Higher chance for sql string to remain in cache which results better performance when the same sql statement is executed.
Clean code hence easier to read and maintain.
Support parameter substitution hence more preferable than EXECUTE command.
Syntax:-
sp_executesql [@sqlstmt ],[ @ParameterDefinitionList],[ @ParameterValueList ]
2. EXECUTE():-
When we use this command the parameters should be converted to characters.
Syntax:-
EXECUTE (@sqlStmt)
Example:-
Create procedure sp_GetSalesHistory
(
@WhereClouse nvarchar(2000)=NULL,
@TotalRowsReturned INT OPUTPUT
)
AS
BEGIN
DECLARE @SelectStmt nvarchar(),
DECLARE @FullStmt nvarchar(),
DECLARE @ParameterList nvarchar()
SET @ ParameterList = ’@TotalRowsReturned INT OUTPUT ’
SET @ SelectStmt = ‘SELECT @ TotalRowsReturned = COUNT(*) FROM SalesHistory’
SET @ FullStmt = @ SelectStmt + ISNULL(@WhereClouse,’ ’)
PRINT @ FullStmt
EXECUTE sp_executesql @ FullStmt ,@ ParameterList ,@ TotalRowsReturned =@ TotalRowsReturned OUTPUT
END
Wednesday, July 11, 2012
Thursday, June 21, 2012
Friday, June 15, 2012
Database Concept
What is RDBMS?
Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers. This allows a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage.
What is normalization?
Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design, the process of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.
Tuesday, June 12, 2012
Top 10 features of SQL Server 2008
Introduction
I this article i am going to describe top 10 features of SQL Server 2008 database.
Description
10. Plug-in model for SSMS. SSMS 2005 also had a plug-in model, but it was not published, so the few developers that braved that environment were flying blind. Apparently for 2008, the plug-in model will be published and a thousand add-ins will bloom.
9. Inline variable assignment. I often wondered why, as a language, SQL languishes behind the times. I mean, it has barely any modern syntactic sugar. Well, in this version, they are at least scratching the the tip of the iceberg.
Instead of:
DECLARE @myVar int
SET @myVar = 5
you can do it in one line:
DECLARE @myVar int = 5
Sweet.
Temporary Table Concept In SQL Server
Introduction
This article covers the concept of temporary tables and tables variables in SQL server database.
Temporary Tables
Lets take a look at a simple CREATE TABLE statement that is going to create a temporary table:
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )
You'll notice I prefixed the table with a pound sign (#). This tells SQL Server that this table is a local temporary table. This table is only visible to this session of SQL Server. When I close this session, the table will be automatically dropped. You can treat this table just like any other table with a few exceptions. The only real major one is that you can't have foreign key constraints on a temporary table.
Points to consider while designing database
SQL Server DO's and DON'Ts
DO know your tools.
Please, don't underestimate this tip. This is the best of all of those you'll see in this article. You'd be surprised of how many SQL Server programmers don't even know all T-SQL commands and all of those effective tools SQL Server has.
"What? I need to spend a month learning all those SQL commands I'll never use???" you might say. No, you don't need to. But spend a weekend at MSDN and browse through all T-SQL commands: the mission here is to learn a lot of what can and what can't be done. And, in the future, when designing a query, you'll remember "Hey, there's this command that does exactly what I need", and then you'll refer again to MSDN to see its exact syntax.
In this article I'll assume that you already know the T-SQL syntax or can find about it on MSDN.
SQL Tuning or SQL Optimization
SQL Tuning or SQL Optimization
Sql Statements are used to retrieve data from the database. We can get same results by writing different sql queries. But use of the best query is important when performance is considered. So you need to sql query tuning based on the requirement. Here is the list of queries which we use reqularly and how these sql queries can be optimized for better performance.
Following are some of the techniques that i remember now to optimize the SQL :--
1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;
Store and Retrieve Images in Sql Server ASP.NET
In this article i am going to write that how to store and retrieve images in our Sql Server Database.Let us start off by first creating a sample database and adding a table to it. We will call the database ‘Employee’ and the table will be called ‘EmpDetails’. This table will contain an image column along with some other columns. Run the following script in your SQL 2005 Query window (or server explorer) to construct the database and the table.