I don't understand, what result are you getting, and what do you want
instead?
In SQL Server 2005 you can differentiate by querying from sys.types and
comparing system_type_id with user_type_id. These will be different for
alias types you have defined, like salutation, lastname etc. (There is one
exception: sysname, which is a built-in alias type.)
But do you really want to filter out these alias types? Why? They can be
used in your tables just like the built-in types, but can have different
lengths.
On 7/21/08 1:44 PM, in article uX8myl16IHA.1468@TK2MSFTNGP05.phx.gbl,
> I ran into a problem with my query where I was trying to get the type and
> length of fields in my table.
[quoted text clipped - 45 lines]
>
> Tom
Plamen Ratchev - 21 Jul 2008 19:30 GMT
I believe it is the same logic when using systypes, when 'xtype' and
'xusertype' are different it is user defined, with exception of sysnames.
Plamen Ratchev
http://www.SQLStudio.com
tshad - 21 Jul 2008 21:27 GMT
>I believe it is the same logic when using systypes, when 'xtype' and
>'xusertype' are different it is user defined, with exception of sysnames.
So the question washow was I going to get the correct systype name from a
syscolumn row?
Can't use xtype to find it. It turned out that I could use xusertype which
is the same as xtype except for user types. User type rows will be unique
and the xusertype field from syscolumns will match the xusertype field from
systypes.
Now I can get the user type name as well.
Thanks,
Tom
> Plamen Ratchev
> http://www.SQLStudio.com
Plamen Ratchev - 21 Jul 2008 21:40 GMT
See my other post, you can use the TYPE_NAME function:
SELECT TYPE_NAME(xtype)
FROM syscolumns;
HTH,
Plamen Ratchev
http://www.SQLStudio.com
tshad - 21 Jul 2008 21:03 GMT
>I don't understand, what result are you getting, and what do you want
> instead?
What I am getting is an error, because I have an object from syscolumns, and
I am trying to get the "type" name and "length". The problem is that if the
"type" id from syscolumns is 167 (varchar, firstname, postalcode, city etc),
I get an error on my select because it can only return one value. But for
167, I have 11 values. And for 175, I have 3.
So if I am looking for the name of the type:
Select a.Name,a.id,Records,
CreatedByName = (SELECT st.name
FROM syscolumns sc
JOIN systypes st ON (sc.xtype = st.xtype)
WHERE (sc.name = 'CreatedBy') AND a.id = sc.id),
FROM (select Object_Name(id) Name,id,count(*) as Records
from syscolumns
where name = 'CreatedBy'
group by Object_Name(id),id
having count(*) = 5) as a
order by Name
this gives me an error if "CreatedBy" is a varhar (167) or a char (175)
since you can't have multiple rows return on this type of subquery (the one
that returns CreateByName.
But if I change it to:
> In SQL Server 2005 you can differentiate by querying from sys.types and
> comparing system_type_id with user_type_id. These will be different for
[quoted text clipped - 61 lines]
>>
>> Tom
Sorry I accitdently hit the wrong keys and it got sent:
"tshad" <tshad@dslextreme.com> wrote in message news:...
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
> message news:C4AA4AE5.D018%ten.xoc@dnartreb.noraa...
>>I don't understand, what result are you getting, and what do you want
>> instead?
What I am getting is an error, because I have an object from syscolumns,
and
I am trying to get the "type" name and "length". The problem is that if
the
"type" id from syscolumns is 167 (varchar, firstname, postalcode, city
etc),
I get an error on my select because it can only return one value. But for
167, I have 11 values. And for 175, I have 3.
So if I am looking for the name of the type:
Select a.Name,a.id,Records,
CreatedByName = (SELECT st.name
FROM syscolumns sc
JOIN systypes st ON (sc.xtype = st.xtype)
WHERE (sc.name = 'CreatedBy') AND a.id = sc.id),
FROM (select Object_Name(id) Name,id,count(*) as Records
from syscolumns
where name = 'CreatedBy'
group by Object_Name(id),id) as a
order by Name
this gives me an error if "CreatedBy" is a varhar (167) or a char (175)
since you can't have multiple rows return on this type of subquery (the one
that returns CreateByName.
But if I change it to:
Select a.Name,a.id,Records,
CreatedByName = (SELECT st.name
FROM syscolumns sc
JOIN systypes st ON (sc.xtype = st.xtype)
WHERE (sc.name = 'CreatedBy') AND uid = 4 AND a.id = sc.id),
FROM (select Object_Name(id) Name,id,count(*) as Records
from syscolumns
where name = 'CreatedBy'
group by Object_Name(id),id
having count(*) = 5) as a
order by Name
where I add a filter (uid = 4), it works fine - but won't give me the user
types - since they are all 167 but only one has uid of 4. The rest are 1.
This is fine for what I am doing at the moment. But it would be nice to be
able to get the correctly type name - I just can't do it this way.
> In SQL Server 2005 you can differentiate by querying from sys.types and
> comparing system_type_id with user_type_id. These will be different for
[quoted text clipped - 6 lines]
>> used in your tables just like the built-in types, but can have different
>> lengths.
Yes, in this case.
Thanks,
Tom
>> On 7/21/08 1:44 PM, in article uX8myl16IHA.1468@TK2MSFTNGP05.phx.gbl,
>> "tshad" <tshad@dslextreme.com> wrote:
[quoted text clipped - 53 lines]
>>>
>>> Tom