In my previous post, I showed some ways to query system objects in SQL Server.  If you switch between MS SQL and other databases, that's the way to go, as the information schema views comply with ANSI SQL-92 standards.  But there is another way to query information from MS SQL Server system catalogs it's by using objects called catalog views.  There's a boat load of these views available, mostly starting with 'sys' and they are queried as SELECT cols from sys.<viewname>.   If you have SQL Management Studio installed, you can find them under the Views->System Views node.

Here's a great starting point for writing your own TSQL catalog views.  But I will use the same samples from my previous post here, but rewritten to use the catalog views instead.

Retrieve a listing of tables in a database:

SELECT * FROM sys.tables

SELECT * FROM sys.views

Retrieve a listing of all the columns in a table (and their basic data types):

SELECT c.name AS column_name,c.column_id,t.name AS type_name,t.is_user_defined,t.is_assembly_type,c.max_length,c.precision,c.scale FROM sys.columns AS c JOIN sys.types AS t ON c.user_type_id=t.user_type_id WHERE c.object_id = OBJECT_ID('products')

Retrieve the primary keys of a table:

SELECT c.name AS column_name,c.column_id,t.name AS type_name,t.is_user_defined,t.is_assembly_type,c.max_length,c.precision,c.scale FROM sys.columns AS c JOIN sys.types AS t ON c.user_type_id=t.user_type_id WHERE c.object_id = OBJECT_ID('products')

Retrieve the foreign keys of a table (separate queries for PK/FK retrieval needed)

SELECT c.name AS column_name,c.column_id,t.name AS type_name,t.is_user_defined,t.is_assembly_type,c.max_length,c.precision,c.scale FROM sys.columns AS c JOIN sys.types AS t ON c.user_type_id=t.user_type_id WHERE c.object_id = OBJECT_ID('products')

Retrieve a listing of stored procedures:

SELECT * FROM sys.procedures

I keep the scripts from the previous post (and other scripts) around as I used to do much more with a variety of databases, plus I've just had those scripts hanging around for a long time as .SQL files.  However, I do use these catalog views as well, as they are quite easy to work with - and the typing's easy too!

There's a great FAQ on using these views, which I keep in my bookmarks so I can frequently access, then copy and paste from when I have to work in SQL Mgmt Studio/Enterprise Mgr.


 
Categories: SQL | Tips

I have several SQL utility scripts (as well as classic ASP scripts, controls, utils, etc...) that I use regularly for everything from retrieving table listings to creating select, insert, update & delete stored procedures.  Below are some of my favorite, daily use scripts. These can be used for quick documentation or for helping to create and manage objects in your database.

Retrieve a listing of all the tables and/or views in a database:

SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_type = 'BASE TABLE'

Retrieve a listing of all the columns in a table (and their basic data types):

DECLARE @tablename VARCHAR(100)

SET @tablename = 'products'

SELECT column_name, data_type, character_maximum_length FROM INFORMATION_SCHEMA.Columns WHERE table_name = @tablename

Retrieve the primary and foreign keys of a table:

DECLARE @tablename VARCHAR(100)

SET @tablename = 'products'

SELECT C.constraint_name, constraint_type FROM information_schema.table_constraints C INNER JOIN INFORMATION_SCHEMA.Key_Column_Usage K ON K.table_name=C.table_name AND K.Constraint_Name = C.Constraint_Name WHERE c.table_name=@tablename

Retrieve a listing of stored procedures:

SELECT specific_name FROM information_schema.routines WHERE routine_type = 'PROCEDURE'

All of these scripts use the INFORMATION_SCHEMA views that are built into SQL.
 

Categories: SQL | Tips

November 19, 2007
@ 09:23 PM

Get it at the MS download center.  There's also an express version too!


 
Categories: Announcements | SQL

Jason and I have been working on some projects together and have had this problem bugging us where the data in a certain table was 'mysteriously' deleting itself (re: this post).  We had checked a particular stored procedure and despite both of us having looked it over many times, we just didn't see that little extra "@" character stuck in there (oopsie).  What we saw was what we humans wanted to, not what was actually there.

This reminds me of those emails you get where you are asked to comb through a paragraph and count the number of times the letter 'F'

Finished files are the result of years of scientific study combined with the experience of many years

There are 6 letter F's in this paragraph.  Most folks skip over the the word 'of' and only count the letter F in the larger words.  Humans skim when we read.  The computer does not.

Here was our procedure (table names changed to protect the innocent)

ALTER PROCEDURE [dbo].[ProcedureName]
@EmailAddress varchar(256)
AS
IF EXISTS (SELECT TableID from The_Table_Name_Was_Pretty_Long
WHERE EmailAddress=@EmailAddress)
BEGIN
         DELETE The_Table_Name_Was_Pretty_Long
         WHERE @EmailAddress = @EmailAddress
END

Obviously now the WHERE @EmailAddress = @EmailAddress should have been WHERE EmailAddress = @EmailAddress.  Indeed, the computer takes your instructions very seriously.

This is fairly common phenomenon though, I see it in my classes every day as well as in the field by both novice and experienced professionals.  The simpler it is, the easier it is for you to overlook it.  If only that machine could just read our minds!

This also reminds me of the words of Charles Babbage

"On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."
I know I said this last time, but really, next time I'll be look more closely and be more careful.
 
Categories: Software Development | SQL