... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2011-12-17

jTDS vs NVARCHAR(MAX)

jTDS truncates VARCHAR and NVARCHAR stored procedure output parameter values that are longer than 4000 characters for NVARCHAR, 8000 for VARCHAR. This is a consequence of the fact that jTDS is still using the old SQL Server 2000 version of the TDS protocol. The jTDS developers are aware of this problem, but the comments in the bug report suggest that it may be a while before the problem is corrected. In the meantime, the work-around is to replace the output parameter with a single row, single column result set. If one is reluctant (or unable) to change the stored procedure, then the following sequence will work as well:

DECLARE @result NVARCHAR(MAX)
EXEC dbo.myproc(..., @result OUTPUT)
SELECT @result

2011-11-16

Iterating Over Tables/Databases in SQL Server

SQL Server provides an undocumented stored procedure that is handy for applying commands to all of the tables within a catalog, sp_MSforeachtable:

sp_MSforeachtable 'print ''?'''

The question mark will be replaced by the table name. In a similar vein, we can iterate over all databases using sp_MSforeachdb. These procedures could disappear at any time, but they have been present in all versions from 7.0 to 2008R2 so far.

Both procedures accept the following arguments:

  • @command1 - the command to run for each table
  • @replacechar - the placeholder character for the table name (default '?')
  • @command2 - another command to execute for each table
  • @command3 - another command to execute for each table
  • @precommand - a command to execute before iterating
  • @postcommand - a command to execute after iterating

2011-08-19

Lambdas In Java

With Oracle's takeover of Java, interest in functional programming appears to be renewed. As the Java language evolves in this direction, perhaps it needs a new name. How about one of these names?

  • Visual J++
  • C#
  • !C#
  • C##
  • OpenC#Beans

:)

2011-04-08

git am vs. Windows

Using git on Windows you may find that if you create a patch using git format-patch and try to apply it using git am, the patch file will fail to apply. The error message is "patch does not apply". If you inspect the patch file, you might be baffled because the patch does apply, that is, the context lines in the diff exactly match (byte for byte) the file being patched.

The problem is that git am invokes git mailsplit to split the patch file into individual patches -- and the latter component strips carriage-return characters by default. Consequently, the patch context fails to match the target files and the patch fails.

If you use git am --ignore-space-change to apply the patch, then it will succeed but any newly added lines will not conform to the Windows newline convention. The lines will end with a linefeed only.

A better solution is to use git am --keep-cr. This arranges to pass the --keep-cr option to git mailsplit, leaving the Windows-style line-endings in place. The patch will then be applied successfully. You can make this behaviour the default through a configuration variable in the am section of the git configuration file:

[am]
        keep-cr = true

2011-03-01

View dangling GIT commits in GITK

You can use GITK to view commits that have no external references by using this command:

gitk --all $(git log -g --pretty=format:%h)

2011-01-23

Cylon Programming Principle

Introducing the Cylon Programming Principle: "In well-designed software, there is only one god." (And his name is Singleton?)

2011-01-11

Eclipse Forms vs. Context Menus

If you are using Eclipse's form toolkit infrastructure, you might run into an problem where you try to install a context menu but it does not appear. If you are using FormToolkit.adapt(Composite) to adapt a custom composite to a form, you must install your menu after adapting it. Otherwise, your menu will be replaced by the parent's menu. This may seem to be reasonable behaviour -- except when the parent widget has no menu at all.

2011-01-08

XML Tricks in SQL Server 2005

Here are some stupid pet XML tricks in SQL Server 2005...

To XML-escape text stored in a column:

SELECT CAST('' AS XML).query('sql:column("v.v")')
FROM (SELECT '<>''"&' AS v) AS v

To XML-escape text stored in a variable:

DECLARE @myText AS NVARCHAR(MAX)
SELECT @myText = '<>''"&'
SELECT CAST('' AS XML).query('sql:variable("@myText")')

To extract individual nodes from XML:

SELECT result.node.query('.')
FROM (SELECT CAST('<x><a/><b/><c/></x>' AS XML) AS x) AS x
CROSS APPLY x.x.nodes('/x/*') AS result(node)

Use XML to perform concatenation aggregation:

;WITH
  strings AS (SELECT 'a' AS s UNION SELECT 'b' UNION SELECT 'c')
SELECT
  (SELECT s AS 'text()' FROM strings FOR XML PATH(''))

Generate nested XML, avoiding FOR XML EXPLICIT:

SELECT
  1 AS "@id"
, 2 AS "a/b/c"
, (SELECT v AS "e"
   FROM (SELECT 1 AS v UNION SELECT 2) AS v
   FOR XML PATH('d'), TYPE )
, 3 AS "text()"
, 4 AS "data()"
FOR XML PATH('element'), ROOT('root')

Use XML namespaces:

;WITH XMLNAMESPACES
( DEFAULT 'urn:default'
, 'urn:a' AS "a"
)
SELECT 1 AS 'a:y' FOR XML PATH('x')

Blog Archive