Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

15 Dec 2010

T-SQL Snippets

FAO bizTech Team : Add frequently used SQL Snippets here.

 

Get Yesterday’s Date without, any time

-- Add -1 to current time + format without hours
Select DATEADD (day , -1 , CONVERT(varchar(10), getdate(), 101) )

 
T-SQL Get dates on a specific day when you don't care about time

Quite often I have to write queries to return rows where something happened on a specific day. But the values in the table could be anytime within that day.
Previous I would pass in a start date, end date to do this, but the query then needs 2 parameters and it looks a bit cumbersome.
I came across this method which I now employ. Don't know if this has much performance impact, but it certainly is quicker to code and takes less parameters from calling application.
Where
(
DateDiff(day, @ReportStartDate,TerminalSession.StartDate) = 0
--StartDate >= @ReportStartDate
--And
--EndDate >= @ReportEndDate
)

2 Mar 2010

Using File Streams SQL Server 2008

Had a bit of trouble getting these up and running. A lot of the samples out there missed steps are did them in wrong order (hopefully below is full list of steps).

Enable the file stream on the server:
http://msdn.microsoft.com/en-us/library/cc645923.aspx
To enable and change FILESTREAM settings

  1. On the Start menu, point to All Programs, point to Microsoft SQL Server 2008, point to Configuration Tools, and then click SQL Server Configuration Manager.
  2. In the list of services, right-click SQL Server Services, and then click Open.
  3. In the SQL Server Configuration Manager snap-in, locate the instance of SQL Server on which you want to enable FILESTREAM.
  4. Right-click the instance, and then click Properties.
  5. In the SQL Server Properties dialog box, click the FILESTREAM tab.
  6. Select the Enable FILESTREAM for Transact-SQL access check box.
  7. If you want to read and write FILESTREAM data from Windows, click Enable FILESTREAM for file I/O streaming access. Enter the name of the Windows share in the Windows Share Name box.
  8. If remote clients must access the FILESTREAM data that is stored on this share, select Allow remote clients to have streaming access to FILESTREAM data.
  9. Click Apply.
  10. In SQL Server Management Studio, click New Query to display the Query Editor.
  11. In Query Editor, enter the following Transact-SQL code:
  12. EXEC sp_configure filestream_access_level, 2
    RECONFIGURE
Or use 1 for T-SQL access only


Enable Filestream in your database
ALTER DATABASE ArkleStream ADD FILEGROUP ArkleStream_Filegroup CONTAINS FILESTREAM

ALTER DATABASE ArkleStream ADD FILE  (
    NAME = ArkleStream_Filegroup
    ,FILENAME = 'c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ArkleStream_Filegroup_Data'
    )
TO FILEGROUP ArkleStream_Filegroup

Create a table using a filesteam column
CREATE TABLE [dbo].[TestFileStreamTable](
    [SomeOtherColumn] [varchar](100) NULL,
    [FileStreamColumn] [varbinary](max) FILESTREAM  NULL,
    [UniqueID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [Test] [varbinary](max) NULL,
UNIQUE NONCLUSTERED
(
    [UniqueID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] FILESTREAM_ON [ArkleStream_Filegroup]

16 Dec 2009

SQL Server Transaction Log Full

Sometimes, for whatever reason the transaction log gets full up.
And shrinking via management studio doesn't work;

sp_detach_db [database name]
Then rename or delete the log file

Then
EXEC sp_attach_db @dbname = N'pubs',
@filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
This will create a new blank log file and get you up and running.

26 Oct 2009

SQL Server 2008 & Windows 7

We have just moved Arkle ePos to use SQL Server 2008. No problems at all.
We have also tested on Windows 7 (32 bit) and it works.

26 Sept 2009

T-SQL Get dates on a specific day when you don't care about time

Quite often I have to write queries to return rows where something happened on a specific day. But the values in the table could be anytime within that day.

Previous I would pass in a start date, end date to do this, but the query then needs 2 parameters and it looks a bit cumbersome.

I came across this method which I now employ. Don't know if this has much performance impact, but it certainly is quicker to code and takes less parameters from calling application.

Where
(
DateDiff(day, @ReportStartDate,TerminalSession.StartDate) = 0

--StartDate >= @ReportStartDate
--And
--EndDate >= @ReportEndDate
)