Tuesday, June 16, 2009

Using CTE (Common Table Expression)

To create a collection of rows without querying a table or a repeating series of INSERT statements.

declare @seed int

select @seed = 8;

WITH RecurseTable(Id)
AS
(
SELECT @seed

UNION ALL

SELECT R.Id -1
FROM RecurseTable R
WHERE R.Id > 1
)

select *
from RecurseTable

Friday, April 24, 2009

Querying Table Definitions

A query to return metadata about a table. This is useful for dynamic query building like in a SqlAdapter, for example.



SELECT C.COLUMN_NAME,
C.DATA_TYPE,
C.CHARACTER_MAXIMUM_LENGTH,
CASE WHEN PK.CONSTRAINT_NAME IS NULL THEN 0 ELSE 1 END IS_KEY,
CASE WHEN IC.object_id IS NULL THEN 0 ELSE 1 END IS_IDENTITY
FROM INFORMATION_SCHEMA.COLUMNS C
LEFT OUTER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE PK ON C.TABLE_NAME = PK.TABLE_NAME
AND C.COLUMN_NAME = PK.COLUMN_NAME
INNER JOIN
sys.tables T ON C.TABLE_NAME = T.name
AND T.[type] = 'U'
LEFT OUTER JOIN
sys.identity_columns IC ON T.object_id = IC.object_id
AND IC.[name] = C.COLUMN_NAME
WHERE C.TABLE_NAME = @TableName

Monday, November 10, 2008

ActionScript for .NET Programmers

NOTE: These notes are based on my development efforts with Flash. If there are any incorrect statements, please let me know in the comments. Thank you.

DEBUGGER

The debugger is not tightly integrated into the IDE. It is a separate tool that you must invoke by selecting Control > Debug.

Once invoked, the project is in a paused state. Select Continue to debug.

Breakpoints are only allowed in the Actions window of the .fla file. You cannot set breakpoints in classes you've created.

The trace() method will become your best friend as the watches do not evaluate complex values like object.items[i]. Also, there is no Immediate window for interacting with the code.

CLASSES

ActionScript 2.0 supports classes in the traditional sense as objects with private and public members, inheritance and defined members. Earlier versions of ActionScript also support malleable objects similar to those defined in JavaScript. Those objects can have properties and methods stuck onto them at run time.

MovieClips are the base object similar to WinForms. Flash supports a form-based model of programming using components derived from MovieClips. This component of this type: mx.screens.Form.

Think of a form as a one frame movie clip.

Note that the forms appear to be inaccessible from code stored in class modules (.as) files. I have found that passing in the form reference to a private member gets the trick done.

PROGRAMMING MODEL

All server interactions are handled asynchronously using callbacks similar to Delegates.

XML

The XML object in ActionScript 2.0 is far less rich than the XMLDOM COM object or the .NET XmlDocument objects.

There is no select nodes or XPATH support for the nodes. Rather, every element, including a node's text is considered a node.

Iteration is done through arrays generated by the childNodes property.

If a node is XML, its nodeType property is 1, and the available propery is nodeName. If the nodeType property is 3, then it is a text node and the nodeValue property is used. Note that the nodeValue is not available to nodes of type XML.

Monday, October 6, 2008

Efficiently Rounding a SQL Server datetime

Truncates hours, minutes and seconds without messy string manipulation:

CAST(FLOOR(CAST(GETDATE() AS DECIMAL(8,2))) AS datetime)

Wednesday, September 10, 2008

Underdocumented SQL Server mail parameter

Due to popular demand, Microsoft added the @query_result_no_padding to the sp_send_dbmail stored procedure.

Sadly, you wont find this in the online help. It was added as part of a Service Pack.

Use this parameter to trim excess spaces from the query results attached to the email. Otherwise, the column output pads out to the length of the char OR varchar field.

For example:
declare @t TABLE (field varchar(10))

insert into @t
select 'hello'

When sent with @query_result_no_padding = 1
the result is:


field
-----
hello


otherwise


field
----------
hello

Friday, June 6, 2008

Decode HTML Strings in SQL Server

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- EXEC UrlDecode '%c3%a1'
-- EXEC UrlDecode '\\Tul1ciprmw1\RichMediaAssets\Open\Telemundo\Al Rojo Vivo con Mar%c3%ada Celeste\_derived_jpg_q90_172x128_m1\3ARValx05Mar%c3%ada Celeste Arrar%c3%a1s.jpg'
-- EXEC UrlDecode '%4D'

ALTER PROCEDURE UrlDecode
-- Add the parameters for the stored procedure here
@URL varchar(2000)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @Position INT,
@Base CHAR(16),
@High TINYINT,
@Low TINYINT,
@NewHigh INT,
@Pattern CHAR(21)

SELECT @Base = '0123456789abcdef',
@Pattern = '%[%][0-9a-f][0-9a-f]%',
@Position = PATINDEX(@Pattern, @URL)

SELECT @High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base) -1,
@Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base) -1

WHILE @Position > 0
BEGIN

IF (@High & 15) = 12 -- xC0
BEGIN
SELECT @NewHigh = @Low * POWER(2, 6) --Shift Low 6 bits

SELECT @High = CHARINDEX(SUBSTRING(@URL, @Position + 4, 1), @Base) -1
SELECT @Low = CHARINDEX(SUBSTRING(@URL, @Position + 5, 1), @Base) -1

SELECT @URL = STUFF(@URL, @Position, 6, CHAR(@NewHigh | (16 * @High) | @Low))

SELECT @High = 0, @Low = 0, @NewHigh = 0

END
ELSE
BEGIN
SELECT @URL = STUFF(@URL, @Position, 3, CHAR(16 * @High | @Low))

END

SELECT @Position = PATINDEX(@Pattern, @URL)
IF @Position > 0
BEGIN
SELECT @High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base) -1,
@Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base) -1
END
END

SELECT @URL

END
GO

Wednesday, July 25, 2007

Test Window

<root>
<users>
<user>Joe</user>
<user>Willie</user>
</users>
</root>





Results Here