SQL

...now browsing by tag

 
 

Query to get the column names/types for all tables in SQL 2005

Wednesday, February 11th, 2009

SELECT schemas.name AS [Schema],
tables.name AS [Table],
columns.name AS [Column],
CASE
WHEN columns.system_type_id = 34 THEN ‘byte[]’
WHEN columns.system_type_id = 35 THEN ‘string’
WHEN columns.system_type_id = 36 THEN ‘System.Guid’
WHEN columns.system_type_id = 48 THEN ‘byte’
WHEN columns.system_type_id = 52 THEN ‘short’
WHEN columns.system_type_id = 56 THEN ‘int’
WHEN columns.system_type_id = 58 THEN ‘System.DateTime’
WHEN columns.system_type_id = 59 THEN ‘float’
WHEN columns.system_type_id = 60 THEN ‘decimal’
WHEN columns.system_type_id = 61 THEN ‘System.DateTime’
WHEN columns.system_type_id = 62 THEN ‘double’
WHEN columns.system_type_id = 98 THEN ‘object’
WHEN columns.system_type_id = 99 THEN ‘string’
WHEN columns.system_type_id = 104 THEN ‘bool’
WHEN columns.system_type_id = 106 THEN ‘decimal’
WHEN columns.system_type_id = 108 THEN ‘decimal’
WHEN columns.system_type_id = 122 THEN ‘decimal’
WHEN columns.system_type_id = 127 THEN ‘long’
WHEN columns.system_type_id = 165 THEN ‘byte[]’
WHEN columns.system_type_id = 167 THEN ‘string’
WHEN columns.system_type_id = 173 THEN ‘byte[]’
WHEN columns.system_type_id = 175 THEN ‘string’
WHEN columns.system_type_id = 189 THEN ‘long’
WHEN columns.system_type_id = 231 THEN ‘string’
WHEN columns.system_type_id = 239 THEN ‘string’
WHEN columns.system_type_id = 241 THEN ‘string’
WHEN columns.system_type_id = 241 THEN ‘string’
END AS [Type],
columns.is_nullable AS [Nullable]

FROM sys.tables tables
INNER JOIN sys.schemas schemas ON (tables.schema_id = schemas.schema_id )
INNER JOIN sys.columns columns ON (columns.object_id = tables.object_id)

WHERE tables.name <> ‘sysdiagrams’
AND tables.name <> ‘dtproperties’

ORDER BY [Schema], [Table], [Column], [Type]

Written by Deepak Vasa - Visit Website

LINQ to SQL – Not Dead

Monday, November 3rd, 2008

For those who are not aware, last week there has been some misunderstanding regarding the future of LINQ to SQL based on announcements made by MS at PDC. Well after reading a few blogs around the Blogosphere I have come accross the following posts which explain that there is future for L2S:

Read Damien Guard’s blog ->http://damieng.com/

Read Huagati’s Blog -> http://blog.huagati.com/res/index.php/2008/11/03/quiet-blog-gone-fishing-is-linq-to-sql-dead-etc/

Further I found this cool image supporing L2S here

Long live LINQ to SQL !!!

Long live LINQ to SQL !!!

Written by Deepak Vasa - Visit Website

SQL Server 2008 – my thoughts….

Tuesday, August 12th, 2008

I have been reading about SQL Server 2008 for a while and today I got a glimpse of the various features and how it can help developers at the Victoria Dot Net users group meeting. Thanks to the special presentation by Chris Hewitt – “What should developers know about SQL Server 2008?”

A few days ago, I read about the new “File Stream” data type and my initial thoughts were how would I use this new feature and where would this be more usefull, later I realised that I could use the FileStreaming data type to store all the various CSV reports that I generate from our various applications. So effectively instead of storing it on a file server and putting in the reference in SQL, I could easily store the entire CSV file in to SQL Server and let it worry about where and how it stores it. More over I can also have all the report metadata in one location (sweet :).

In the presentation Chris showed us a few examples on how we can send a Table as parameter to Stored Procedures and the new Merge Statement.  Well, frankly speaking it took me a few seconds to comprehend the huge benifits of sending in a table as a parameter to a stored procedure. The biggest benifit is the ability to insert large number of rows in one go by passing the data as a table parameter. Previously you could do that with temp tables but those tables were only specific to the stored procedure which creates them, however with this new feature we have some thing on the lines of Global Temp Tables. For more detailed explanation and examples, please refer this article

Coming to the Merge statement that’s another cool feature that has huge number of benefits. In scenarios where you need to to insert, update, or delete data based on certain conditions, programmers have to take care of the actions in the application, however with Merge statements we can do the insert, update or delete in one single statement based on the join conditions. For more information, please refer this article at BuilderAU.

Another cool feature is the Filtered Indexing. As the name suggests, all it means is that when we create Indexes for a table in SQL Server 2008 based on a particular criteria (simple where clause). I am very keen to test this on one our biggest tables (9 million rows and counting). At the moment  it takes quite a bit of time to query this table for certain criteria, I would love to do some bench marking on this table and see how efficient the Filtered Indexes are.

Finally a one more interesting feature is the ability to Debug Sql Statements in the same way as .NET code. Chris showed how we could debug any SQL statement or Stored procedure, and also the MS has finally introduced Intellisense for SQL statements. Allthough I have been told that 3rd party tools have been providing SQL intellisense for quite some time now.

For those of you who are interested in other features like Sparse, new DateTime formats, new Convert functionality etc, please refer SQL Server 2008 @ Microsoft.

Written by Deepak Vasa - Visit Website

How to convert ntext to nvarchar(max) in SQL 2005 – the right way

Tuesday, August 5th, 2008

Today, I found this very very useful blog on how to convert ntext fields to nvarchar(max) in Sql 2005. Apparently when converting a field from ntext to nvarchar(max), SQL Server does not change the internal structure and this is bad as the performance slows down significantly. However the following blog has got a simple fix to make SQL do the correct change.

http://geekswithblogs.net/johnsPerfBlog/archive/2008/04/16/ntext-vs-nvarcharmax-in-sql-2005.aspx

Written by Deepak Vasa - Visit Website