Thursday, November 12, 2009

Browser Programming

Using the new programming language Go http://golang.org/ from Google, it will be possible to run Go programs directly in browsers like Chrome. http://news.cnet.com/8301-30685_3-10395355-264.html?part=rss&subj=news&tag=2547-1_3-0-5

The question is how will web developers adopt this new approach?

Monday, November 2, 2009

Script to Enable/Disable All Foreign Keys

To generate SQL statements which will disable all foreign key constraints in a database, you can use this script

select 'ALTER TABLE [' + s.name + '].[' + t.name + '] NOCHECK CONSTRAINT [' + fk.name +'];'
as script
from sys.foreign_keys fk inner join sys.tables t on fk.parent_object_id = t.object_id
inner join sys.schemas s on t.schema_id = s.schema_id

To enable foreign key the NOCKECK keyword needs to be changed to CHECK

If data was inserted when tables foreign keys were disabled and would like to check integrity,
DBCC CHECKCONSTRAINTS('TABLE_NAME') [WITH ALL_CONSTRAINTS] can be used
the optional WITH ALL_CONSTRAINT is used to include all constraints even the ones that are disabled even the ones

Here is a capture script from sql profile to see all foreign keys and their columns

SELECT
SCHEMA_NAME(tbl.schema_id) AS [Table_Schema],
tbl.name AS [Table_Name],
cstr.name AS [ForeignKey_Name],
fk.constraint_column_id AS [ID],
cfk.name AS [Name],
crk.name AS [ReferencedColumn]
FROM
sys.tables AS tbl
INNER JOIN sys.foreign_keys AS cstr ON cstr.parent_object_id=tbl.object_id
INNER JOIN sys.foreign_key_columns AS fk ON fk.constraint_object_id=cstr.object_id
INNER JOIN sys.columns AS cfk ON fk.parent_column_id = cfk.column_id and fk.parent_object_id = cfk.object_id
INNER JOIN sys.columns AS crk ON fk.referenced_column_id = crk.column_id and fk.referenced_object_id = crk.object_id
ORDER BY
[Table_Schema] ASC,[Table_Name] ASC,[ForeignKey_Name] ASC,[ID] ASC

Friday, October 2, 2009

Shrink Databases using sp_MSForEachDB

Using undocumented stored procedure sp_MSForEachDB and DBCC SHRINKDATABASE command you can shrink all databases on an SQL Server in one line of code

exec sp_MSForEachDB 'if ''?'' not in (select name from sys.databases where is_read_only = 1) DBCC SHRINKDATABASE([?]) '

You may need to add additional checks [skip offline dbs for example]. Test on SQL Server 2005.

Wednesday, May 20, 2009

T-SQL Script to Generate SHRINKFILE statements for all DBs

This a t-sql script I use to generate DBCC SHRINKFILE statements for all databases on a SQL Server 2005/2008. Microsoft recommends that you change the recovery model to simple shrink the file and change back to full if the recovery model is full to begin with. You cannot shrink readonly dbs (or offile dbs obviously)

The script is provided AS IS.

use master;
GO
create procedure [dbo].[udp_genShrinkStatements] as begin
declare @sql nvarchar(max);
declare @filename varchar(256);
declare @dbid int;
declare @dbname varchar(256);
declare @recoveryModel int; -- 3 simple; 1 full
declare dbcursor cursor for SELECT     database_id,[name], recovery_model --, recovery_model_desc
FROM         sys.databases
WHERE     (database_id > 4) AND (is_read_only = 0) and (state = 0) --skip system, readonly and offile dbs
ORDER BY   [name]
open dbcursor;
set @sql = '';
fetch next from dbcursor into @dbid,@dbname,@recoveryModel;
while @@FETCH_STATUS = 0
begin
declare filecursor cursor for SELECT [name] from sys.sysaltfiles where dbid = @dbid;
open filecursor;
select @dbname = '['+@dbname+']';
--select @sql = @sql + @dbname;
-- print '--'+@dbname;
--select @sql = @sql + '--';
-- print 'USE ' + @dbname +';';
select @sql = @sql + 'USE ' + @dbname +';' ;
-- print 'GO';
--select @sql = @sql + ' GO ';
if @recoveryModel = 1
begin
-- print 'ALTER DATABASE '+@dbname +' SET RECOVERY SIMPLE WITH NO_WAIT;';
select @sql = @sql + 'ALTER DATABASE '+@dbname +' SET RECOVERY SIMPLE WITH NO_WAIT;';
end
fetch next from filecursor into @filename
while @@FETCH_STATUS = 0
begin
-- print 'DBCC SHRINKFILE('''+ @filename +''');';
select @sql = @sql + 'DBCC SHRINKFILE('''+ @filename +''');';
fetch next from filecursor into @filename
end
close filecursor
deallocate filecursor
if @recoveryModel = 1
begin
-- print 'ALTER DATABASE '+@dbname +' SET RECOVERY FULL WITH NO_WAIT;';
select @sql = @sql + 'ALTER DATABASE '+@dbname +' SET RECOVERY FULL WITH NO_WAIT;';
end
-- print '---------------';
--select @sql = @sql + '---------------';
fetch next from dbcursor into @dbid,@dbname,@recoveryModel;
end
close dbcursor;
deallocate dbcursor;
--exec sp_executesql @stmt = @sql;
SELECT @sql;
end

Tuesday, March 10, 2009

Report Builder 2.0 & Reporting Services 2008

We're still on reporting services 2005 but I've playing with Report Builder 2.0 in which you can see some of the features of Reporting Services 2008 such as the long-awaited control tablix. Very Nice! you can download it from here [http://www.microsoft.com/downloads/details.aspx?familyid=9f783224-9871-4eea-b1d5-f3140a253db6&displaylang=en] The builder looks really cool and the look-and-feel of Office 2007 and many more improvements of the VS Designer for RS 2005. 

Of of most annoying bugs in 2005 was that when you make a change to a query in the report DataSet+ and save the report using CTRL+S (or click save icon), the DataTime parameters were converted to Strings. But so far I haven't seen this happening in RB 2008.

One way I worked around the bug I mentioned (DateTime converted to Strings) is by building the query after making the changes and before CTRL+S  ( or saving).

Other welcomed additions to Reporting Services 2008 are the better charting capabilities and the Gauge controls.

Tuesday, March 3, 2009

T-SQL: A Set-Oriented Language + Ranking Functions

The other day I had a query that invloved several tables and complex logic to a certain degree. The initial query involved using user-defined functions and the such and it took roughly 12 minutes to run which was not acceptable. After a little bit of tweeking and thinking of sets and not procedural logic I managed to the query to run in under 10 seconds for the same amount of data. Thanks partially to t-sql ranking functions [ http://msdn.microsoft.com/en-us/library/ms189798.aspx#]

Dense_Rank was especially useful. Unlike Rank(), Dense_Rank() does not leaves gap in the sequence of numbers desginating ranks.