PC Review


Reply
Thread Tools Rate Thread

Determining a SQL Server datatype length

 
 
=?Utf-8?B?Sm9lIE1vbm5pbg==?=
Guest
Posts: n/a
 
      3rd Feb 2005
I have a database that holds large varchar data. The table has a column of
type varchar(8000). The stored procedure that is called to add the data to
the table also has a paramater of type varchar(8000). The stored procedure
is wrapped within a .net web service. I would like the web service to check
the length of the data passed to it and throw an exception if the data is
greater than 8000 characters long. But I don't want to hard code the 8000
byte limit into the web service. .net has MaxValue properties for simple
datatypes like int. System.Data.SqlDbType.VarChar.MaxValue does not exist,
however. Is there a way to get the maximum length of the VarChar datatype
from .net or better yet, the maximum accepted length of a varchar parameter
of a particular stored procedure?
 
Reply With Quote
 
 
 
 
Mary Chipman [MSFT]
Guest
Posts: n/a
 
      3rd Feb 2005
String.Length will give you the number of characters in a string. Is
that what you were looking for>

--Mary

On Thu, 3 Feb 2005 07:19:06 -0800, "Joe Monnin" <Joe
(E-Mail Removed)> wrote:

>I have a database that holds large varchar data. The table has a column of
>type varchar(8000). The stored procedure that is called to add the data to
>the table also has a paramater of type varchar(8000). The stored procedure
>is wrapped within a .net web service. I would like the web service to check
>the length of the data passed to it and throw an exception if the data is
>greater than 8000 characters long. But I don't want to hard code the 8000
>byte limit into the web service. .net has MaxValue properties for simple
>datatypes like int. System.Data.SqlDbType.VarChar.MaxValue does not exist,
>however. Is there a way to get the maximum length of the VarChar datatype
>from .net or better yet, the maximum accepted length of a varchar parameter
>of a particular stored procedure?


 
Reply With Quote
 
NuTcAsE
Guest
Posts: n/a
 
      3rd Feb 2005
Joe, to determine the max length of a parameter on a stored proc during
runtime you will first need to get the schema of the stored proc and
then check the length of the parameter.

You can do this by quering the INFORMATION_SCHEMA.PARAMETERS view. This
view provides information on the various parameters defined on sprocs.
For example:

SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME =
'MyProcName'

This will return you a result set contianinng the parameters for the
proc, its data type and length. So you could expand that sql statement
to get the length of the parameter. For ex:

SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.PARAMETERS
WHERE SPECIFIC_NAME = 'MyProcName' AND PARAMETER_NAME = '@name'

That select statement will return the maximum character length of a
varchar (or char) parameter. Then you can check for the length without
hard coding 8000.

PS. Dont forget to put the @ in the where condition.

NuTcAsE

 
Reply With Quote
 
=?Utf-8?B?Sm9lIE1vbm5pbg==?=
Guest
Posts: n/a
 
      3rd Feb 2005
No, what I'm after is the number of characters allowed in the stored
procedure paramater. For example, say I have a stored procedure:

create procedure sp_Proc1
@someparam varchar(30)
as
update tblWhatever set col1=@someparam

From my .net app, I want to find out how many characters I can pass to the
@someparam parameter in sp_Proc1 (30 in this case). If I pass a 60 character
string to this stored procedure, the last 30 characters will be dropped and
no exception will be raised, although one probably should. But since it's
not, I want to check to make sure that the string I want to pass to the
stored procedure is not longer than the stored procedure will accept.

"Mary Chipman [MSFT]" wrote:

> String.Length will give you the number of characters in a string. Is
> that what you were looking for>
>
> --Mary
>
> On Thu, 3 Feb 2005 07:19:06 -0800, "Joe Monnin" <Joe
> (E-Mail Removed)> wrote:
>
> >I have a database that holds large varchar data. The table has a column of
> >type varchar(8000). The stored procedure that is called to add the data to
> >the table also has a paramater of type varchar(8000). The stored procedure
> >is wrapped within a .net web service. I would like the web service to check
> >the length of the data passed to it and throw an exception if the data is
> >greater than 8000 characters long. But I don't want to hard code the 8000
> >byte limit into the web service. .net has MaxValue properties for simple
> >datatypes like int. System.Data.SqlDbType.VarChar.MaxValue does not exist,
> >however. Is there a way to get the maximum length of the VarChar datatype
> >from .net or better yet, the maximum accepted length of a varchar parameter
> >of a particular stored procedure?

>
>

 
Reply With Quote
 
=?Utf-8?B?Sm9lIE1vbm5pbg==?=
Guest
Posts: n/a
 
      3rd Feb 2005
Thanks. That will work. However, if I now move my system to antoher
database, the whole thing will break. It's not as simple as replacing the
SqlClient library with the AnotherDatabaseClient library. Is this the only
way to do it, or is there some any more general .net way of doing it that's
not dependent upon a particular database (or at least supported within the
..net classes for that database)?

"NuTcAsE" wrote:

> Joe, to determine the max length of a parameter on a stored proc during
> runtime you will first need to get the schema of the stored proc and
> then check the length of the parameter.
>
> You can do this by quering the INFORMATION_SCHEMA.PARAMETERS view. This
> view provides information on the various parameters defined on sprocs.
> For example:
>
> SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME =
> 'MyProcName'
>
> This will return you a result set contianinng the parameters for the
> proc, its data type and length. So you could expand that sql statement
> to get the length of the parameter. For ex:
>
> SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.PARAMETERS
> WHERE SPECIFIC_NAME = 'MyProcName' AND PARAMETER_NAME = '@name'
>
> That select statement will return the maximum character length of a
> varchar (or char) parameter. Then you can check for the length without
> hard coding 8000.
>
> PS. Dont forget to put the @ in the where condition.
>
> NuTcAsE
>
>

 
Reply With Quote
 
NuTcAsE
Guest
Posts: n/a
 
      4th Feb 2005
Hey Joe,

Well the approach I suggested is very SQL Server centric. In any case
which ever database you do use, you will first need to get the schema
of the stored proc and check out its parameter lengths. Im not sure how
that is achieved on other databases, but you can abstract the
functionality of getting the parameter lengths into a seperate project.

It would work something like this:

1. Main application calls GetParameterLength ("paramName", "sprocName",
DbConnection connection) :int of lets say a project called DbSchema.dll
2. DbSchema project first checks the provider type of the connection
(currently in 1.1 you can do this by checking its type).
3. If the connection is of SqlConnection, then you execute the above
example and get the parameter lenth. If its say OracleConnection, you
use an oracle specific query (im sure you can get schema information
for oracle too). Basically a switch statement to execute the correct
query.

Apart from that AFIK there is not .net generic way to get the schema
information of a stored proc. Sorry

NuTcAsE

 
Reply With Quote
 
=?Utf-8?B?Sm9lIE1vbm5pbg==?=
Guest
Posts: n/a
 
      7th Feb 2005
Apparantly, Microsoft has already thought about this. I just discoverd that
ADO.net 2.0 will support a GetSchema() method found in the connection class
that will provide a .net generic way to get the size of table columns. The
article (http://www.devx.com/dbzone/Article/27131/0/page/1) does not mention
if stored procedure metadata will also be available....Very interesting.

"NuTcAsE" wrote:

> Hey Joe,
>
> Well the approach I suggested is very SQL Server centric. In any case
> which ever database you do use, you will first need to get the schema
> of the stored proc and check out its parameter lengths. Im not sure how
> that is achieved on other databases, but you can abstract the
> functionality of getting the parameter lengths into a seperate project.
>
> It would work something like this:
>
> 1. Main application calls GetParameterLength ("paramName", "sprocName",
> DbConnection connection) :int of lets say a project called DbSchema.dll
> 2. DbSchema project first checks the provider type of the connection
> (currently in 1.1 you can do this by checking its type).
> 3. If the connection is of SqlConnection, then you execute the above
> example and get the parameter lenth. If its say OracleConnection, you
> use an oracle specific query (im sure you can get schema information
> for oracle too). Basically a switch statement to execute the correct
> query.
>
> Apart from that AFIK there is not .net generic way to get the schema
> information of a stored proc. Sorry
>
> NuTcAsE
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining the length of a presentation =?Utf-8?B?RG9uIEQ=?= Microsoft Powerpoint 1 11th May 2007 10:33 PM
Determining max. length of text that can be entered for another de =?Utf-8?B?Sm9obiBL?= Microsoft Dot NET Framework Forms 15 16th Feb 2007 10:00 AM
Determining length of a string in pixels =?Utf-8?B?SGF5U2VlZA==?= Microsoft Dot NET Framework Forms 1 8th Dec 2005 10:49 PM
determining datatype davegb Microsoft Excel Programming 8 1st Dec 2005 09:29 PM
Determining the length of text blue Microsoft ASP .NET 1 20th Nov 2003 07:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:58 AM.