Showing posts with label level. Show all posts
Showing posts with label level. Show all posts

Tuesday, March 27, 2012

Getting the actual length for columns in a result set

Is there a way to get the length (i.e. number of characters, bytes ...
) in the metadata information for a resultset?
I'm using low level ODBC calls, and my problem is that both
SQLDescribeCol and SQLColAttribute return the column size for the
length. This doesn't sound bad, but when reading the data I need to
have allocated a buffer big enough to hold the data.
More specifically, if I'm using the Text datatype and the result set
contains the text "hello" the size will always = 2^31. Does anyone
know of a low level ODBC call which would return 5, prior to fetching
the data?
Thanks in advance, -geoffThe problem is that the length in the metadata describes the maximum length
of the column. A varchar(5) column will report 5 when you call
SQLDescribeCol(). The metadata for a column is the same for every row in
the table (instance of the column). What you are interested in is the
length of the actual data in the column (or the length of a particular
instance of the column). This information is considered part of the actual
data in the row, rather than part of the column's metadata.
For non-BLOB columns, you typically want to allocate a buffer that can hold
the maximum length of the column (sure there are exceptions). For BLOB
columns you really should use SQLGetData() (rather than SQLBindCol()). If
you call SQLGetData() with a NULL TargetValuePtr parameter, but a valid
StrLen_or_IndPtr parameter, then SQLGetData() will return the length of the
data that is available for the column. Then you can use that value to
allocate a buffer and call SQLGetData() again to read the column.
Brannon Jones
Developer - MDAC
This posting is provided "as is" with no warranties and confers no rights.
"Geoff" <geoffpollard@.go.com> wrote in message
news:595a2518.0402111842.698363b7@.posting.google.com...
> Is there a way to get the length (i.e. number of characters, bytes ...
> ) in the metadata information for a resultset?
> I'm using low level ODBC calls, and my problem is that both
> SQLDescribeCol and SQLColAttribute return the column size for the
> length. This doesn't sound bad, but when reading the data I need to
> have allocated a buffer big enough to hold the data.
> More specifically, if I'm using the Text datatype and the result set
> contains the text "hello" the size will always = 2^31. Does anyone
> know of a low level ODBC call which would return 5, prior to fetching
> the data?
> Thanks in advance, -geoff|||I suspected there was another way. However, SQLGetData seems to fail
when I pass a NULL TargetValuePtr. The SQLRETURN == -1, and the
SQLGetDiagRec states "Invalid argument value". Here is a code snippet:
SQLRETURN sql_status_code;
SQLINTEGER namelen;
for (int i=0;i<numColumns;i++)
{
// fails
sql_status_code =
SQLGetData(hstmt,i+1,SQL_C_CHAR,NULL,0,&namelen);
// works
SQLCHAR test[255];
sql_status_code =
SQLGetData(hstmt,i+1,SQL_C_CHAR,test,255
,&namelen);
}
Prior to this the following statements returned SQL_SUCCESS:
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt);
// need to use server-side cursors to handle multiple queries
SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYP
E,
(SQLPOINTER)SQL_CURSOR_STATIC,SQL_IS_INT
EGER);
SQLPrepare(hstmt, (SQLCHAR*) s_SQL, SQL_NTS);
SQLExecute(hstmt);
for (int i=0;i<numColumns;i++)
SQLDescribeCol(...) //read column names
SQLFetch(hstmt)
--[ here is where I've been calling SQLGetData ]--
Any help would be greatly appreciated. Thanks in advance, -geoff
"Brannon Jones [MS]" <branjo@.nospam.microsoft.com> wrote in message news:<ureN$Ba8DHA.2760@.T
K2MSFTNGP09.phx.gbl>...
> The problem is that the length in the metadata describes the maximum lengt
h
> of the column. A varchar(5) column will report 5 when you call
> SQLDescribeCol(). The metadata for a column is the same for every row in
> the table (instance of the column). What you are interested in is the
> length of the actual data in the column (or the length of a particular
> instance of the column). This information is considered part of the actua
l
> data in the row, rather than part of the column's metadata.
> For non-BLOB columns, you typically want to allocate a buffer that can hol
d
> the maximum length of the column (sure there are exceptions). For BLOB
> columns you really should use SQLGetData() (rather than SQLBindCol()). If
> you call SQLGetData() with a NULL TargetValuePtr parameter, but a valid
> StrLen_or_IndPtr parameter, then SQLGetData() will return the length of th
e
> data that is available for the column. Then you can use that value to
> allocate a buffer and call SQLGetData() again to read the column.
> --
> Brannon Jones
> Developer - MDAC
> This posting is provided "as is" with no warranties and confers no rights.
>
> "Geoff" <geoffpollard@.go.com> wrote in message
> news:595a2518.0402111842.698363b7@.posting.google.com...|||Sorry I was slightly incorrect. You need to pass a valid pointer for
TargetValuePtr, but you pass a zero length. We will return the available
length in the StrLen_or_IndPtr parameter, and the return code will be
SQL_SUCCESS_WITH_INFO (SQLGetDiagRec will return: "[Microsoft][ODBC SQL
Server Driver]String data, right truncation", SQLState: 01004). Then you
allocate the buffer and call again.
Let me know if that doesn't help.
Brannon Jones
Developer - MDAC
This posting is provided "as is" with no warranties and confers no rights.
"Geoff" <geoffpollard@.go.com> wrote in message
news:595a2518.0402140153.5a248edc@.posting.google.com...
> I suspected there was another way. However, SQLGetData seems to fail
> when I pass a NULL TargetValuePtr. The SQLRETURN == -1, and the
> SQLGetDiagRec states "Invalid argument value". Here is a code snippet:
> SQLRETURN sql_status_code;
> SQLINTEGER namelen;
> for (int i=0;i<numColumns;i++)
> {
> // fails
> sql_status_code =
> SQLGetData(hstmt,i+1,SQL_C_CHAR,NULL,0,&namelen);
> // works
> SQLCHAR test[255];
> sql_status_code =
> SQLGetData(hstmt,i+1,SQL_C_CHAR,test,255
,&namelen);
> }
> Prior to this the following statements returned SQL_SUCCESS:
> SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt);
> // need to use server-side cursors to handle multiple queries
> SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYP
E,
> (SQLPOINTER)SQL_CURSOR_STATIC,SQL_IS_INT
EGER);
> SQLPrepare(hstmt, (SQLCHAR*) s_SQL, SQL_NTS);
> SQLExecute(hstmt);
> for (int i=0;i<numColumns;i++)
> SQLDescribeCol(...) //read column names
> SQLFetch(hstmt)
> --[ here is where I've been calling SQLGetData ]--
> Any help would be greatly appreciated. Thanks in advance, -geoff
> "Brannon Jones [MS]" <branjo@.nospam.microsoft.com> wrote in message
news:<ureN$Ba8DHA.2760@.TK2MSFTNGP09.phx.gbl>...
length
in
actual
hold
If
the
rights.

Wednesday, March 21, 2012

Getting rid of the "All" level in query results

I have two seemingly identical cubes, one imported from AS 2000 and one
created using AMO in AS 2000 (they are both named new1 and are located in
separate databases). Yet, when issuintg the following simple query:
select {[RecvPay].[RecvPay].members} on columns from new1
I get only columns 1 and 2 on the imported cube, while I get an additional
"All RecvPay" column on the created cube. I have scoured the XMLA script
generated from each RecvPay dimension and how it is used in the new1 cube
without seeing any meaningful differences. I want to get rid of this "All
RecvPay" in my results. Am I missing something?

Thanks,
Boris Zakharin, MCAD
Metavante Risk and Compliance Solutions

You could just use select {[RecvPay].[RecvPay].Children} on columns from new1

|||I understand there are other ways to get it, but why the difference?
|||

There is an IsAggregatable property on each attribute which sets if it generates this automatic All member. If you don't want an All member in the attribute at all you can change this setting from the default of "true" to false. If you had the all member turned off in AS2000, this setting was probably kept when you migrated, if you generated the dimension in AMO without overriding the defaults, then it would automatically generate an All member.

If you only want the All member excluded from the query then there is a slight change in naming between AS2000 and AS2005. AS 2000 uses a <dimension>.<level>.members syntax, and was not truely multi-hierarchy aware. AS2005 is truely multi hierarchy aware and you need to use <dimension>.<hierarchy>.<level>.members. When you create an attritube it generates an implied single level hierarchy.

|||Thank you. I'll try it first thing tomorrow
|||

That does work in my example (and in the much more complicated originsl query this came from), but that is definitely not how the 2000 dimension was constructed since I can see "All RecvPay" when browsing that dimension. I am just wondering whether other queries might fail or produce unexpected results with this change.

|||

In this case, the difference in you queries is most likely explained by the fact that in SSAS 2005 you need to specify the heirarchy in the member/level references.

In AS 2000 [RecvPay].[RecvPay] refers to the RecvPay level of the RecvPay dimension

In AS 2005 [RecvPay].[RecvPay] refers to the RecvPay hierarchy of the RecvPay dimension

When you get the members of an entire hierarchy it includes the All member. To refer to just the level of RecvPay members (which does not include the all member) you need to use a [RecvPay].[RecvPay].[RecvPay] reference.

Getting rid of the "All" level in query results

I have two seemingly identical cubes, one imported from AS 2000 and one
created using AMO in AS 2000 (they are both named new1 and are located in
separate databases). Yet, when issuintg the following simple query:
select {[RecvPay].[RecvPay].members} on columns from new1
I get only columns 1 and 2 on the imported cube, while I get an additional
"All RecvPay" column on the created cube. I have scoured the XMLA script
generated from each RecvPay dimension and how it is used in the new1 cube
without seeing any meaningful differences. I want to get rid of this "All
RecvPay" in my results. Am I missing something?

Thanks,
Boris Zakharin, MCAD
Metavante Risk and Compliance Solutions

You could just use select {[RecvPay].[RecvPay].Children} on columns from new1

|||I understand there are other ways to get it, but why the difference?
|||

There is an IsAggregatable property on each attribute which sets if it generates this automatic All member. If you don't want an All member in the attribute at all you can change this setting from the default of "true" to false. If you had the all member turned off in AS2000, this setting was probably kept when you migrated, if you generated the dimension in AMO without overriding the defaults, then it would automatically generate an All member.

If you only want the All member excluded from the query then there is a slight change in naming between AS2000 and AS2005. AS 2000 uses a <dimension>.<level>.members syntax, and was not truely multi-hierarchy aware. AS2005 is truely multi hierarchy aware and you need to use <dimension>.<hierarchy>.<level>.members. When you create an attritube it generates an implied single level hierarchy.

|||Thank you. I'll try it first thing tomorrow
|||

That does work in my example (and in the much more complicated originsl query this came from), but that is definitely not how the 2000 dimension was constructed since I can see "All RecvPay" when browsing that dimension. I am just wondering whether other queries might fail or produce unexpected results with this change.

|||

In this case, the difference in you queries is most likely explained by the fact that in SSAS 2005 you need to specify the heirarchy in the member/level references.

In AS 2000 [RecvPay].[RecvPay] refers to the RecvPay level of the RecvPay dimension

In AS 2005 [RecvPay].[RecvPay] refers to the RecvPay hierarchy of the RecvPay dimension

When you get the members of an entire hierarchy it includes the All member. To refer to just the level of RecvPay members (which does not include the all member) you need to use a [RecvPay].[RecvPay].[RecvPay] reference.

Friday, February 24, 2012

Getting error while using "text" datatype

Hello, I am writing a sproc and am getting this error: Any ideas? Thanks!!

Msg 402, Level 16, State 1, Procedure InsertUserPreferences, Line 18

The data types text and text are incompatible in the equal to operator.

-

create procedure InsertUserPreferences

(

@.PublisherServer text

)

as

begin

if exists(Select Preference_StringList from USER_Preference where Preference_StringList = @.PublisherServer)

begin

--UPDATE

exec dbo.uProc_USER_Preference

end

If you are using 2005, change to using varchar(max) and this will work. You cannot use a text value in a equality comparison

create table test
(
textValue text
)
go
select *
from test
where textValue = ''
go

You could use a cast to only get the first 8000 characters:

select *
from test
where cast(textValue as varchar(8000)) = ''
go

If that will work for you.

Is that your entire proc? How des the update work? If it exists, do you just update the modify date?

|||I will give it a shot and get back with you. Thank you for your help!!!|||Dude that worked!! Thanks man!!!!

Getting error while using "text" datatype

Hello, I am writing a sproc and am getting this error: Any ideas? Thanks!!

Msg 402, Level 16, State 1, Procedure InsertUserPreferences, Line 18

The data types text and text are incompatible in the equal to operator.

-----------------------------------

create procedure InsertUserPreferences

(

@.PublisherServer text

)

as

begin


if exists(Select Preference_StringList from USER_Preference where Preference_StringList = @.PublisherServer)

begin

--UPDATE

exec dbo.uProc_USER_Preference

end

You cannot use an equal operator with TEXT or NTEXT. You can only use LIKE for comparison. Why are you using text, and not varchar? Are you using SQL 2000 or 2005?|||

Hello and thanks for responding!! I restored data from a SQL Server 2000 database to a SQL Server 2005 to meet our requirements. It was our DBA's decision to make that field a datatype of 'text'.

|||I would recommend changing it to VARCHAR if possible.