Posts Tagged ‘SQL Server’
Creating Insert Statements for SQL Server
Ran across an interesting approach to generating insert statements for SQL Server 2005 (and other variants). If uses a less well known function called master.dbo.fn_varbintohexstr. The approach that was taken was to use the fn_varbintohexstr function to encode the data so you did not need to use cursors or any fancy parsing to handle unicode and quotes in the generated insert statements. I thought it was a bit clever.
Note: you might need to register at SQLServerCentral.com to gain access to the site to read the above linked article.
Calculating Age in T-SQL
Here’s a decent article on calculating the age of a person (or anything else) written by Lynn Pettis. Discusses some pitfalls with leap years.
Using and Managing SQL Server Aliases
I know that not many people I have met in my SQL Server experience have used SQL Aliases. In many ways it is one of the most useful features of SQL Server. Roman Rehak wrote a great article about SQL Server Aliases. You have to be registered to see the full article, but some of the highlights are discussed below.
Creating an alias all depends on which client tools are installed on the computer you are using. Here are the common methods for running the SQL Client Configuration Tool:
- If SQL Server 2000 client tools are installed, use the SQL Client Network Utility and select the Alias tab.
- If SQL Server 2005 client tools are installed, use the SQL Server Configuration Manager and expand the SQL Native Client Configuration node to find the node for Aliases.
- If you do not have any client tools installed, you can still get at the Client Network Utility by executing “cliconfg” from any command prompt.
Aliases are useful in the following scenarios:
- Create an alias that is a friendlier name than the server name itself.
- In a Development/QA/Production environment. Using aliases allow you to keep the code base across your servers the same without having to change source code (sort of related to the last bullet).
- Enforce the use of a particular protocol. If you get the “Cannot generate SSPI context” error you can try creating an alias to force the use of the Named Pipes.
- A distributed environment with multiple servers that reference each other using the Linked Servers feature. You can define the linked servers with the aliases instead of the actual names. This makes future moving of databases and such much easier since you will not need to chnage the linked server names in your code.
- In a high-availability environment to allow you to quickly change the server without changing all your code (such as web applications and such).
I’m sure there are other useful reasons to use aliases, but that covers a few of them that show the power of the alias function.
If you have any other good ideas, please comment on the post.
Resetting the IDENTITY counter in SQL Server T-SQL
You’ve been there before. You are busily testing your import routines and running up the Identity column in your main import table. Before you know it the value is in the multi-millions and you just want to reset it back to 1.
The way to accomplish this is to use the DBCC CHECKIDENT command.
For example:
DBCC CHECKIDENT (SalesForce, reseed, 0)
This assumes your table is called SalesForce and you want to start the numbering at 1. If you wanted to start the number at 2000 you can substitute 0 with 1999. There has been some discussion that has said if the table never had records added to it the reseed value itself will be used instead of the reseed + 1 value. You should not need to use this statement for a “new” table.
Credit for this post is given to Pinal Dave over at http://sqlauthority.com/. The specific post this I am referring to is the one at this page.