Use INFORMATION_SCHEMA to find data about your database

Have you ever been in this situation?  You’re the new developer for a database powered application and you have to figure out what tables contain a specific column?  Or maybe you’ve got a legacy database that needs it’s stored procedures and user defined functions cleaned up?  Or perhaps you’ve been assigned a unique database inventory task that requires you to report on the structure of your database, rather than the contents of your database.  Besides cracking open your favorite SQL editor and clicking through your database by hand, you might find the data you need from your database’s INFORMATION_SCHEMA System View.

All of the RDMS systems I’ve worked with have built in System Views ( or tables ) that contain data about the structure and contents of your database.  These views are there to help you with non data tasks.  For the sake of this post we’re only going mention the INFORMATION_SCHEMA, which is the system view I use most in SQL Server, and MySQL.  For your reference, here are links to INFORMATION_SCHEMA docs for four popular RDMS systems.

  1. SQL Server – http://msdn.microsoft.com/en-us/library/ms186778.aspx
  2. MySQL – http://dev.mysql.com/doc/refman/5.5/en/information-schema.html
  3. Oracle – http://download.oracle.com/docs/cd/E17952_01/refman-5.0-en/information-schema.html
  4. DB2 – http://www.tar.hu/sqlbible/sqlbible0100.html

If you work with SQL Server, you’ll be happy to know you can see all of the System Views in Management Studio by default.

SQL Server Management Studio shows all System Views

System Views in the AdventureWorks database

If you work with MySQL, you can see the INFORMATION_SCHEMA database in MySQL Workbench, but you’ll have to enable it in Preferences first.

Edit > Preferences > SQL Editor > Check 'Show Metadata Schemata'

Now for the MySQL users, turning on the Metadata Schemata is not required in order to query those system tables.  Assuming your user has appropriate permissions, you can always fire up Workbench and fire a query like this.

[sql]
SELECT *
FROM INFORMATION_SCHEMA.Columns
WHERE Column_Name LIKE ‘%user%’
[/sql]

MySQL has INFORMATION_SCHEMA tables too!

Now that you know about INFORMATION_SCHEMA, you should be happy to know this is part of the SQL-92 standard. Which means whatever sql scripts you write in MySQL, will most likely work with SQL Server as well. So go ahead and start querying your database, you’ll probably find uses for the INFORMATION_SCHEMA in your daily life really quickly.

I’m going to assume you’ve got the general idea here, so here are a few sql scripts that I’ve used over the years.  The first three are informational queries, and the last three are stored procedures that generate .NET or Coldfusion code based off of a table name.

Find all tables with the column EmployeeID

[sql]
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE ‘EmployeeID’
[/sql]

Which tables have an EmployeeID column?

How many tables have the word employee in the name

[sql]
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE LOWER(TABLE_NAME) LIKE ‘%employee%’
[/sql]

How many %employee% tables are in the DB?

How many SPROCs and UDFs does our database contain?

[sql]
SELECT s.SPROCs, f.UDFs
FROM
(
SELECT COUNT(ROUTINE_NAME) AS ‘SPROCs’, NULL AS ‘UDFs’
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = ‘PROCEDURE’
) AS s,
(
SELECT NULL AS ‘SPROCs’, COUNT(ROUTINE_NAME) AS ‘UDFs’
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = ‘FUNCTION’
) AS f
[/sql]

How many stored procedures and user defined functions are in the DB?

The stored procedures listed below all accept an incoming table name, then reads data from INFORMATION_SCHEMA to generate code for the specified database table.  Instead of listing the raw SQL, I’m just showing a sample result and allowing you to download the raw SQL sprocs.

Generate C# ADO.NET to VO Stored Procedure

Build custom C# ADO.NET to custom VO fillerup code

download stored procedure


Generate .NET VO class Stored Procedure

Generate C# VO class for DB table name

download stored procedure

Generate Coldfusion CFFunction with CFQUERY Stored Procedure

Generate Coldfusion CFFunction with CFQUERY for specified table name

download stored procedure

I use these System Views almost daily, and there are a lot of scenarios where my job would take a lot longer to do if I didn’t know about this information.  The INFORMATION_SCHEMA also feeds my passion to build code that makes code, just love it.  Hopefully this nickel tour was enough information to help somebody out.

An aggregate may not appear in the set list of an UPDATE statement

Ever seen the error “An aggregate may not appear in the set list of an UPDATE statement” when working with SQL Server?  I ran into this one recently after trying to put a COUNT in an UPDATE statement.  I was rewriting some legacy code to use a stored procedure, and it turned out to be the perfect case for a Temporary Table.

Instead of boring you with a work scenario, let’s take a simpler one that uses the AdventureWorks database.  This example will create a list of sales people, total order count for each person, and store this list a single table variable to be used as the final data table.

Should be three simple steps right?

1. Create @Table variable

[sql]
DECLARE @SalesPeople TABLE
(
EmployeeID int NOT NULL,
SalesPersonID int NOT NULL,
FullName varchar(200) NOT NULL,
Title varchar(200) NOT NULL,
sales_count int NULL default 0
)
[/sql]

2. INSERT sales people into @Table

[sql]
– HACKISH : Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT e.EmployeeID, sp.SalesPersonID,
c.FirstName + ‘ ‘ + c.LastName as FullName,
e.Title
FROM Sales.SalesPerson sp,
HumanResources.Employee e,
Person.Contact c
WHERE sp.SalesPersonID = e.EmployeeID
AND e.ContactID = c.ContactID
[/sql]

3. UPDATE @Table with COUNT

[sql]
UPDATE @SalesPeople
SET
sales_count = COUNT( soh.SalesOrderID )
FROM @SalesPeople sp, Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
AND sp.SalesPersonID = soh.SalesPersonID
[/sql]

Not COUNT allowed in an UPDATE SET statement

The third step is where the original error comes in, so let’s update this to four steps and see how a Table Variable gets through this.

1 & 2 – Repeat from above

3. Create Table Variable of order counts

[sql]
SELECT soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO #SalesOrderCounts
FROM Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID
[/sql]

4. Update @Table with order counts

[sql]
UPDATE @SalesPeople
SET sales_count = tmp.sales_count
FROM @SalesPeople sp, #SalesOrderCounts tmp
WHERE sp.SalesPersonID = tmp.SalesPersonID
[/sql]

And here’s the full script from start to finish with the table variable in use.

[sql]
– Master table of sales people
DECLARE @SalesPeople TABLE
(
EmployeeID int NOT NULL,
SalesPersonID int NOT NULL,
FullName varchar(200) NOT NULL,
Title varchar(200) NOT NULL,
sales_count int NULL default 0
)

– Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT e.EmployeeID, sp.SalesPersonID,
c.FirstName + ‘ ‘ + c.LastName as FullName,
e.Title
FROM Sales.SalesPerson sp, HumanResources.Employee e, Person.Contact c
WHERE sp.SalesPersonID = e.EmployeeID
AND e.ContactID = c.ContactID

– put sales counts into the other kind of #tableVariable
SELECT soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO #SalesOrderCounts
FROM Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID

– Update our master @table with data from #tableVariable
UPDATE @SalesPeople
SET sales_count = tmp.sales_count
FROM @SalesPeople sp, #SalesOrderCounts tmp
WHERE sp.SalesPersonID = tmp.SalesPersonID

– dump the results
SELECT FullName, Title, sales_count
FROM @SalesPeople

– cleanup
drop table #SalesOrderCounts
[/sql]

Eleven Coldfusion-ish tips from the field

I’ve had this running list of Coldfusion tips on my wall for the last few years and it’s time to get these online.  All of the items in this list came from Coldfusion projects over the last few years, but a good portion of these could easily be considered tips for server programmers.  I definitely run into the same items when programming Asp.NET.

There is no rhyme or reason here, just some things I felt need to be repeated.

1. PreserveSingleQuotes()

This one came in really handy on a project requiring large text files to be imported into a MySQL database.  I used Coldfusion to upload and read the files into large INSERT chunks using MySQL’s multi – row INSERT syntax. Code built the VALUES portion of the SQL, then I just fed the data into a function for insertion.

[sql]
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
[/sql]
[coldfusion]
<cfquery name="insert_data" result="insert_result" DATASOURCE="#request.dsn#" USERNAME="#request.dbuser#" PASSWORD="#request.dbpswd#">
INSERT INTO table
( column1, column2, column3, column4, column5, column6, column7 )
VALUES
#PreserveSingleQuotes( insert_values )#
</cfquery>
[/coldfusion]

2. What if GENERATED_KEY doesn’t work?

If you’re using MySQL and the GENERATED_KEY property of your cfquery objects isn’t populating, you can use LAST_INSERT_ID instead.

[sql]SELECT LAST_INSERT_ID();[/sql]

3. Use ArrayAppend when building strings

Classic performance tuning tip for just about any programming language.  Here I’ll give a Coldfusion example and keep it dead simple.  If you ever have to concatenate strings in code, user ArrayAppend instead.  Here are two loops that do the same thing.  If you run this code, you should notice loop1 takes forever, and loop2 is smoking fast.

slow…..

[coldfusion]
<cfscript>
xx = 100000;
insertString = "";

// do the loop
while( xx > 0 ) {
insertString &= xx & " ";
xx–;
}

WriteOutput( insertString);
</cfscript>
[/coldfusion]

FAST!

[coldfusion]
<cfscript>
xx = 1000000;
insertArray = ArrayNew(1);
// do the loop
while( xx > 0 ) {
ArrayAppend( insertArray, xx & " " );
xx–;
}

WriteOutput( ArrayToList( insertArray, " " ) );
ArrayClear( insertArray );
</cfscript>
[/coldfusion]

4. If CSV, then CHR

This one is simple, if you find yourself creating CSV or any other text file, use special characters when dealing with single and double quotes, etc.

  • chr(9) = Tab
  • chr(34) = ” double quote
  • chr(39) = ‘ single quote

And if you’re not sure of the correct code for the character you’re looking to use, just wrap that character in ASC() and WriteOutput to the page.

5. Use CFMail with GMail

This is a no brainer, but with how difficult sending email can be with other languages, I’m mentioning it here.

Application.cfc

[coldfusion]
<cfscript>
APPLICATION.mail.server = "smtp.gmail.com";
APPLICATION.mail.port = "465";
APPLICATION.mail.ssl = true;
APPLICATION.mail.user = "gmailAccount";
APPLICATION.mail.pswd = "gmailPasssword";
</cfscript>
[/coldfusion]

Emailer.cfm

[coldfusion]
<cfmail to="work@ericfickes.com"
bcc=""
from="web@master.com"
subject="sending mail is easy with Coldfusion"
server="#application.mail.server#"
useSSL="#application.mail.ssl#"
port="#application.mail.port#"
username="#application.mail.user#"
password="#application.mail.pswd#">
#emailBody#
</cfmail>
[/coldfusion]

6. CFSCRIPT doesn’t know NULL?

Another tip from the land of importing and exporting data.  While working with query objects in CFScript, for some reason I could never accurately detect for NULL values.  I tried all sorts of detection schemes and ended up just writing a hacky fail safe.  Please, if you have a better suggestion for *easy* NULL detection in CFSCript, add it in the comments below.

utils.cfc

[coldfusion]
<!— Simple value getter with try / catch to get around NULL values
This function originated in a script where we always needed a " " even if
the value from the database was null.
—>
<cffunction name="qryGetString" access="public" returntype="string">

<cfargument name="data" type="string">

<cftry>
<cfscript>
return #data# & " ";
</cfscript>

<cfcatch type="Any">
<cfreturn " "/>
</cfcatch>
</cftry>

</cffunction>
[/coldfusion]

Exporter.cfm

[coldfusion]
// largeish loop
tab = chr(9);
for( xx = 1; xx <= queryObj.RecordCount; xx++ )
{
// start the row
this_row = utils_cfc.qryGetString( queryObj.FirstName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MiddleName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.LastName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.Suffix[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MedicalTitle[xx] ) & tab &
utils_cfc.qryGetString( queryObj.email[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_phone[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_fax[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_name[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address1[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address2[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_city[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_state[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_zipcode[xx] ) & tab &
utils_cfc.qryGetString( queryObj.hospital_affiliation[xx] ) & tab;

// do stuff with the data
}
[/coldfusion]

7. How I find list items

Ever notice the different behavior in the Coldfusion ListFind commands?  I ended up writing my own ListHasValue function in order to find exact pattern matching in a list.  I had a list of role ids in a list, and just couldn’t get the built in functions to tell me when my id was in the list without also matching on other ids.  This one makes sense when you run some code.

The top of this sample as my custom ListHasValue() command, and the lower half does three simple loops counting from 1 to 100, and using ListFind, ListContains, and ListHasValue for number checking against the same list.

[coldfusion]
<!—
Use this to do an exact pattern check for a value in a list.
This is useful inside of a loop checking numbers against a number list.

EX :

list = "91, 92"

if you loop from 1 – 100, ListFind and related CF functions will match
on 1, 2, and 9. Not just 91 and 92

Use this when you’re looping and you ONLY want to match on 91, or 92

—>
<cffunction name="ListHasValue" access="public" returntype="boolean">

<cfargument name="list" required="yes" type="string">
<cfargument name="value" required="yes" type="any">

<cfscript>
// clean up to be safe
list = trim( toString( list ) );

// check to see if we have a *possible* match
position = ListContains( list, value ) ;

if( position > 0 )
{
// NOTE : KEEP THE TRIM AND TOSTRING
found_value = trim( toString( ListGetAt( list, position ) ) );

if( Compare( value, found_value ) == 0 )
{
return true;
}
}

// no match for you!
return false;
</cfscript>

</cffunction>

<cfscript>
list = "1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99";
xx = 1;

WriteOutput( "List = " & list & "<hr>");

while( xx < 100 ) {
if( ListContains( list, xx ) > 0 )
{
WriteOutput( "ListContains found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// ListFind
if( ListFind( list, xx ) > 0 )
{
WriteOutput( "ListFind found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// Eric’s ListHasValue
if( ListHasValue( list, xx ) )
{
WriteOutput( "ListHasValue found " & xx & "<br>" );
}
xx++;
}
</cfscript>
[/coldfusion]

If you run this code on your Coldfusion server, you should notice the following results.  ListContains matches single digits from the loop that do not really exist in the list.  ListFind only finds the number 1?  And finally, my function does exactly what I needed it to do.  Tell me when a specific number exists in a list.

CF ListFind function comparison

Coldfusion ListFind functions don't always behave how I want them to


8. Make PDFs faster

This could easily be it’s own topic, but I’ll say one thing about making PDFs faster with CFDocument.  Only put final content between <cfdocument> and </cfdocument>.  That is, if you have any processing code, cfqueries, cfloops, inside of your cfdocument tag, your cfml page is running slower than it needs to be.  Here’s a simple example of one of my cfml pages that has only final content in the cfdocument tags.

The key to this example is moving all of my content creation code into an external file, then including at the top of my page. I always do a check for my main PDF_BODY variable, and then spit out my PDF document.

[coldfusion]
<cfinclude template="code/export_pdf_codefile.cfm">

<cfif PDF_BODY NEQ "">

<cfdocument name="provider_profile"
format="PDF"
pagetype="A4"
mimetype="application/pdf"
orientation="portrait"
margintop="0"
marginbottom="0.2"
marginleft="0.2"
marginright="0.2"
>
<cfoutput>#PDF_BODY#</cfoutput>
</cfdocument>

<!— send directly to client —>
<cfheader name="Content-Disposition" value="attachment; filename=#filename#">
<cfcontent type="application/pdf" variable="#provider_profile#">

<cfelse>
No PDF content found
</cfif>
[/coldfusion]

9. Use parameterized queries

This is a tip for all server side programmers whether you use Coldfusion, ASP, JSP, PBJ.  Use parameterized queries when doing any database interaction.  It’s too easy not to use, and you get protection from SQL Injection, as well as enforcing proper data types when speaking to your database.  This is something all server programmers should do regardless of your language, the sample below is for Coldfusion.

BAD

[coldfusion]
<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
( ‘#Form.field1#’, ‘#Form.field2#’, ‘#Form.field3#’ )
</cfquery>
[/coldfusion]

GOOD

[coldfusion]
<cfset val1 = Form.field1>
<cfset val2 = Form.field1>
<cfset val3 = Form.field1>

<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
(
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val1#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val2#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val3#" />
)
</cfquery>
[/coldfusion]

10. Where’d the time go?

For the good programmers already using parameterized queries, ever insert a timestamp into your database and find out the date is correct, but the time is always 12:00:00?  Take a closer look at the cfsqltype in your cfqueryparam, I had this exact problem and here’s what happened.

Using cf_sql_date does not include the full date and timestamp, just the date.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_date" value="#paymentDate#" />
[/coldfusion]

 

Using cf_sql_timestamp includes the full date and timestamp I was looking for.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#paymentDate#" />
[/coldfusion]

11. Stored Procedures are a little different

This last one isn’t much of a tip, but more of a reminder to myself.  I do so much database work that stored procedures are just queries to me, but not so to Coldfusion and the CFQuery tag.  If you want to get data from a stored procedure, you need to use the CFStoredproc tag.  Here’s a sample of passing one argument into a stored procedure, and how to get the resulting data.

[coldfusion]
<cfstoredproc datasource="myDb" procedure="GetDataFaster">

<cfprocparam type="in" cfsqltype="cf_sql_integer" value="#inputVar#" />

<!— specify sproc result here, cfstoredproc res != returned recordset —>
<cfprocresult name = sprocResult>

</cfstoredproc>

<cfreturn #sprocResult.ColumnFromQuery#>
[/coldfusion]

Injecting javascript into asp.net via code

Microsoft has a great MSDN article on using javascript along asp.net, but they didn’t mention a technique I like to use, put it in a Literal control.  While there are many ways to add javascript to a page, I find putting the javascript in a literal much less stressful. Using a Literal control placeholder is also a good way to add messaging to a page after postback, but we’re just going to look at adding javascript.

Let’s take a simple example.  Say you’ve got a comment form that you want to auto close, or reload after the form was posted.  Below is a simple single file style asp.net page with a simple javascript function that reloads this page.

<%@ Page Language="C#" %>

<script runat="server">
/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

    }
}

/////////////////////////////////////////////////////////////////////////////// 
/// Do stuff with the form data, then refresh page using javascript
protected void submitComments(object sender, EventArgs e)
{

    try
    {
	//
	// do stuff here
	//

	// set javascript timer to reload page afer 3 seconds
	js_target.Text = "setTimeout('reload()', 3000);";

    }
    catch (Exception exc)
    {
        Response.Write( "ERROR : " + exc.Message );
    }
}
</script>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
	<title>Comments</title>
	<script type="text/javascript">
	// page reload helper
	function reload() {
		document.location.replace( document.location );
	}
	</script>
</head>
<body>

<form id="form1" runat="server" method="post">

<script type="text/javascript"><asp:Literal runat="server" id="js_target" /></script>

	Comments
	<asp:TextBox runat="server" ID="comment_box" Width="200" />
	<br><br>

	Your name
	<asp:TextBox runat="server" ID="fullname" Width="200" />
	<br><br>
	<asp:Button runat="server" ID="submit_btn" onclick="submitComment" Text="submit" />

</form>
</body>
</html>

If you look just under the form tag you’ll see the key to this technique, an asp literal wrapped by an open and close script tag.

<script type="text/javascript"><asp:Literal runat="server" id="js_target" /></script>

When you load your page and view the source you’ll just see an empty script tag, so it shouldn’t interfere with the execution or rendering of your page.

The last part of this technique is simple, in your server code just set your Literal control’s .Text value to your javascript code. In this case when I post my comment form, after handling the input data I display a thank you message, then set some javascript to reload the page.

ltl_js.Text = "setTimeout('reload()', 3000);";

That’s all there is to it. Drop a literal in an empty script block and BAM!, you have an easy way to add javascript to your asp.net page.