SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record

It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.
SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.

To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.

Functin - How to Get Leap Year

/*Detecting leap year*/
CREATE FUNCTION dbo.IsLeapYear(@Year int)
DECLARE @RetVal bit
IF (@Year % 400) = 0 --//every 400 years
SET @RetVal = 1
IF (@Year % 100) = 0
SET @RetVal = 0
IF (@Year % 4) = 0
SET @RetVal = 1
SET @RetVal = 0

MaxLength not working in ASP.NET textbox multiLine

Great code I found for maxlength issue with .NET textboxes. .NET textboxes, if TextMode is set to MutilLine, the maxlength property just doesn't work, sux. So after playing around with different soloutions. I found this on, thanks to a guy named "Leo." So a big thanks to him!

Add this function in JavaScript on your page, I stuck it my "functions.js" file I include on the page anyway.

function checkMaxLen(txt,maxLen) {
if(txt.value.length > (maxLen-1)) {
var cont = txt.value;
txt.value = cont.substring(0,(maxLen -1));
return false;

Then on the textbox use something like this:

<asp:TextBox runat="server" ID="txtComments" CssClass="comment_textbox" Height="75px" TextMode="MultiLine" onkeyup="return checkMaxLen(this,151)"></asp:TextBox>

Notice the function passes the textbox (this) and the maxlength you want, in this case 150, notice I have 151, the function will limit the maxlength minus one... So if you want to limit the textbox with mutiline set to 100, you enter 101, make sense?

Linked Server - How to pass a variable to a linked server query

This article describes how to pass a variable to a linked server query.

When you query a linked server, you frequently perform a pass-through query that uses the OPENQUERY, OPENROWSET, or OPENDATASOURCE statement. You can view the examples in SQL Server Books Online to see how to do this by using pre-defined Transact-SQL strings, but there are no examples of how to pass a variable to these functions. This article provides three examples of how to pass a variable to a linked server query.

To pass a variable to one of the pass-through functions, you must build a dynamic query.

Any data that includes quotes needs particular handling. For more information, see the "Using char and varchar Data" topic in SQL Server Books Online and see the following article in the Microsoft Knowledge Base:

Pass Basic Values

When the basic Transact-SQL statement is known, but you have to pass in one or more specific values, use code that is similar to the following sample:

      DECLARE @TSQL varchar(8000), @VAR char(2)
     SELECT  @VAR = 'CA'
     SELECT  @TSQL = 'SELECT * FROM OPENQUERY(MyLinkedServer,''SELECT * FROM pubs.dbo.authors WHERE state = ''''' + @VAR + ''''''')'
     EXEC (@TSQL)

Pass the Whole Query

When you have to pass in the whole Transact-SQL query or the name of the linked server (or both), use code that is similar to the following sample:

DECLARE @OPENQUERY nvarchar(4000), @TSQL nvarchar(4000), @LinkedServer nvarchar(4000)
SET @LinkedServer = 'MyLinkedServer'
SET @OPENQUERY = 'SELECT * FROM OPENQUERY('+ @LinkedServer + ','''
SET @TSQL = 'SELECT au_lname, au_id FROM pubs..authors'')'

Use the Sp_executesql Stored Procedure

To avoid the multi-layered quotes, use code that is similar to the following sample:

DECLARE @VAR char(2)
EXEC MyLinkedServer.master.dbo.sp_executesql
    N'SELECT * FROM pubs.dbo.authors WHERE state = @state',
    N'@state char(2)',

Upgrading to IE8 breaks debugging with Visual Studio 2005

Since Microsoft published IE8 as a Windows Update and my boss said he upgraded to it, I figured it was safe to upgrade to it as well. I’ve been enjoying the changes for a couple days now, but when I attempted to debug an ASP.NET application with Visual Studio 2005, it would no longer stop at a breakpoint. Pretty frustrating if you ask me. I’ve found a few articles online describing problems, most of which I haven’t encountered yet. You may want to implement all these workarounds.

The problem I was having appears to be related to how IE8 uses multiple processes. Apparently VS 2005 doesn’t know which process to attach to. Here are three solutions:

1.Don’t have IE8 running at the time you want to debug. This worked for me.
2.Modify the registry as described in the second link above. This worked for me as well. Here are the steps:
1.Open RegEdit
2.Browse to HKEY_LOCAL_MACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main
3.Add a DWORD value called TabProcGrowth with a value of 0
4.Turn off protected mode browsing in IE (Security page of Internet Options) if you are running on Vista or newer.
3.Uninstall IE8 to revert back to IE7. I don’t really want to do this though. I’ve grown particularly fond of a few of the new features in IE8, such as previous session restore (although I wish it would allow me to restore more than just the last session) and improvements in search.

I don’t know if this is a problem with VS 2008, but some of the articles seem to imply it only applies to VS 2005. Maybe it’s time to upgrade Visual Studio

Referencing 2.0 Web Services (asmx) in Visual Studio 2008

Recently I needed to add a reference to a classic (.Net 2.0) web service inside a new project that was being created via Visual Studio 2008.
Now, I have not done a lot of work with WebServices in the past 6-9 months, but thought it would be cake. Please keep in mind, the service I wanted to connect to was NOT a WCF Service, it was a standard ASMX service.
Below is what I thought would work, but did not

1- Attempt to Add A Service Reference

2- Choose Services in Solution (my Project is in the same solution file).

3- View the generated service implementation.

4- WCF Style code implementation

What you will notice from step 4 is that the way you go about implementing this service is in the WCF style. This is not what I was looking for. I was wanting to reference this like all the other code in our projects.

Below is what DID work

2- Attempt to Add A Service Reference

3-Need to add the service as a Web Reference

4- Finally need to search for and find your web service
5- Lastly, if you have done everything correctly, you should see the following.
Now I know that the preferred service type is now WCF, but come-on not everyone is using WCF just yet. Adding a traditional web service reference is way too much friction. Why is not possible to add a reference from the solution explorer? Really can anyone answer me that.
Till next time,

Date/Time Conversions Using SQL Server

There are many instances when dates and times don't show up at your doorstep in the format you'd like it to be, nor does the output of a query fit the needs of the people viewing it. One option is to format the data in the application itself. Another option is to use the built-in functions SQL Server provides to format the date string for you.

SQL Server provides a number of options you can use to format a date/time string. One of the first considerations is the actual date/time needed. The most common is the current date/time using getdate(). This provides the current date and time according to the server providing the date and time. If a universal date/time is needed, then getutcdate() should be used. To change the format of the date, you convert the requested date to a string and specify the format number corresponding to the format needed. Below is a list of formats and an example of the output:

Format # Query (current date: 12/30/2006) Sample
1 select convert(varchar, getdate(), 1) 12/30/06
2 select convert(varchar, getdate(), 2) 06.12.30
3 select convert(varchar, getdate(), 3) 30/12/06
4 select convert(varchar, getdate(), 4) 30.12.06
5 select convert(varchar, getdate(), 5) 30-12-06
6 select convert(varchar, getdate(), 6) 30 Dec 06
7 select convert(varchar, getdate(), 7) Dec 30, 06
10 select convert(varchar, getdate(), 10) 12-30-06
11 select convert(varchar, getdate(), 11) 06/12/30
101 select convert(varchar, getdate(), 101) 12/30/2006
102 select convert(varchar, getdate(), 102) 2006.12.30
103 select convert(varchar, getdate(), 103) 30/12/2006
104 select convert(varchar, getdate(), 104) 30.12.2006
105 select convert(varchar, getdate(), 105) 30-12-2006
106 select convert(varchar, getdate(), 106) 30 Dec 2006
107 select convert(varchar, getdate(), 107) Dec 30, 2006
110 select convert(varchar, getdate(), 110) 12-30-2006
111 select convert(varchar, getdate(), 111) 2006/12/30

8 or 108 select convert(varchar, getdate(), 8) 00:38:54
9 or 109 select convert(varchar, getdate(), 9) Dec 30 2006 12:38:54:840AM
14 or 114 select convert(varchar, getdate(), 14) 00:38:54:840

You can also format the date or time without dividing characters, as well as concatenate the date and time string:

Sample statement Output
select replace(convert(varchar, getdate(),101),'/','') 12302006
select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','') 12302006004426