Finding StoreGeneratedPattern value in EdmProperty in T4 Template
If you need to find out if a primary key in your conceptual model which is using the Identity value from the property StoreGeneratedPattern, you can use the following code: (NOTE: This is used within a T4 template) string inputFile = @"..\EntitiesModel.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string annotationNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/annotation"; foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e [...]
Determing SQL Server Table Size
This is a bit of code I found that determines the size used by each table in your database. Make sure you run DBCC UPDATEUSAGE first to correct any incorrect stats (pages etc) on your tables. DBCC UPDATEUSAGE (YOUR DATABASE NAME) DECLARE @TableName VARCHAR(100) –For storing values in the cursor –Cursor to get the [...]
You receive a “The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect” exception when using NVarchar parameters with Sqlclient
The problem I got the following random error: You receive a “The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect” exception when using NVarchar parameters with Sqlclient The Solution Its a known bug with the .Net SqlClient Data Provider. If you have a field in the database of type nvarchar(max) [...]
Clean your SQL Server database!
Here are some lines of code that can help clean your database from test data so you can start over with a fresh canvas. /* Disable constraints and triggers (if any) */ exec sp_MSforeachtable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’ exec sp_MSforeachtable ‘ALTER TABLE ? DISABLE TRIGGER ALL’ /*Perform delete operation on all table for [...]
Finding the Length of an Image Field in SQL Server
Problem: I need to know the size in bytes of an image stored in an Image field in SQL Server Solution: SELECT DATALENGTH(column) FROM table
Data in SSRS Report Header
Here is a tip for displaying data from data-sets into the Page Header/Footer of a SSRS report. Traditional the hacky way of performing such tasks is to have a hidden text-box in the body of the report with the respective data-set field value. You can then get the value of this text-box in your page [...]
Split Function for T-SQL using XML
Here is some sample code to split a string and return the results via a table. CREATE FUNCTION [dbo].[split] ( @del char(1), @str varchar(max) ) RETURNS @tResult TABLE ( val varchar(max) ) AS BEGIN declare @xml xml set @xml = N’<root><r>’ + replace(@str,@del,’</r><r>’) + ‘</r></root>’ insert into @tResult(val) select r.value(‘.’,’varchar(max)’) as item from @xml.nodes(‘//root/r’) as [...]
The specified ‘@subsystem’ is invalid
If you get the following error: Create maintenance plan failed. The specified ‘@subsystem’ is invalid (valid values are returned by sp_enum_sqlagent_subsystems). (Microsoft SQL Server, Error 14234) Just install Integration Services (just a little job…) and reboot. References: http://support.microsoft.com/kb/909036/en-us
Selecting a row position within a SELECT query
I want to get the third row in a result set from the following: ID Name 1 A 2 B 3 C 4 D You can acheive this by using the ROW_NUMBER() rank function using SQL Server 2005/2008. The following code will return the 3rd record. create table #temp ( ID int, [Name] nvarchar(50) ) [...]
SQL Server Express 2008 Maintenance Script
The issue I have with SQL Server Express Edition 2008 is that the SQL Agent is disabled and according to Microsoft, this has been disabled on purpose. So, how do we schedule jobs to do such tasks as Rebuilding Indexes, Full-text Catalogs etc? I use the Task Scheduler on Windows 2008 Server that executes a [...]


