Generate a unique number in Sql Server 2005
2008-04-02 13:15
405 查看
We often find ourselves in the need of generating unique numbers in our database applications.
Let us quickly take a tour of how to do that using Sql server 2005.
SQL
Server contains the NEWID() function. This function creates a unique
value of type uniqueidentifier. We can use this function in several
ways to generate unique numbers to suit our requirements :
Generate GUID's :
UniqueIndentifiers are also knows as GUID's. To generate a GUID use :
SELECT NEWID() as GuidNo
generates FBF2D8E9-F8BE-4F0B-9D49-7CA7C2E3F22C on my machine
Generate only digits :
SELECT ABS(CAST(CAST(NEWID() AS VARBINARY(5)) AS Bigint)) as UniqueNo
generates 427357674589 on my machine
Generate fixed digit unique numbers :
At times, we may also need to generate fixed digit numbers. You can do that in the following manner :
SELECT REPLACE(STR(CAST(CAST(NEWID() AS binary(5)) AS bigint),12),0,0)
generates 470370453006 on my machine
Let us quickly take a tour of how to do that using Sql server 2005.
SQL
Server contains the NEWID() function. This function creates a unique
value of type uniqueidentifier. We can use this function in several
ways to generate unique numbers to suit our requirements :
Generate GUID's :
UniqueIndentifiers are also knows as GUID's. To generate a GUID use :
SELECT NEWID() as GuidNo
generates FBF2D8E9-F8BE-4F0B-9D49-7CA7C2E3F22C on my machine
Generate only digits :
SELECT ABS(CAST(CAST(NEWID() AS VARBINARY(5)) AS Bigint)) as UniqueNo
generates 427357674589 on my machine
Generate fixed digit unique numbers :
At times, we may also need to generate fixed digit numbers. You can do that in the following manner :
SELECT REPLACE(STR(CAST(CAST(NEWID() AS binary(5)) AS bigint),12),0,0)
generates 470370453006 on my machine
相关文章推荐
- Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET
- Work with XML Data Type in SQL Server 2005 from ADO.NET 2.0
- XQuery in SQL Server 2005
- [原创]T-SQL Enhancement in SQL Server 2005 - Part I
- How to check number of Active connections in SQL server?
- Dejan Sunderic, «Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET» (3rd edition)
- sql express 2008 - Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instan
- DDL Triggers in SQL Server 2005
- Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection
- Working with tempdb in SQL Server 2005
- [转]各种分页 in SQL Server 2005, SQL Server 2000, MS Access and MySQL
- Missing Indexes in SQL Server 2005
- Give the New PIVOT and UNPIVOT Commands in SQL Server 2005 a Whirl
- T-SQL Enhancement in SQL Server 2005[上篇]
- SQL语句执行顺序 in SQL Server 2005
- SecurityException when using SQL Server 2005 JDBC driver in WebSphere 6
- Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
- Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection
- How to Alter a Replicated Article in SQL Server 2005
- [翻译-收藏] Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005