Thursday, 4 December 2014

SQL Server: Difference between != and <> Operator used for NOT EQUAL TO Operation

SQL – Difference between != and <> Operator used for NOT EQUAL TO Operation

Here is interesting question received on my Facebook page. (On a side note, today we have crossed over 50,000 fans on SQLAuthority Facebook Fan Page).
What is the difference between != and <>Operator in SQL Server as both of them works same for Not Equal To Operator? 
Very interesting question indeed. Even though this looks very simple when I asked quite a few people if they know the answer before I decided to blog about it. The answer which I received was that it seems that many know the answer but everybody wanted to know the more about it.
Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.
Here is the follow up question I received right I answer that there is no difference between those operator.
If != and <> both are the same, which one should be used in SQL queries?
Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.
Though, many of the leading database applications supports both of the operators. For example -
  • SQL Server
  • MySQL
  • Oracle
  • SQLite
  • Sybase
  • IBM Informix
  • PostgreSQL
Here is my return question to you which one of the following operators you use for NOT EQUAL TO operation?
  1. !=
  2. <>

Tuesday, 25 November 2014

Website: How to take a Web application offline

To take a Web application offline before deployment

  1. Create a file called App_offline.htm and include a message in the file that lets users know that the site is unavailable because you are updating the site.
  2. Place the App_offline.htm file in the root folder of the target Web site.
    While the App_offline.htm file exists in the root of your Web site, any request to the Web site will redirect to that file.
    When you have finished deploying the site, remove the App_offline.htm file.

Monday, 3 November 2014

SQL: stored procedures

SQL Server user-defined functions and stored procedures offer similar functionality. Both allow you to create bundles of SQL statements that are stored on the server for future use. This offers you a tremendous efficiency benefit, as you can save programming time by:
  • Reusing code from one program to another, cutting down on program development time
  • Hiding the SQL details, allowing database developers to worry about SQL and application developers to deal only in higher-level languages
  • Centralize maintenance, allowing you to make business logic changes in a single place that automatically affect all dependent applications

At first glance, functions and stored procedures seem identical. However, there are several subtle, yet important differences between the two:

  • Stored procedures are called independently, using the EXEC command, while functions are called from within another SQL statement.
  • Stored procedure allow you to enhance application security by granting users and applications permission to use stored procedures, rather than permission to access the underlying tables. Stored procedures provide the ability to restrict user actions at a much more granular level than standard SQL Server permissions. For example, if you have an inventory table that cashiers must update each time an item is sold (to decrement the inventory for that item by 1 unit), you can grant cashiers permission to use a decrement_item stored procedure, rather than allowing them to make arbitrary changes to the inventory table.
  • Functions must always return a value (either a scalar value or a table). Stored procedures may return a scalar value, a table value or nothing at all
    .

SQL:Stored Procedure Vs User Defined Function in Sql Server



Difference between Stored Procedure and User Defined Function in Sql Server


Below are the some of the major differences between User Defined Function and Stored Procedure in Sql Server.
To know more on the User-Defined functions with examples please visit the article: User-Defined function.To know more on the Stored Procedure with examples please visit the articleStored Procedure.
Sl. No.User Defined functionStored Procedure
1Function must return a value.Stored procedure may or not return values.
2Will allow only Select statement, it will not allow us to use DML statements.Can have select statements as well as DML statements such as insert, update, delete
etc
3It will allow only input parameters, doesn’t support output parameters.It can have both input and output parameters.
4It will not allow us to use try-catch blocks.For exception handling we can use try catch blocks.
5Transactions are not allowed within functions.Can use transactions within Stored procefures.
6We can use only table variables, it will not allow using temporary tables.Can use both table variables aswell as temporary table in it.
7Stored procedures can’t be called from function.Stored Procedures can call functions.
8Functions can be called from select statement.Procedures can’t be called from Select/Where/Having etc statements. Execute/Exec
statement can be used to call/execute stored procedure.
9UDF can be used in join clause as a result set.Procedures can’t be used in Join clause


Stored Procedures can contain a single SQL statement or a group of SQL statements with data flow control logic containing IF-ELSE, WHILE loop constructs, TRY-CATCH, transactions, etc.
SPs are used to return one or many result-sets to its calling application.
On the other hand Functions or UDFs can contain single or multiple SQL statements depending on its type. A Scalar UDF & Inline UDF can only have a single SELECT statement. And a Multi-Statement UDF can contain a body with multiple SQL statements including SELECTS, IF-ELSE, WHILE loops and DMLs but limited to manipulating table variables only.
UDFs return a single Scalar value or a Table variable to the calling SELECT statement.
Following are some common differences between an SP & a UDF:
Stored Procedures:
– Can be used to read and modify data.
– To run an SP Execute or Exec is used, cannot be used with SELECT statement.
– Cannot JOIN a SP in a SELECT statement.
– Can use Table Variables as well as Temporary Tables inside an SP.
– Can create and use Dynamic SQL.
– Can use transactions inside (BEGIN TRANSACTION, COMMIT, ROLLBACK) an SP.
– Can use used with XML FOR clause.
– Can use a UDF inside a SP in SELECT statement.
– Cannot be used to create constraints while creating a table.
– Can execute all kinds of functions, be it deterministic or non-deterministic.
Functions:
– Can only read data, cannot modify the database.
– Can only be used with SELECT statement, JOINS & APPLY (CROSS & OUTER).
– Can JOIN a UDF in a SELECT statement.
– Cannot use a Temporary Table, only Table Variables can be used.
– Cannot use a Dynamic SQL inside a UDF.
– Cannot use transactions inside a UDF.
– Cannot be used with XML FOR clause.
– Cannot execute an SP inside a UDF.
– Can be used to create Constraints while creating a table.
– Cannot execute some non-deterministic built-in functions, like GETDATE().


Thursday, 2 October 2014

SQL Server: Natural vs. Surrogate Keys in SQL Server : Getting the Proper Perspective


Natural vs. Surrogate Keys in SQL Server : Getting the Proper Perspective

I once walked into a bar, and saw two construction workers pounding each other to a pulp.  The argument was over what was the better tool—a  hammer or screwdriver.  I feel a similar sensation when I see SQL developers arguing over whether to use natural or surrogate keys.    Few other arguments in database design can cause tempers to flare so quickly.  Fans of surrogates consider anyone who uses a natural key a drooling idiot, whereas natural key acolytes believe the use of a surrogate warrants burning alive at the stake.    
Which side is right?  Neither.  Both natural and surrogate keys have their own place, and a developer who doesn’t use both as the situation demands is shortchanging both himself and his applications.

Definitions

A natural key is simply a column or set of columns in a table that uniquely identifies each row.   Natural keys are a feature of the data, not the database, and thus have business meaning on their own.  Quite often a natural key is more than a single column.  For instance, the natural key for a table of addresses might be the five columns: street number, street name, city, state, and zip code.
What is a surrogate key?  Most people will define it as some variation of “a system-generated value used to uniquely identify a row”.    Unlike natural keys, surrogates have no business meaning.   In SQL Server, by far the most common technique for generating surrogate values is the ubiquitous IDENTITY column.
While the above definition is true, there’s another very important part to it.    The value of a surrogate must never be exposed to the outside world.   Users should never be allowed to see the key, under any conditions.   Display the value of a surrogate key on a report, allow it to be viewed on a form or even used as a search term – these are all forbidden.   Once you expose a surrogate key, it immediately begins acquiring business meaning.  

Smart Keys: The Worst of Both Worlds

A smart key is an artificial key with one or more parts that contain business meaning.  For instance, an employee table where each primary key begins with the initial of the employee (“JD1001” for John Doe), or a table of paint products, where the key identifies the can size, color, and paint type, i.e.  “1G-RED- LATEX”.
Smart keys are a sort of hybrid between natural and surrogate keys.  They’re seductively attractive to many developers, but should be avoided like the plague.  They tend to make your design very brittle and subject to failure as business rules change.
Note: if your data already contains meaningful product codes or other keys such as those described above, then they are simply natural keys and the above caveat doesn’t apply.     It’s a smart key only when the value is constructed by the developer.

Benefits of Natural Keys

A natural key is…well, natural.  Since its values already exist in the data, using a natural key means you don’t have to add and maintain a new column.  This also means smaller tables and less storage requirements.  As more rows fit on a database page, it can sometimes mean greater performance. (It can also mean less- more on this later) .  However, in practical terms, the space savings are minor, except for very narrow tables.   Using a surrogate key usually means an additional index is required, though. 
Generating sequential key values is inherently a serial process, so using a natural key rather than an IDENTITY column can be a performance boost for inserts, especially in OLTP environments.
Since natural key values are used as foreign keys in child tables, it can mean the elimination of joins for queries that require no other columns from the parent other than the natural key.
One benefit of natural keys often claimed by its proponents is that they can aid in self-documenting your database schema.   Explicitly naming each natural key documents what specifically identifies each row in a table, and joining on natural keys helps to identify the natural relationships between tables.  This argument based onelegance appeals strongly to those working in academia; it may or may not have much value for developers working in the dirt and grime of real production systems.

Benefits of Surrogate Keys

Since surrogate values are controlled by the system, you never have to worry about duplicate, missing, or changing values.  They’re also an easy and reliable way to join tables; when writing queries, you never need worry about remembering which combination of columns is the natural key.  However, some of these benefits are less compelling than they seem.   I’ll discuss separately each case for using a surrogate, and whether or not it holds up.
When no natural key on the table exists – Yes.  
If the table has no unique identifier, then you must create one.  Unkeyed tables are in general a very bad idea.   There are exceptions such as logging or summary tables in which rows are only inserted, never updated.  Otherwise, if your table doesn’t have a unique key—create one.
When the natural key can change- Sometimes.  
Immutability is certainly a desirable feature for a key, but it’s by no means a requirement.   Using this as an excuse is the trademark of a lazy developer.  Data changes.   If the key changes, update it.  If it’s being used as a foreign key in another table – update that table too.  Cascading updates exist for a reason, after all.
Obviously if a key changes very often, or will be used as a FK for many other tables, there can be performance or concurrency implications to making it the primary key .  In this case, you do want to consider a surrogate, and use it if appropriate. 
There’s one particular case where the stability of a surrogate key actually works against you.  For lookup tables, particular those containing selection options for given fields, changes to the lookup value are often not meant to be cascaded into child tables.   For example, an application may store the “referral source” for new customers or marketing leads, whether they were generated via an ad in a newspaper or magazine, a yellow pages entry, word of mouth, etc.   These referral codes can be very specific, and are normally stored in a lookup table.  Once set, the code should be preserved historically, even if the original lookup table value is updated or removed.   This is behavior very difficult to achieve with a surrogate key, but trivial with a natural key.
When natural key values can be missing or duplicated - No
This is probably the most misunderstood aspect of the natural vs. surrogate debate.   Natural keys are unique by definition.   If it isn’t a serious error for a value to be missing or duplicated, then that value isn’t a natural key to begin with.  And if it is an error, then you’re almost always better off trapping that error at the database level,  rather than allowing that bad data into your DB. 
As example, consider a table of employees keyed off Social Security Number.   Users are complaining the database throws errors when they don’t have a SSN or mistakenly enter a duplicate.   So you replace the SSN PK with a surrogate and smugly conclude you’ve solved the problem.   But have you?   Now some employees don’t have SSNs, and the accounting module starts failing when printing tax records … or worse, collates all the NULL SSN entries together, reporting them as a single employee.     The search function starts returning the wrong rows because some employees are sharing the same SSNs, and the new hire in the mailroom gets the boss’s paycheck, because someone in HR accidentally cut and pasted a SSN.  
In reality, all you’ve done is short-circuit out the data integrity safeguards in your database, and pass the responsibility for the problem up to the application level. Bad move.
These sorts of problems exist because most tables have a uniqueness requirement at the tion level.    A surrogate key only solves the uniqueness problem  at the database level, but users (who cannot and should not see the surrogate value) still don’t have a way to uniquely identify each record.  This also explains why when, even if you choose to use a surrogate key, you will usually want to also add a unique constraint on the original natural key, since uniqueness is no longer being automatically enforced by the PK.
But wait a minute!  What if your business rules specifically require employees to be input before you have their SSN data?  Or what if your table holds overseas employees that may not have a SSN at all?  Does that mean you can’t use a natural key?  Maybe.  One possibility is to assign your own unique values in these cases.   One system I’ve seen used randomly generated alphabetic values for temporary SSNs, whereas the standard numeric value identified a “real” one.   Better yet is to examine your table for some other column or columns that can be used as a natural key.  Or maybe you really do want to drop natural keys altogether.  The point here is not that surrogates should never be used, but simply that if your natural key isn’t unique, you are going to have problems beyond those that a surrogate will solve.
When the natural key is very wide, or a composite of multiple columns – Sometimes 
Wide keys make for fat indexes.  Fat indexes have performance implications.  A very wide key  can hurt performance far more than the extra space required by a surrogate.   Replacing a composite key with a surrogate also simplifies your queries, but this should never be a primary consideration.  It’s poor form to replace the natural key of a two CHAR(2) columns with am INT IDENTITY, for no other reason than it makes your queries prettier.
One common case where a multi-column natural key should always be used is the so-called junction table: a table used to implement a many-many relationship between two other tables.  Most junction tables have only two columns, each a FK back to a parent table.  The combination of these two FKs is itself the primary key for the table.   Adding a surrogate to a table like this is just asking for trouble.
For performance reasons.  
This is the trickiest question of all.   Replacing a wide key with a narrower value means smaller indexes, and more values retrieved from every index page read.  This does boost performance.  However,  you’ll usually retain the index on the natural key (to enforce uniqueness if nothing else) and that means another index to maintain.  If your table is very narrow, the additional column for the surrogate can noticeably impact performance and storage requirements.  Finally, some queries that may have not required JOINs with a natural  foreign key may now need them.  For instance, our employee SSN example might have a child table containing reported hours worked:
Table: ReportedHours
Start_Time
DATETIME
Stop_Time
DATETIME
EmployeeID
(Foreign key to Employee table)
If the EmployeeID  FK is SSN, then we can retrieve a list of total hours by SSN from this table alone:
SELECT EmployeeID, SUM(DATEDIFF(hr,StopTime,StartTime))
FROM ReportedHours
GROUP BY EmployeeID
With EmployeeID as a surrogate key, however, we must JOIN back to the EMPLOYEE table:
SELECT SSN, SUM(DATEDIFF(hr,StopTime,StartTime))
FROM ReportedHours h
JOIN Employees e ON h.EmployeeID = e.EmployeeID
GROUP BY e.SSN
Continues…

Tuesday, 23 September 2014

SQL SERVER – Add Any User to SysAdmin Role – Add Users to System Roles

SQL SERVER – Add Any User to SysAdmin Role – Add Users to System Roles

The reason I like blogging is follow up questions. I have wrote following two articles earlier this week. I just received question based on both of them. Before I go on questions, I recommend to read both of the article first. Both of them are very small article so they are quick to read.
The question I had received is following: “How to add any user to systemadmin role?
This is very simple process.
Method 1: T-SQL
USE [master]
GO
CREATE LOGIN [test] WITH PASSWORD=N'test'DEFAULT_DATABASE=[master],CHECK_EXPIRATION=OFFCHECK_POLICY=OFFGOEXEC master..sp_addsrvrolemember @loginame N'test'@rolename =N'sysadmin'GO
Method 2:  SQL Server Management Studio
Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQL SERVER – ERROR: FIX – Database diagram support objects cannot be installed because this database does not have a valid owner


SQL SERVER – ERROR: FIX – Database diagram support objects cannot be installed because this database does not have a valid owner


Recently one of my friend sent me email that he is having some problem with his very small database. We talked for few minutes and we agreed that to further investigation I will need access to the whole database. As the database was very big he dropped it in common location (you can use livemesh or dropbox  or any other similar product) and I picked up from the location.
I was able to install the database successful. He informed me that he has created database diagram so I can easily understand his database tables. As soon as I tried to open the database diagram I faced following error. For a while I could not figure out how to resolve the error.
Error:
Database diagram support objects cannot be installed because this database does not have a valid owner. To continue, first use the Files page of the Database Properties dialog box or the ALTER AUTHORIZATION statement to set the database owner to a valid login, then add the database diagram support objects.
Workaround / Fix / Solution :
Well for a while I attempted few things and nothing worked. After that I carefully read the error and I realized that solution was proposed in the error only. I just have to read it carefully. Here are the steps I did to make this work.
-- Replace YourDatabaseName in following scriptALTER AUTHORIZATION ON DATABASE::YourDatabaseName TO sa
GO
  • Select your database >> Right Click >> Select Properties
  • Select FILE in left side of page
  • In the OWNER box, select button which has three dots (…) in it
  • Now select user ‘sa’ or NT AUTHORITY\SYSTEM and click OK.
This should solve your problem.
Please note, I suggest you check your security policies before changing authorization. I did this to quickly solve my problem on my development server. If you are on production server, you may open yourself to potential security compromise.
Reference: Pinal Dave (http://blog.sqlauthority.com)