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.

Sunday, February 15, 2009

Intersystems Caché Column Mismatch

In one of my applications I deal with Caché, an object-oriented database systems from InterSystems. The application communicates with Caché through an ODBC driver. I added Caché as a linked server to my SQL Server 2005.

One of the queries invovled selecting all columns from a table. So I had no doubt that 

select A,B,C,D,AMT
from TBL 

query would work. Whenever I ran this query in SQL Manage Studio I got this message:

"OLE DB provider 'MSDASQL' for linked server 'LS' returned message 'Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.' Cannot get the current row value of column LS..SCH.TBL.AMT from from OLE DB provider 'MSDASQL' for linked server 'LS' Could not convert the data value due to reasons other than sign mismatch or overflow"

By process of elmination I found out that the culprit is AMT. After some reading through Cache documentations [http://vista.intersystems.com/csp/docbook/DocBook.UI.Page.cls?KEY=GIC_INTRO ; and other places ] I solved this problem. Basically although the ODBC presented the AMT column as Currency or [decimal in the linked server] that did not mean that Cache really stored only decimal in that column. And somehow that column contained alphabet characters which caused the query to fail every time it asked for the AMT column.

And to solve this problem through SQL Server I ran the following query instead

SELECT A,B,C,D,AMT
FROM 
OPENQUERY(LS,'SELECT A,B,C,D,convert(nvarchar(50),AMT) as AMT FROM TBL');

I ran into other problems with other columns and the cause is basically the discrepancy between the way the ODBC driver sees Cache columns and their datatypes and the actual layout of these columns in Cache. And openquery does a good job in helping to solve this problem.



Tuesday, February 10, 2009

Computing Science and Computers History

One of the things I like is computer science history, what was computing like a decade or two ago and was interesting and the such. Here is a talk given by Bill Gates in 1989 hosted by the University of Waterloo Computer Science Club [http://csclub.uwaterloo.ca/media/1989%20Bill%20Gates%20Talk%20on%20Microsoft
I listened to a good portion of the talk a while back and from a history persective I liked it specially the talk on OS/2.


Monday, January 26, 2009

Using Report Services 2005 Web Service

EDIT: Your problem may be easily solved if you read this first http://alsaydi.blogspot.com/2010/11/consuming-reporting-services-2005-web.html 

Today I wrote a very simple application that connects to Reporting Services 2005 web service.
The Web Services URL is:

http://[servername]/ReportServer/ReportService2005.asmx

Many sources give the following lines of code as a sample (assuming you have all the proper using statements)

ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
CatalogItem [] items = rs.ListChildren("/Sales Reports", true);

I first tried this with Visual Studio 2008 and noticed that there is no ReportingService2005 class. The closes class ReportingService2005SoapClient. So I switched to Visual Studio 2005 and the VS IntelliSense showed ReportingService2005 and my code worked.

After a some time of playing around I found a way to get the code to work in VS2008.

The app.config file of t he VS2008 a couple of changes needs to be made:

the binding tage , the property allowCookies has to be set true (I think not sure)


<security mode="TransportCredentialOnly">                                          
<transport clientCredentialType="Windows" proxyCredentialType="Windows"
realm="" />
</security>



This simple will use Windows integrated authentication.
One more line of code is need to allow impersonation:
rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

assuming rs is an instance of ReportingService2005SoapClient.

So a sample code looks something like the following:

ReportingService2005SoapClient rs = new ReportingService2005SoapClient();
rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
CatalogItem[] items;
rs.ListChildren("/", true, out items);
dataGridView1.DataSource = items;

Cool things can be done through RS2005 Web Service!