Archive for the ‘win32’ Category

TSQL – Function to Split a Delimited String and Return a Field

Tuesday, November 2nd, 2010

Here is a MS SQL Server (T-SQL) user defined function to split up a delmited string, and return the index value you care about.

Example:
select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',1)
returns:
'def'

CREATE FUNCTION dbo.GetValueOfSplitStringByIndex (@sep char(1), @s varchar(1540), @index int)
RETURNS varchar(1540)
AS
-- Takes a @sep separated string, turns it into a list internally and returns the value from that
-- list found at the @index location.  Null is return if that @index does not exist.
-- Sample usage (testing):
-- select dbo.GetValueOfSplitStringByIndex('-','200-2505-0000-01-149-101-10',1)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',0)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',1)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',2)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',3)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l',4)
-- select dbo.GetValueOfSplitStringByIndex(',','abc,def,hhhhhhh,l,',0)
-- select dbo.GetValueOfSplitStringByIndex(',','abc',0)
Begin
	declare @pos int
	declare @piece varchar(500)
	declare @curr_index int
	set @curr_index = 0
	-- Need to tack a delimiter onto the end of the input string if one doesn't exist
	if right(rtrim(@s),1) <> @sep
	 set @s = rtrim(@s)  + @sep

	set @pos =  patindex('%' + @sep + '%' , @s)
	while @pos <> 0
	begin
		set @piece = left(@s, @pos - 1)
		-- You have a piece of data, so insert it, print it, do whatever you want to with it.
		if @curr_index=@index
		return cast(@piece as varchar(1540))

		set @s = stuff(@s, 1, @pos, '')
		set @pos =  patindex('%' + @sep + '%' , @s)
		set @curr_index = @curr_index + 1
	end
	return Null
end

Fixing DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER When Working with Excel Files

Monday, August 16th, 2010

So I set up a SQL Agent Job to run my SSIS package following these steps.

Here’s the crazy thing. The package would run perfectly when I ran it directly or via a batch file, but when I ran it through the SQL Agent Job it would die and I would see a message like this in the log file I set up:

DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "Connection 1" failed with error code 0xC0209303. There may be error messages posted before this with more information on why the AcquireConnection method call failed.

I tried all of the work-arounds mentioned on this page. However none of them helped.

The answer it turns out is that 64 bit SQL Server Integration Services can’t handle Excel (xls) files! The trick is to use the 32 bit runtime of dtexec. To make the SQL Agent Job do this, go to the execution options tab of the job step and select “use 32 bit runtime”.

SSIS – How to Export a Flat File with New Lines

Monday, June 21st, 2010

I tried making a flat file in SQL Server Integration Services but SSIS decided to put all of the output on one line. I couldn’t figure out a way to get it to make one line per record using a fixed width file type.

Finally what I ended up doing was creating a ragged right file, and making a dummy field with 0 width as the last field. You see, ragged right format works perfectly for exporting fixed width flat files with one record per line except that it trims the trailing spaces after that last field.

So making a dummy 0 width field lets you keep all of the trailing spaces in the last field you care about but still lets SSIS do its thing to add carriage returns/line breaks.

Source

Fixing SSRS Error: ‘/’ is an unexpected token. The expected token is ‘=’. Line 9, position 33.

Monday, June 21st, 2010

So I got this error when running a report in SQL Server Reporting Services that takes several minutes to run:

'/' is an unexpected token. The expected token is '='. Line 9, position 33.

It turns out you just have to go to the web config file in C:\Program Files\Microsoft SQL Server\MSSQL.3\Reporting Services\ReportServer and change the timeout value to something high enough.

(Be sure to back up your web config file before editing it.)

Source

SQL Server Reporting Services (SSRS) – How to Change Number of Rows Per Page

Tuesday, May 11th, 2010

One way to do this is to change the interactive size of the report to something very long or short. For example if you want 1000 rows per page (don’t ask!) you can change the interactive size length to something like 200 inches.

It takes some trial and error to find the right length to use.

More discussion here.