Wednesday, March 28, 2012
Padd Character Function To Return Variable Type
I have a UDF for SQL that will padd a varchar value to a specific width and
align left or right. I found this code online somewhere, but the problem is
that it accpets a value up to Varchar(8000) and returns a varchar(8000). I
am writing a query to upload to a mainframe and need fixed-width fields, but
each field is not the same size. I can pass in the correct width to the
function and it padds correctly, but in a DTS transformation to a text file,
it thinks all fields are 8000 characters wide.
I am asking if it is possible to return a variable size data type instead of
a fixed type.
Here is the orig function:
ALTER FUNCTION [dbo].[PaddChar] (
@.ValueToPad varchar(8000),
@.PadCharacter char(1),
@.Justification bit,
@.Width int
)
/ ****************************************
***********************************
*************************
This function allows the USER TO pass IN a string / character value AND it
will padd the value according TO given parameters. The parameters are
AS follows:
@.ValueToPad = Value to be padded BY function.
@.PadCharacter = Character used TO pad a given value.
@.Justification = Justification format bit
0 - Value will be RIGHT justified after padding.
1 - Value will be LEFT justified after padding.
@.Width = Total COLUMN width OF the output after padding.
----
--
NOTE - ALL VALUES passes IN FOR padding must be OF CHAR or
VARCHAR data type.
****************************************
************************************
*************************/
RETURNS varchar(8000) AS
BEGIN
DECLARE @.x int
IF @.ValueToPad IS NULL SET @.ValueToPad = ''
IF @.PadCharacter IS NULL SET @.PadCharacter = ''
SET @.X = @.Width - LEN(@.ValueToPad)
--Right Justify Value
IF @.Justification = 0
BEGIN
SET @.ValueToPad = REPLICATE(@.PadCharacter,@.X) + @.ValueToPad
END
ELSE
--Left Justify Value
BEGIN
SET @.ValueToPad = @.ValueToPad + REPLICATE(@.PadCharacter,@.Width)
END
RETURN @.ValueToPad
END
and what I want basically and which doesn't seem to work, is:
ALTER FUNCTION [dbo].[PaddChar] (
@.ValueToPad varchar(8000),
@.PadCharacter char(1),
@.Justification bit,
@.Width int
)
/ ****************************************
***********************************
*************************
This function allows the USER TO pass IN a string / character value AND it
will padd the value according TO given parameters. The parameters are
AS follows:
@.ValueToPad = Value to be padded BY function.
@.PadCharacter = Character used TO pad a given value.
@.Justification = Justification format bit
0 - Value will be RIGHT justified after padding.
1 - Value will be LEFT justified after padding.
@.Width = Total COLUMN width OF the output after padding.
----
--
NOTE - ALL VALUES passes IN FOR padding must be OF CHAR or
VARCHAR data type.
****************************************
************************************
*************************/
RETURNS varchar(@.Width) AS
BEGIN
DECLARE @.x int
IF @.ValueToPad IS NULL SET @.ValueToPad = ''
IF @.PadCharacter IS NULL SET @.PadCharacter = ''
SET @.X = @.Width - LEN(@.ValueToPad)
--Right Justify Value
IF @.Justification = 0
BEGIN
SET @.ValueToPad = REPLICATE(@.PadCharacter,@.X) + @.ValueToPad
END
ELSE
--Left Justify Value
BEGIN
SET @.ValueToPad = @.ValueToPad + REPLICATE(@.PadCharacter,@.Width)
END
RETURN @.ValueToPad
END
Does anyone have any ideas on this?
Thanks,
NathanWhen I need to export fixed-width fields my approach is a bit
different. I create a view that formats each column the way I need it
to a fixed length, then export from the view. Examples of formatting:
convert(char(30), ItemName) as ItemName
STR(ItemValue,9,2)
Roy|||Thank you, I rarely use CHAR since it does padd for storage in the database,
duh! That worked to replace my instances of the dbo.PaddChar in my query fo
r
all the intstances where I am just padding on the right with spaces.
However, I had to just wrap a couple of them that I am right aligning, or
padding with 0's.
Since my query only pulls back a few rows a day the performance isn't bad.
Thank you again,
Nathan
"Roy Harvey" wrote:
> When I need to export fixed-width fields my approach is a bit
> different. I create a view that formats each column the way I need it
> to a fixed length, then export from the view. Examples of formatting:
> convert(char(30), ItemName) as ItemName
> STR(ItemValue,9,2)
> Roy
>
Monday, March 26, 2012
Package Validation Error in Custom PipelineComponent
Hi
I'm developing an PipelineComponent (ComponentType.Transform). When I try to execute this component, the following error is thrown:
Code Snippet
===================================
Package Validation Error (Package Validation Error)
===================================
Error at Data Flow Task [DTS.Pipeline]: Buffer Type 1 had a size of 0 bytes.
Error at Data Flow Task [DTS.Pipeline]: The buffer manager failed to create a new buffer type.
Error at Data Flow Task [DTS.Pipeline]: The Data Flow task cannot register a buffer type. The type had 50 columns and was for execution tree 0.
Error at Data Flow Task [DTS.Pipeline]: The layout failed validation.
Error at Data Flow Task: There were errors during task validation.
(Microsoft.DataTransformationServices.VsIntegration)
Program Location:
at Microsoft.DataTransformationServices.Project.DataTransformationsPackageDebugger.ValidateAndRunDebugger(Int32 flags, DataWarehouseProjectManager manager, IOutputWindow outputWindow, DataTransformationsProjectConfigurationOptions options)
at Microsoft.DataTransformationServices.Project.DataTransformationsProjectDebugger.LaunchDtsPackage(Int32 launchOptions, ProjectItem startupProjItem, DataTransformationsProjectConfigurationOptions options)
at Microsoft.DataTransformationServices.Project.DataTransformationsProjectDebugger.LaunchActivePackage(Int32 launchOptions)
at Microsoft.DataTransformationServices.Project.DataTransformationsProjectDebugger.LaunchDtsPackage(Int32 launchOptions, DataTransformationsProjectConfigurationOptions options)
at Microsoft.DataTransformationServices.Project.DataTransformationsProjectDebugger.Launch(Int32 launchOptions, DataTransformationsProjectConfigurationOptions options)
Can anyone help me, please? The "Integration Services Error and Message Reference" didn't help me much.
Thank you
Manuel Bauer
The error does not give me any clues other than it happens during a validation stage, so what is in your Validate method?
Have your tried debugging this, i.e. Visual Studio attached to the execution host?
|||Hi
Thank you for your answer. My pipeline component's validate method runs without any problems, I debugged this in design time and in run time (before the exception occured). In my opinion, the problem is the early validation (http://sqljunkies.com/WebLog/knight_reign/archive/2005/04/20/12365.aspx) of the data flow task. As I expect, the data flow task does any validation itself.
I can't imagine what "The Data Flow task cannot register a buffer type. The type had 50 columns and was for execution tree 0." means, so I really don't know where the problem could be.
The exception is thrown immediately after running the package (also after running the validate method of my pipeline component).
|||Does the package work if you take your component out of it?
The early validation vs late validation is the same Validate method calls, and since it generally works, I'd say it is clear that your transform has a problem, regardless of if it is the early or late call. Early and Late means normally Validate gets called twice, there is no difference between them. The Task will have a validate method, and a Data Flow will then call Validate for each child component.
How can the exception be thrown "after" running a package, Execute will never get called if a task fails validation. What is the return of your Validate method when you debug it?
Did you debug against dtsdebughost or just Visual Studio (designer)? For simplicity when trying to resolve run-time issues I set the code project debug option to use dtexec directly, just using /F and a package file. It is faster than attaching to a designer instance of VS.
|||I found a solution for my problem. I was thinking wrong about this validation thing, I thought that the data flow task does some independant, general validation itself.
In my component, I "forgot" to call base.Validate(), which would no be a problem if the own validate method is implemented as id should. In my case, it was not.
Thank you for your competent analysis of my problem.
Manuel Bauer
Package Validation Error + Code 0xC004801C
Hi,
I am learning to use SSIS for creating packages. I am getting the following error when I try to execute my package...
Package Validation Error
Error at Data Flow Task[DTS. Pipeline]:The "runtime connection "FlatFileConnection "(122)" in the connection manager collection, Connections, of component "Flat File Destination ("120") does not have a value for the ID property. Verify that the ConnectionManagerID property of the runtime connection object has been set for the component.
Error at Data Flow Task [DTS.Pipeline]: component "Flat File Destination" (120) failed validation and returned error code 0xC004801C.
Error at Data Flow Task[DTS.Pipeline]: One or more component failed validation.
Error at Data Flow Task: There were errors during task validation.
(Microsoft.DataTransformationServices.VsIntegration)
I'd appreciate if someone can help me with this. Do I need to change some settings? Also, what does the number in brackets indicate, say in FlatFileConnection(122)? Is it valuable information while debugging?
Just to give an idea, my package is like this -
source FlatFile -> Derived Column -> Lookup1 ->Lookup2 ->OLE DB destination(SQL server DB table)
Lookup1 and Lookup2 have error redirected to one flat file destination each.
Thanks in advance!
Geez....the error was just because I hadn't assigned a Connection Manager to that Flat File destination!! Oops!!!Friday, March 9, 2012
Owner of database
Many thanks.You should GRANT permissions for other user to access the table.
Refer to books online for more information.|||The table owner could also be causing you some greif. In other words if you create 'tbl_one' with 'user_one' and then try and access the table with 'user_two' you may need to explictly identify the tables owner. In other words 'select * from user_one.tbl_one' .
Of course just as Satya mentioned the user would need select permissions on this table (user_one.tbl_one)
Saturday, February 25, 2012
Overloading add/subtract operators for CLR UDT
Hi,
I have an implementation of the UDT - 3-dimentional vector. In my code I have implemented add, subtract and multiply methods for the type. I have also implemented overloaded operators for +/-/* in my C# code. Those overloaded operator are working as expected in C# tests. However when I��m trying to use +/-/* operators in T-SQL over my UDT it returns the following error:
Invalid operator for data type. Operator equals add, type equals Vector.
The following fragment does work:
DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector;
SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector)
SELECT @.v1 'v1', @.v2 'v2', @.v1.[Add](@.v2) 'v1 + v2'
And this fragment does not work:
DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector;
SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector)
SELECT @.v1 'v1', @.v2 'v2', @.v1+@.v2 'v1 + v2'
I guess that SQL Server is not aware of the operators�� overload I have implemented in the C# code. Is there any way to instruct SQL Server to use overloaded operators in the T-SQL so the code will look naturally @.a + @.b instead of @.a.[Add](@.b) and as a result use standard summary functions SUM() instead of writing user defined aggregate function for the Vector type field?
Maxim
? No, unfortunately overloading in SQLCLR is not possible. By the way, I prefer to do statics for Add/Subtract/etc, so instead of: @.a.Add(@.b) You'd do: type::Add(@.a, @.b) That way it's a little bit closer to the "a + b" syntax... But obviously not quite the same. You'll also still have to create your own UDA. -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Maxim Michtchenko@.discussions.microsoft.com> wrote in message news:03e7313f-081c-44f5-89f0-ce44d7de911d@.discussions.microsoft.com... Hi, I have an implementation of the UDT - 3-dimentional vector. In my code I have implemented add, subtract and multiply methods for the type. I have also implemented overloaded operators for +/-/* in my C# code. Those overloaded operator are working as expected in C# tests. However when I��m trying to use +/-/* operators in T-SQL over my UDT it returns the following error: Invalid operator for data type. Operator equals add, type equals Vector. The following fragment does work: DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector; SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector) SELECT @.v1 'v1', @.v2 'v2', @.v1.[Add](@.v2) 'v1 + v2' And this fragment does not work: DECLARE @.v1 Vector, @.v2 Vector, @.v3 Vector; SELECT @.v1 = CAST('1,1,1' as Vector), @.v2 = CAST('2,2,2' as Vector) SELECT @.v1 'v1', @.v2 'v2', @.v1+@.v2 'v1 + v2' I guess that SQL Server is not aware of the operators�� overload I have implemented in the C# code. Is there any way to instruct SQL Server to use overloaded operators in the T-SQL so the code will look naturally @.a + @.b instead of @.a.[Add](@.b) and as a result use standard summary functions SUM() instead of writing user defined aggregate function for the Vector type field? Maxim|||Thanks,
I guess I'll have to accept that. By the way I agree with you - static method looks clearner.
Maxim
|||I have a method that is overloaded. One takes 3 parameters and one takes 5. If overloading does not work, why does the CLR Function that I am trying to create work if I pass in 5 parameters? Does it take the method with the most parameters?