Sloan's profileGranada Coder, C#, Sql S...PhotosBlogLists Tools Help

Sloan Holliday

Occupation
Interests
Photo 1 of 2

Granada Coder, C#, Sql Server Blog

5/6/2009

One True Lookup Table Discussion

 
I recently saw an article on the "One True Lookup Table".
 
 
Code Sample Here (Right-click, Save As works best)
 
The author makes 2 points.
//
In most cases, neither of the above applies, and responsibility for data integrity resides solely in the application code.
Show me such a database, and I’ll show you some data where the code does not match anything in the lookup table.
//
 
Well, I can provide a solution that uses a UDF (user defined function) and a check constraint.
And the statement "and I’ll show you some data where the code does not match anything in the lookup table" would not be valid if this solution is employed.
The FK exists to keep integrity.<<(This exists in addtion to the constraint)
And the check constraint makes sure that you only can apply values of the correct "CodeCategory".
(Aka, I cannot put in a OrderStatus value into a column wanting a Country or similar).
 
The solution is Sql Server specific, but the idea is there.
 
Keep in mind I use this for ~most tables.  I still create a few standalone lookup tables when the cookie cutter does not work.
 
I am not saying this is the best solution in every scenario.  There is a performance issue with validating the check constraint for every INSERT/UPDATE action.
But for bulk data inserts (or updates), you can drop the constraint and readd it if that becomes an issue.
 
1/27/2009

Bulk Insert Example, using an IDataReader To Strong DataSet to Sql Server XML

Here is a sample I wrote that someone requested I post.
 
Right click HERE and do a "Save As" or "Save Target As".
 
 
The sample gets an IDataReader on a Source Datastore (Access/Jet database), populates a strong dataset, then after so many records, ships the DataSet.GetXml() to a sql server stored procedure.
This should run faster than doing a row by row get/insert that alot of people code up.
 
Yes, there are many ways to do bulk inserts/imports with Sql Server.  I chose this one because in my real life code, I need to run the rows through some business rules before importing. 
 
Enjoy.
 
12/10/2008

SqlServer2005/2008 // OUTPUT clause in INSERT/UPDATE/DELETE statements

SqlServer2005/2008 // OUTPUT clause in INSERT/UPDATE/DELETE statements

These types of samples are all over the place on the web, but here is my original example for which I believe is better clarity.

Original Example(s) at:
http://blogs.msdn.com/sqltips/archive/2005/06/13/OUTPUT-clause.aspx

create table PrimaryHolderTable ( i int identity (1001,2) not null primary key, j int not null unique )
create table #OutputResultsHolder ( i int not null, j int not null)
 
insert into PrimaryHolderTable (j)
output inserted.i, inserted.j into #OutputResultsHolder
select top 10 o.object_id from sys.objects as o order by o.object_id desc --<< from sys.objects is there just to provide some rows
 

select * from #OutputResultsHolder
drop table #OutputResultsHolder, PrimaryHolderTable;

go
 
 

create table dbo.EmployeeTable ( EmpKey int identity(1001,2) ,  EmpAge int not null );
create table dbo.AuditTable ( EntityKey int not null default -1  ,  OldValue int null, NewValue int null , Tag varchar(64)  );
 
insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , 'Employee Inserted' into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 values( 18 );

insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , 'Employee Inserted' into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 values( 20 );

insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , 'Employee Inserted' into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 values( 22 );
 
 
update dbo.EmployeeTable
   set EmpAge  = EmpAge + 1
output inserted.EmpKey , deleted.EmpAge, inserted.EmpAge , 'Employee Updated' into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 where EmpAge <=20;
 
delete from dbo.EmployeeTable
output deleted.EmpKey , deleted.EmpAge, NULL , 'Employee Deleted'  into dbo.AuditTable (EntityKey , OldValue , NewValue , Tag)
 where EmpAge > 0;--Test multi rows
 
select * from dbo.EmployeeTable;--<<will be empty at this point
select * from dbo.AuditTable;
 
drop table dbo.EmployeeTable, dbo.AuditTable;
go
 

7/24/2008

Cursors, SetBased, and Scalar UDF

This is an oldie but a goodie.

If you're doing CURSOR development for Sql Server to solve business problems, then STOP.

DOWNLOAD this file (right-click and save-as works best) for an example of non cursor based logic solving.

03 and 04 solve the same business problem, but 2 different ways. 

03 is the cursor model.  04 is the udf model.

Read the comments, and try to make a decent sized database (row count) so you see the massive and significant time difference.

 

6/13/2008

Software Quote

Usually I don't post quotes, but this one has gotten enough positive feedback, I guess I'll post it.
 
The conversation context:  A VB6 developer moving to C# and posed the question "Where is the Resume Next in C#?".
 
Here was my reply:
 
My VB.NET suggestions:

Exception Handling?  (formally know as ErrorHandling)

Take the vb6 mentality of exception handling and do the 4 following things:

1.  Put it in a brown paper bag.
2.  Take it to someone's house you don't like.
3.  Set it on fire.
4.  Run away from it as fast as you can.

Check here:
http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx
(A great post by Krzysztof Cwalina on exceptional handling)

I was able to attend Krzysztof's presentation on Reusable Frameworks at TechEd2008, and he mentioned a future version (V2) of his book Framework Design Guidelines.  Sweet!
3/4/2008

Multiple RDBMS Support and the Factory Design Pattern

 
August, 2008 Update.
I added in SqlServerCE support (version 3.1 for desktop).
Now there is SqlServer, Access, Excel, Text and SqlServerCE.
SqlServerCE was a little tricky at first, but I got it running.
 
 
This is an early preview, and I don't have alot of comments or explanation yet.
  
Download the code HERE (Right Click / Save As works best)
One of my first real jobs my company had to support multiple RDBMS (Relational Database Management Systems).
Sql Server 6.5, 7.0, Sybase, and Access/Jet (Although I don't consider Access/Jet a real RDBMS).
 
Oh, the countless number of "IF gDataBaseType="Access" then" statements.  YUCK!
 
I had been advising people to use the Factory Pattern for multiple RDBMS support, but finally decided to author a demo.
 
You'll need Sql Server 2000,2005 or 2008.  You don't have to have those, but it helps.
 
 
Most important instruction.  Read the README.txt file before unzipping.
Again, read the README.txt file BEFORE unzipping.
 
I'll followup with this at some point, but wanted to get the sneak preview out the door.
 
..
 
 

11/29/2007

MembershipProvider Helper To Transfer Data

If you're using the MembershipProvider (actually the concrete SqlMembershipProvider), then you may have run into an issue where you develop all the Users and Roles on your local machine, using the asp_net.mdf in the Express version of Sql Server 2005.

All of the sudden, you need to deploy your application and you need a different database to store your information.
You'll probably find this resource for deploying on a different version of Sql Server
http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx

Ok.  You run the aspnet_regsql.exe utility, and you have the correct database, but it is empty.

What do you do now?

I've created this helper script to enable you to move all your applications/users/roles to a new database.
This script doesn't actually INSERT the new data, but creates the TSQL code that you can run on the new database.

I've posted the code here (as a text file).

Hopefully, it will make your life a little easier when you need to copy to a new database all the Membership information.

 
There are no music lists on this space.