USE [DatabaseName] TRUNCATE TABLE [TableName] DBCC CHECKIDENT ("TableName", RESEED, 1);
Showing posts with label Transact SQL. Show all posts
Showing posts with label Transact SQL. Show all posts
Friday, June 12, 2015
Reseeding the Primary Key/ID column of a SQL Table
If you ever need to remove all the records from a SQL database table and then reseed/reset the value of the primary key/ID column, here are the Transact-SQL queries that you can use:
Monday, November 19, 2012
Adding time to a DateTime value obtained via a SQL SELECT query
If you're looking for a good way to add a fixed duration of time to a DateTime value obtained in a SQL SELECT query, the following example provides the Transact-SQL code that can do this:
In this example, I'm obtaining values from a table called Documents and am simply adding one hour to the DateTime column called LastModifiedDateTime and placing that value in a new column called ExpirationDateTime. Of course, you may require a different duration such as days, months, or years and can account for this by adjusting the first parameter of the DATEADD function appropriately (see http://msdn.microsoft.com/en-us/library/ms186819.aspx for more info).
DECLARE @DurationTillExpire INT SET @DurationTillExpire = 1 SELECT dbo.Documents.DocumentId, dbo.Documents.LastModifiedDateTime, (SELECT DATEADD(hour, @DurationTillExpire, dbo.Documents.LastModifiedDateTime)) AS 'ExpirationDateTime' FROM dbo.Documents
In this example, I'm obtaining values from a table called Documents and am simply adding one hour to the DateTime column called LastModifiedDateTime and placing that value in a new column called ExpirationDateTime. Of course, you may require a different duration such as days, months, or years and can account for this by adjusting the first parameter of the DATEADD function appropriately (see http://msdn.microsoft.com/en-us/library/ms186819.aspx for more info).
Wednesday, May 9, 2012
Save SQL Server Management Studio Output in a Pipe-Delimited File
In the event that you would like to obtain a pipe-delimited (i.e. '|') output file of data obtained from a query executed in SQL Server Management Studio, here is a quick procedure for making that happen:
If you're using Windows Server 2008 R2:
If you're using Windows Server 2008 R2:
- Click Start -> Control Panel
- Click Clock, Language, and Region
- Click Change the date, time, or number format
- Click the Additional settings... button
- In the List separator field, replace ',' with '|'
- Click Apply
- Click OK
- Click OK
- Open SQL Management Studio and execute your query
- Write click in the Results window and select Save Results As...
- Select the appropriate directory path
- For File name:, enter an appropriate filename
- For Save as type:, select CSV (Comma delimited)
- Click Save
- You may now open the file in Notepad and confirm that the output is indeed pipe-delimited as opposed to comma-delimited
Once you have the pipe-delimited output you desire, the procedure to reset back to comma delimited is as follows:
- Click Start -> Control Panel
- Click Clock, Language, and Region
- Click Change the date, time, or number format
- Click the Additional settings... button
- In the List separator field, replace '|' with ','
- Click Apply
- Click OK
- Click OK
The procedures will be slightly different for other MS operating systems, but the concept will be the same.
Tuesday, February 28, 2012
SQL Transaction Template
Here is a transaction template that could come in handy any time you need to link a couple of related SQL operations together:
BEGIN TRANSACTION BEGIN TRY --Add related SQL operations here IF @@TRANCOUNT > 0 COMMIT TRANSACTION END TRY BEGIN CATCH SELECT ERROR_MESSAGE() AS ErrorMessage; IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; END CATCH
Subscribe to:
Posts (Atom)