Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

January 26, 2010

Templates for SQL Server Management Studio

by @ 9:45. Filed under SQL Server

Templates for SQL Server Management Studio 2008 are stored in this folder on WIndows XP:

C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql

On my new WIndows 7 machine, the location is:

C:\Users\[user]\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql

Create sub-folders and SQL files there and they show up in the Template Explorer. Really useful…

/Emil

February 6, 2009

Finding depenpencies on a table column

by @ 12:36. Filed under SQL Server

This will list stored procedures, views, etc. that uses a given table column:

SELECT OBJECT_NAME(sd.id) Dependent_Object,
      (SELECT xtype FROM sysobjects so WHERE so.id = sd.id) Object_Type
FROM sysobjects so
INNER JOIN syscolumns sc ON so.id = sc.id
INNER JOIN sysdepends sd  ON so.id = sd.depid AND sc.colid = sd.depnumber
WHERE so.id = OBJECT_ID('tblcategorypage')
AND sc.name = 'categorytype'

Credits for this tip to SQLServerCentral.

/Emil

Bitwise operators in T-SQL?

by @ 9:08. Filed under SQL Server

You won't need them very often but it might be good to know that they're there. Here's an example of how to use a bitwise AND in a where clause:

SELECT * FROM uppdrag WHERE (applikationer & 1)> 0

In this example, 'applikationer' is a column containing several combined flags and we want to check the setting on the flag in the lowest bit.

The bitwise operators are:

February 4, 2009

Easy way to create temporary tables in SQL Server

by @ 19:42. Filed under SQL Server

Have you ever had a need to create temporary database tables in a SQL Server session or procedure? Are you used to creating them by hand like this:

CREATE TABLE #tmp
(
    id INT,
    name VARCHAR(32)
)

Depending on what you need the table for, then there might be an easier way. Have a look at this short session:

SELECT * INTO mytemptable FROM foobar
SELECT * FROM mytemptable
DROP TABLE mytemptable

The first line creates a new temporary table based on the result set of a SELECT query, and stores the result set in the table. The query can be more complex than in the example, with JOINs, WHERE clauses etc. Incredibly useful when mangling and transforming data!

Generate a new GUID in T-SQL

by @ 12:57. Filed under SQL Server

This is how to generate a new GUID in SQL Server T-SQL:

SELECT newid()

Result:

1CA040D5-53D6-4E7C-8D36-1000C8B03A91

September 11, 2008

Installing the client tools for SQL Server

by @ 8:22. Filed under SQL Server

I'm back now from my parental leave, so here's the first posting in a while...

Ever installed SQL Server forgetting to install the client tools such as Management Studio, Profiler, etc? Here's how to install them the easiest way.

Instead of running the main installation program, which may or may not work, run [drive]:\SQL Server x86\Tools\Setup\SqlRun_Tools.msi to install the tools directly. Much quicker and we're not depending on updates made to SQL Server since the installation.

Tip found here.

/Emil

November 19, 2007

Retrieving the database id

by @ 12:22. Filed under SQL Server

It can sometimes be useful to know the id of a given database, e.g. for filtering events when profiling. Here are a few ways to do that:

-- Current database
SELECT db_id()

-- Named database
SELECT db_id('Northwind')

-- All databases
USE master
SELECT name, dbid FROM sysdatabases

January 31, 2006

Formatting dates in SQL

by @ 12:36. Filed under SQL Server

Here's an easy way of extracting ISO dates from a datetime column:

SELECT top 10
convert(varchar(10),starttid, 120) AS datum
-- style = 120 results in yyyy-mm-dd
FROM pass

Result:

2005-08-02
2005-08-04

January 25, 2006

UPDATE with ambigous table name

by @ 13:06. Filed under SQL Server

When doing an UPDATE and there are two occurrances of the table to update, then use the alias as table name. Example

UPDATE alias1       -- NOT the table name!
SET foo = 'bar'
FROM table_foobar alias1
INNER JOIN table_foobar alias2 ON alias2.foo = alias1.bar

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

September 2010
M T W T F S S
« Jun    
 12345
6789101112
13141516171819
20212223242526
27282930  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

36 queries. 0.594 seconds