Archive

Posts Tagged ‘feature’

T-SQL: Add column with default values to a table

January 5th, 2010 Yuriy No comments

Let's say you need to add a column to an existing table that already contains some rows. And this new column needs to be prepopulated with a default value. For example integer nullable column NEW_COLUMN is added to table MY_TABLE. Common approach is to check for column existence, and if it doesn't exist - create and populate it:

if not (exists(SELECT * FROM SYSCOLUMNS WHERE ID = OBJECT_ID('[dbo].[MY_TABLE]') AND Name = 'NEW_COLUMN'))
BEGIN
	ALTER TABLE [dbo].[MY_TABLE] ADD [NEW_COLUMN] [int] NULL
	UPDATE [dbo].[MY_TABLE] SET [NEW_COLUMN] = 1
END

But this doesn't work - you get "Invalid column name" error. The trick is - you don't need a separate UPDATE statement. ALTER TABLE ... ADD statement has a DEFAULT parameter. You can modify previous code like this:

if not (exists(SELECT * FROM SYSCOLUMNS WHERE ID = OBJECT_ID('[dbo].[MY_TABLE]') AND Name = 'NEW_COLUMN'))
BEGIN
	ALTER TABLE [dbo].[MY_TABLE] ADD [NEW_COLUMN] [int] NULL DEFAULT 1 WITH VALUES
END

This ALTER statement assigns default value of 1 to the new column. WITH VALUES parameter is important, it populates existing rows with the default value (without it it will be populated by NULLs).

Categories: SQL Tags: , ,

Resizing IE Modal Dialog in JavaScript

December 3rd, 2009 Yuriy No comments

If you open an IE Modal Dialog window and later on need to resize it using JavaScript code, standard "window.resizeTo" method doesn't work. Use dialogWidth and dialogHeight instead. For example:

window.dialogWidth='640px';
window.dialogHeight='480px';

UltraWebGrid in OutlookGroupBy Mode: How to display custom aggregates (SUM, AVG, MIN, MAX) in GroupBy row

November 19th, 2009 Yuriy No comments

Last year I described a way to display accurate row count when grid in OutlookGroupBy mode has multiple sub-groups.

That solution requires custom counting function. Fortunately to display aggregates such as SUM, AVG, MIN and MAX UltraWebGrid has a built-in functionality in form of GroupByRowDescriptionMaskDefault property of DisplayLayout. It specifies the string to be displayed in every GroupBy row and can include following substitutes:

  • [caption] - will display GroupBy column header text
  • [value] - will display common to the group cell value
  • [count] - will display row count (does not work correctly with sub-groups)
  • [avg], [sum], [min], [max] - will display the aggregate for the column it's grouped by
  • [avg:ColumnKey], [sum:ColumnKey], [min:ColumnKey], [max:ColumnKey] - will display the aggregate for any other column, where ColumnKey is the key of the column

With this in mind in just a few simple steps we can make UltraWebGrid to display something like this:


Read more...

Session crossover in IE8

November 6th, 2009 Yuriy No comments

In IE6/7 if you open 2 independent browsers (not using "New Window" or "New Tab") they do not share common session and you can login to, let's say, a web mail account under 2 different users at the same time.
This is not the case with IE8. Using 2 browser (and seeing 2 "iexplore.exe" processes in task manager) if you try to login as 2 users - the second will take over the first. I guess it's a new "feature" of the browser.
But IE8 gives you a way out, albeit not obvious one. In menu, you can select File -> New Session. This will open a new browser window with independent session.

Categories: New Stuff, Rant Tags: , , ,