RSS

Category Archives: T-SQL

Retrieving id of inserted record: What to use @@IDENTITY or SCOPE_IDENTITY() or OUTPUT

Hi guys,
This seems very basic information but need to know properly.

Issue:
I need to find out or have the id of the records which I inserted in table.

Analysis:
This requirement seems very simple but there could be various cases where you would not get the correct id just by using @@IDENTITY function.

Think of the case where you are inserting a record in table A and there is a trigger which fires as soon as there is a insert in table A which insert a record in table B. so in such case @@IDENTITY wont give you the correct id/pointer of inserted record.

So to deal with this issue you can use SCOPE_IDENTITY(). You will always get the last value you have inserted in table. However, it is noticed that sometimes in some scenarios SCOPE_IDENTITY() does not promise the correctness of results. May be due to parallelism or multiple inserts or bulk inserts in table(s). E.g. when you do … Insert Into Select <columns> from Table

Well, Microsoft recommends that “You do not use either of these functions in your queries when parallel plans are involved as they are not always reliable. Instead use the OUTPUT clause of INSERT statement to retrieve the identity value“.

Check out MS articles:
http://support.microsoft.com/default.aspx?scid=kb;en-US;2019779
http://msdn.microsoft.com/en-us/library/ms177564.aspx

I hope this will help you somewhere.
Thanks, Khilit

 

Tags: , , , , , ,

The Story of Precision, Scale, and Length in SQL Server

Hi guys,

Did you ever faced issues with BIG mathematical figures – related to either overflow or any  other arithmetic errors. This may be applicable to SQL arithmetic operations or arithmetic  operations you do with SQL table in SSIS etc.

Well, check 1 more time, if you have any issues in your database design related to column  data type which stores the BIG numbers.

I ran into this issue couple of times, and reason behind this issue was same all the time. So  decided to do a quick post. And that reason is “Improper or insufficient precision, scale and length defined for storing Decimals and Numeric values in table“.

I wanted to describe this with an example but there is already a very nice explanation by MSDN  on this topic. Check out: http://msdn.microsoft.com/en-us/library/ms190476.aspx

You will notice in this article that, how any arithmetic operation will affect size and space  allocation of the resultant value during mathematical operation. You should use following shown table for defining and designing tables in such a way that can  handle BIG mathematical figures during operations.

From MSDN, for your quick view:

Operation

Result precision

Result scale *

e1 + e2 max(s1, s2) + max(p1-s1, p2-s2) + 1 max(s1, s2)
e1 – e2 max(s1, s2) + max(p1-s1, p2-s2) + 1 max(s1, s2)
e1 * e2 p1 + p2 + 1 s1 + s2
e1 / e2 p1 – s1 + s2 + max(6, s1 + p2 + 1) max(6, s1 + p2 + 1)
e1 { UNION | EXCEPT | INTERSECT } e2 max(s1, s2) + max(p1-s1, p2-s2) max(s1, s2)
e1 % e2 min(p1-s1, p2 -s2) + max( s1,s2 ) max(s1, s2)

* The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.

Important Note: Don’t forget to check for your SQL Server Version.

I hope you will find this informative.
Thanks, Khilit

 

Tags: , , , , ,

Tracking Down Ghost Characters: WHERE Clause Does Not Work Properly In T-SQL Query

Hi guys,
I would like to share an experience with you when I was working on 1 SSIS related issue.

Scenario was:
I have to exclude some records from Table A by matching some keys of Table B.

 Table A

Year Month CustomerCode <Extra Columns>
2012 JAN CX1000  
2012 JAN CX2000  
2012 JAN CD3000  

 

 Table B

Year Month RecCustomerCode <Extra Columns>
2012 JAN CX1000  
2012 JAN CX7000  
2012 JAN CD8000  

 

 What is expected was – Table A’s following records:

Year Month CustomerCode <Extra Columns>
2012 JAN CX2000  
2012 JAN CD3000  

  Read the rest of this entry »

 

Tags: , , , , , , , , ,

View is not showing newly added column in table

Hi all,
This is for the people who still use “Select * From Table” in their SQL Views. I don’t know what is the reason that you are using this way to create your views, but you could be the victim of this scenario (see… what title says).

Let me show you how you can solve this issue… in-case you come across one.

When this happens??
You make changes in table which is the source of your view. Changes like modifying existing column definition, adding new columns or removing existing columns from table could cause this issue.

Where can I see this issue??
Check the meta-data of your table and view for comparison. (Tip: You can either use traditional method using SYS.OBJECTS joins SYS.COLUMNS or you can check using INFORMATION_SCHEMA.COLUMNS)

What is the solution?
There are different ways to circumvent this issue.

Read the rest of this entry »

 

Tags: , , , , ,

What happens to IDENTITY Column Value When SQL Transaction Fails??

Hi All,

I would like to share a simple interview question. This is very simple yet many developers overlooks or not taking care of it.

Do you know what happens to identity value if transaction fails? Do you think that identity value of table is unchanged after failure?

Before looking at the solution below, try to memorize ACID properties of transaction and answer it.

Check-out the POC shown below & wobble your mind :)

Read the rest of this entry »

 

Tags: , , ,

Best Practices : Writing Stored Procedures

Hi All,

I would like to share a handy list which I found today from my old collection during files clean-up. These are few quick tips that every developer should bear in mind while writing T-SQL codes particularly Stored Procedures.

It’s always hard to find the best version of such list but we can definitely make a better version, so you are welcome to make this as a better-list by adding/editing below tips.

Read the rest of this entry »

 
2 Comments

Posted by on March 14, 2012 in SQL Server, Suggestion, T-SQL

 

Tags: , , ,

“Specify Values for Template Parameters” in SQL Server

Hi All,

SQL Server Management Studio provides a dialog box named “Specify Values for Template Parameters” for filling values for the template you have used. This is very handy if you always like interface based interactions with components.

E.g. When you choose any template from Template Explorer, you will see template with lots of place-holders where you can specify your values. In following example I am going to show you the template of basic stored procedure.

Read the rest of this entry »

 
 

Tags: ,

When my SQL table was updated???

Here is very tiny SQL query which will help you find out when the tables are modified in the SQL server database. First you try the * (get all the columns) and then choose the selected ones.

– List all the tables updated in SQL Server (in desc order)
Use your Database Name
Select * from sys.tables order by Modify_date desc

Like wise, you can also check few more readymade tables which store some helpful information for you.

Read the rest of this entry »

 

Tags: ,