Collation issue | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Collation issue

We have a SQL Server 2000 database server with a database created on this box with 65 compatibility set.<br /><br />I have created a stored procedure based on the undocumented stored procedure sp_MShelpcolumns.<br /><br /><font color="blue">USE MASTER<br />GO<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_tabledefinition]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)<br />drop procedure [dbo].[sp_tabledefinition]<br />GO<br />create procedure [dbo].[sp_tabledefinition] (<br /> @tablename nvarchar(517),<br /> @flags int = 0,<br /> @orderby nvarchar(10) = null,<br /> @flags2 int = 0<br /> )<br />as<br /> — taken from sp_MShelpcolumns –<br /> create table #sphelpcols<br /> (<br /> col_name nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL,<br /> col_typename nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL,<br /> col_len int NOT NULL,<br /> col_prec int NULL,<br /> col_scale int NULL,<br /> col_null bit NOT NULL,<br /> col_identity bit NOT NULL,<br /> col_flags int NULL,<br /> col_seed nvarchar (40) COLLATE database_default NULL,<br /> col_increment nvarchar (40) COLLATE database_default NULL,<br /> col_collation nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NULL,<br /> col_language int,<br /> —————————————<br /> — Required Fields –<br /> —————————————<br /> col_id int NOT NULL,<br /> col_objectid int NOT NULL,<br /> col_dridefid int NULL,<br /> col_basetypename nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL<br /> )<br /><br />if @flags is null<br />select @flags = 0<br />if (@tablename = N’?’)<br />begin<br />print N”<br />print N’Usage: sp_MShelpcolumns @tablename, @flags int = 0′<br />print N’ where @flags is a bitmask of:'<br />print N’ 0x0200= No DRI (ignore Checks, Primary/Foreign/Unique Keys, etc.)'<br />print N’ 0x0400= UDDTs –&gt; Base type'<br />print N’ 0x80000= TimestampToBinary (convert timestamp cols to binary(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)'<br />print N’ 0x40000000= No Identity attribute'<br />return 0<br />end<br /><br />declare @objid int<br />select @objid = object_id(@tablename)<br />if (@objid is null)<br />begin<br />RAISERROR (15001, -1, -1, @tablename)<br />return 1<br />end<br /><br />set nocount on<br /><br />insert #sphelpcols<br />select <br />convert(varchar(255), c.name), <br />convert(varchar(255), st.name),<br /> case when bt.name in (N’nchar’, N’nvarchar’) then c.length/2 else c.length end,<br />ColumnProperty(@objid, c.name, N’Precision’),<br />ColumnProperty(@objid, c.name, N’Scale’),<br />convert(bit, ColumnProperty(@objid, c.name, N’AllowsNull’)),<br />case when (@flags & 0x40000000 = 0) then convert(bit, ColumnProperty(@objid, c.name, N’IsIdentity’)) else 0 end,<br />case when st.name in (N’char’,N’varchar’,N’binary’,N’varbinary’,N’nchar’,N’nvarchar’) then 0x0001 when st.name in (N’decimal’,N’numeric’) then 0x0002 else 0 end + case isnull(ColumnProperty(@objid, c.name, N’IsRowGuidCol’), 0) when 0 then 0 else 0x0008 end,<br />case when (ColumnProperty(@objid, c.name, N’IsIdentity’) &lt;&gt; 0) then CONVERT(varchar(80), ident_seed(@tablename)) else null end,<br />case when (ColumnProperty(@objid, c.name, N’IsIdentity’) &lt;&gt; 0) then CONVERT(varchar(80), ident_incr(@tablename)) else null end,<br /> c.collation,<br /> c.language,<br /> —————————————<br /> — Required Fields –<br /> —————————————<br />c.colid,<br /> c.id,<br />case when (@flags & 0x0200 = 0 and c.cdefault is not null and (OBJECTPROPERTY(c.cdefault, N’IsDefaultCnst’) &lt;&gt; 0)) then t.id else null end,<br /> convert(varchar(255), bt.name)<br />from dbo.syscolumns c<br />left outer join dbo.sysobjects d on d.id = c.cdefault<br />left outer join dbo.sysobjects r on r.id = c.domain<br />join dbo.systypes st on st.xusertype = c.xusertype<br />join dbo.systypes bt on bt.xusertype = c.xtype<br />left outer join dbo.syscomments t on t.id = c.cdefault and t.colid = 1<br />and not exists (select * from dbo.syscomments where id = c.cdefault and colid = 2)<br />where c.id = @objid<br />order by c.colid<br /><br />if (@flags & 0x80000 != 0)<br />update #sphelpcols set col_typename = N’binary’, col_len = 8, col_flags = col_flags | 0x0001 where col_typename = N’timestamp'<br /><br />if (@flags is not null and @flags != 0)<br />begin<br />if (@flags & 0x0400 != 0)<br />begin<br />declare @typeflagmask int <br />select @typeflagmask = (convert(int, 0x0001) + convert(int, 0x0002))<br /><br />update #sphelpcols <br />set col_typename = convert(varchar(255), b.name),<br />col_flags = col_flags & [email protected] + case when b.name in (N’char’,N’varchar’,N’binary’,N’varbinary’,N’nchar’,N’nvarchar’) then 0x0001 when b.name in (N’decimal’,N’numeric’) then 0x0002 else 0 end<br />from #sphelpcols c, <br />dbo.systypes n, <br />dbo.systypes b<br />where n.name = col_typename –// xtype (base type) of name <font color="red">(FAILS HERE)</font id="red">[<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />]<br />and b.xusertype = n.xtype –// Map it back to where it’s xusertype, to get the name<br /><br />end<br />end<br /><br />if (@flags & 0x0200 = 0 and (OBJECTPROPERTY(@objid, N’TableHasPrimaryKey’) &lt;&gt; 0)) begin<br />declare @indid int<br />select @indid = indid from dbo.sysindexes i where i.id = @objid and i.status & 0x0800 &lt;&gt; 0<br />if (@indid is not null)<br />update #sphelpcols <br />set col_flags = col_flags | 0x0004<br />from #sphelpcols c, <br />dbo.sysindexkeys i<br />where i.id = @objid and i.indid = @indid and i.colid = c.col_id<br />end<br /><br />set nocount off<br />if (@orderby is null or @orderby = N’id’)<br />begin<br />select c.col_name, <br />c.col_typename, <br />c.col_len,<br />col_prec = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_prec else NULL end,<br />col_scale = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_scale else NULL end,<br />c.col_null, <br />c.col_identity, <br />c.col_flags,<br />c.col_seed,<br /> c.col_increment,<br />c.col_collation,<br />case when ( c.col_language &gt;= 0 ) then c.col_language else -1 end as col_language&l t;br />from ((#sphelpcols c<br /> left outer join dbo.syscomments cm on cm.id = c.col_objectid and cm.number = c.col_id) left outer join dbo.syscomments cn on c.col_dridefid is not null and cn.id = c.col_dridefid)<br /> left outer join (select distinct <br />FT_COLNAME = scol.name, <br />FT_ID = sdep.number <br /> from dbo.syscolumns scol, <br />dbo.sysdepends sdep <br /> where scol.colid = sdep.depnumber and<br /> sdep.deptype = 1 and<br /> scol.id = @objid and<br /> sdep.depid = @objid and<br /> ColumnProperty(scol.id, scol.name, N’IsTypeForFullTextBlob’) = 1<br />) as d on c.col_id = d.FT_ID<br />order by c.col_id<br />end else begin<br />select c.col_name, <br />c.col_typename, <br />c.col_len,<br />col_prec = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_prec else NULL end,<br />col_scale = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_scale else NULL end,<br />c.col_null, <br />c.col_identity, <br />c.col_flags,<br />c.col_seed,<br /> c.col_increment,<br />c.col_collation,<br />case when ( c.col_language &gt;= 0 ) then c.col_language else -1 end as col_language<br />from (<br />(#sphelpcols c<br /> left outer join dbo.syscomments cm on (cm.id = c.col_objectid <br /> and cm.number = c.col_id )<br />)<br /> left outer join dbo.syscomments cn on (c.col_dridefid is not null <br />and cn.id = c.col_dridefid)<br /> )<br /> left outer join (select distinct <br />FT_COLNAME = scol.name, <br />FT_ID = sdep.number <br /> from dbo.syscolumns scol, <br />dbo.sysdepends sdep <br /> where scol.colid = sdep.depnumber and<br /> sdep.deptype = 1 and<br /> scol.id = @objid and<br /> sdep.depid = @objid and<br /> ColumnProperty(scol.id, scol.name, N’IsTypeForFullTextBlob’) = 1<br />) as d on (c.col_id = d.FT_ID)<br />order by c.col_name<br />end<br />GO</font id="blue"><br /><br /><font color="black">Execute the following line against a 65 compatible database on the SQL 2000 database server</font id="black"><br />execute sp_tabledefinition ‘dbo.EnterTableNameHere’, 512, @orderby = ‘id'<br /><br />Does anyone know how I can get around this?[?]<br />
I never reach a collation error. I get this error first: Server: Msg 207, Level 16, State 3, Procedure sp_tabledefinition, Line 123
Invalid column name ‘col_numtype’.
Server: Msg 207, Level 16, State 1, Procedure sp_tabledefinition, Line 123
Invalid column name ‘col_numtype’.
Server: Msg 207, Level 16, State 1, Procedure sp_tabledefinition, Line 150
Invalid column name ‘col_numtype’.
Server: Msg 207, Level 16, State 1, Procedure sp_tabledefinition, Line 150
Invalid column name ‘col_numtype’. This is when running the following in Northwind:
execute sp_tabledefinition ‘dbo.Employees’, 512, @orderby = ‘id’
Apologies, correction made for col_numtype – still get collation conflict.<br /><font color="red"><br />USE MASTER<br />GO<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_tabledefinition]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)<br />drop procedure [dbo].[sp_tabledefinition]<br />GO<br />create procedure [dbo].[sp_tabledefinition] (<br />@tablename nvarchar(517),<br />@flags int = 0,<br />@orderby nvarchar(10) = null,<br />@flags2 int = 0<br />)<br />as<br />– taken from sp_MShelpcolumns –<br />create table #sphelpcols<br />(<br />col_name nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL,<br />col_typename nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL,<br />col_len int NOT NULL,<br />col_prec int NULL,<br />col_scale int NULL,<br />col_null bit NOT NULL,<br />col_identity bit NOT NULL,<br />col_flags int NULL,<br />col_seed nvarchar (40) COLLATE database_default NULL,<br />col_numtype smallint NOT NULL,<br />col_increment nvarchar (40) COLLATE database_default NULL,<br />col_collation nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NULL,<br />col_language int,<br />—————————————<br />– Required Fields –<br />—————————————<br />col_id int NOT NULL,<br />col_objectid int NOT NULL,<br />col_dridefid int NULL,<br />col_basetypename nvarchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> COLLATE database_default NOT NULL<br />)<br /><br />if @flags is null<br /> select @flags = 0<br /><br />if (@tablename = N’?’)<br />begin<br />print N”<br />print N’Usage: sp_MShelpcolumns @tablename, @flags int = 0′<br />print N’ where @flags is a bitmask of:'<br />print N’ 0x0200 = No DRI (ignore Checks, Primary/Foreign/Unique Keys, etc.)'<br />print N’ 0x0400 = UDDTs –&gt; Base type'<br />print N’ 0x80000 = TimestampToBinary (convert timestamp cols to binary(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)'<br />print N’ 0x40000000 = No Identity attribute'<br />return 0<br />end<br /><br />declare @objid int<br />select @objid = object_id(@tablename)<br /><br />if (@objid is null)<br />begin<br />RAISERROR (15001, -1, -1, @tablename)<br />return 1<br />end<br /><br />set nocount on<br /><br />insert #sphelpcols<br />select <br />convert(varchar(255), c.name), <br />convert(varchar(255), st.name),<br />case when bt.name in (N’nchar’, N’nvarchar’) then c.length/2 else c.length end,<br />ColumnProperty(@objid, c.name, N’Precision’),<br />ColumnProperty(@objid, c.name, N’Scale’),<br />case when (@flags2 & 1 &lt;&gt; 0 and bt.name in (N’tinyint’,N’smallint’,N’decimal’,N’int’,N’real’,N’money’,N’float’,N’numeric’,N’smallmoney’,N’bigint’)) then 1 else 0 end,<br />convert(bit, ColumnProperty(@objid, c.name, N’AllowsNull’)),<br />case when (@flags & 0x40000000 = 0) then convert(bit, ColumnProperty(@objid, c.name, N’IsIdentity’)) else 0 end,<br />case when st.name in (N’char’,N’varchar’,N’binary’,N’varbinary’,N’nchar’,N’nvarchar’) then 0x0001 when st.name in (N’decimal’,N’numeric’) then 0x0002 else 0 end + case isnull(ColumnProperty(@objid, c.name, N’IsRowGuidCol’), 0) when 0 then 0 else 0x0008 end,<br />case when (ColumnProperty(@objid, c.name, N’IsIdentity’) &lt;&gt; 0) then CONVERT(varchar(80), ident_seed(@tablename)) else null end,<br />case when (ColumnProperty(@objid, c.name, N’IsIdentity’) &lt;&gt; 0) then CONVERT(varchar(80), ident_incr(@tablename)) else null end,<br />c.collation,<br />c.language,<br />—————————————<br />– Required Fields –<br />—————————————<br />c.colid,<br />c.id,<br />case when (@flags & 0x0200 = 0 and c.cdefault is not null and (OBJECTPROPERTY(c.cdefault, N’IsDefaultCnst’) &lt;&gt; 0)) then t.id else null end,<br />convert(varchar(255), bt.name)<br />from dbo.syscolumns c<br />left outer join dbo.sysobjects d on d.id = c.cdefault<br />left outer join dbo.sysobjects r on r.id = c.domain<br />join dbo.systypes st on st.xusertype = c.xusertype<br />join dbo.systypes bt on bt.xusertype = c.xtype<br />left outer join dbo.syscomments t on t.id = c.cdefault and t.colid = 1<br />and not exists (select * from dbo.syscomments where id = c.cdefault and colid = 2)<br />where c.id = @objid<br />order by c.colid<br /><br />if (@flags & 0x80000 != 0)<br />update #sphelpcols set col_typename = N’binary’, col_len = 8, col_flags = col_flags | 0x0001 where col_typename = N’timestamp'<br /><br />if (@flags is not null and @flags != 0)<br />begin<br />if (@flags & 0x0400 != 0)<br />begin<br />declare @typeflagmask int <br />select @typeflagmask = (convert(int, 0x0001) + convert(int, 0x0002))<br /><br />update #sphelpcols <br />set col_typename = convert(varchar(255), b.name),<br />col_flags = col_flags & [email protected] + case when b.name in (N’char’,N’varchar’,N’binary’,N’varbinary’,N’nchar’,N’nvarchar’) then 0x0001 when b.name in (N’decimal’,N’numeric’) then 0x0002 else 0 end<br />from #sphelpcols c, <br />dbo.systypes n, <br />dbo.systypes b<br />where n.name = col_typename –// xtype (base type) of name (FAILS HERE)<br />and b.xusertype = n.xtype –// Map it back to where it’s xusertype, to get the name<br />end<br />end<br /><br />if (@flags & 0x0200 = 0 and (OBJECTPROPERTY(@objid, N’TableHasPrimaryKey’) &lt;&gt; 0)) <br />begin<br />declare @indid int<br />select @indid = indid from dbo.sysindexes i where i.id = @objid and i.status & 0x0800 &lt;&gt; 0<br /><br />if (@indid is not null)<br />update #sphelpcols <br />set col_flags = col_flags | 0x0004<br />from #sphelpcols c, <br />dbo.sysindexkeys i<br />where i.id = @objid and i.indid = @indid and i.colid = c.col_id<br />end<br /><br />set nocount off<br /><br />if (@orderby is null or @orderby = N’id’)<br />begin<br />select c.col_name, <br />c.col_typename, <br />c.col_len,<br />col_prec = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_prec else NULL end,<br />col_scale = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_scale else NULL end,<br />c.col_null, <br />c.col_identity, <br />c.col_flags,<br />c.col_seed,<br />c.col_increment,<br />c.col_collation,<br />case when ( c.col_language &gt;= 0 ) then c.col_language else -1 end as col_language<br />from ((#sphelpcols c<br />left outer join dbo.syscomments cm on cm.id = c.col_objectid and cm.number = c.col_id) left outer join dbo.syscomments cn on c.col_dridefid is not null and cn.id = c.col_dridefid)<br />left outer join (select distinct <br />FT_COLNAME = scol.name, <br />FT_ID = sdep.number <br />from dbo.syscolumns scol, <br />dbo.sysdepends sdep <br />where scol.colid = sdep.depnumber and<br />sdep.deptype = 1 and<br />scol.id = @objid and<br />sdep.depid = @objid and<br />ColumnProperty(scol.id, scol.name, N’IsTypeForFullTextBlob’) = 1<br />) as d on c.col_id = d.FT_ID<br />order by c.col_id<br />end <br />else <br />begin<br />select c.col_name, <br />c.col_typename, <br />c.col_len,<br />col_prec = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_prec else NULL end,<br />col_scale = case when (col_basetypename in (N’decimal’,N’numeric’) or (@flags2 & 1 &lt;&gt; 0 and col_numtype & 1 &lt;&gt; 0)) then c.col_scale else NULL end,<br />c.col_null, <br />c.col_identity, <br />c.col_flags,<br />c.col_seed,<br />c.col_increment,<br />c.col_collation,<br />case when ( c.col_language &gt;= 0 ) then c.col_language else -1 end as col_language<br />from (<br />(#sphelpcols c<br />left outer join dbo.syscomments cm on (cm.id = c.col_objectid <br />and cm.number = c.col_id )<br />)<br />left outer join dbo.syscomments cn on (c.col_dridefid is not null <br />and cn.id = c.col_dridefid)<br />)<br />left outer join (select distinct <br />FT_COLNAME = scol.name, <br />FT_ID = sdep.number <br />from dbo.syscolumns scol, <br />dbo.sysdepends sdep <br />where scol.colid = sdep.depnumber and<br />sdep.deptype = 1 and<br />scol.id = @objid and<br />sdep.depid = @objid and<br />ColumnProperty(scol.id, scol.name, N’IsTypeForFullTextBlob’) = 1<br />) as d on (c.col_id = d.FT_ID)<br />order by c.col_name<br />end<br />GO</font id="red"><br /><br />use northwind<br />GO<br />execute sp_tabledefinition ‘dbo.Employees’, 512, @orderby = ‘id'<br />
Now I get this error:
Server: Msg 515, Level 16, State 2, Procedure sp_tabledefinition, Line 59
Cannot insert the value NULL into column ‘col_numtype’, table ‘tempdb.dbo.#sphelpcols___000100000013’; column does not allow nulls. INSERT fails.
The statement has been terminated. If I change the colum to allow null it works on both compatibility modes (65 and 80). But I have the same collation on the tempDB as my user databases so I guess that is why it works. What collation do you have on your tempDB and what collation do you have on your user database and does sp_MShelpcolumns work in this database?

quote:Originally posted by Argyle Now I get this error:
Server: Msg 515, Level 16, State 2, Procedure sp_tabledefinition, Line 59
Cannot insert the value NULL into column ‘col_numtype’, table ‘tempdb.dbo.#sphelpcols___000100000013’; column does not allow nulls. INSERT fails.
The statement has been terminated. If I change the colum to allow null it works on both compatibility modes (65 and 80). But I have the same collation on the tempDB as my user databases so I guess that is why it works. What collation do you have on your tempDB and what collation do you have on your user database and does sp_MShelpcolumns work in this database?

The tempDB & master database have a collation type of SQL_Latin1_General_CI_AS and the 65 database I want to run this against has a collation type of SQL_Latin1_General_CP850_CI_AS. The problem I have is that an SQL 2k database running 65 Compatiblity will not, it appears allow for any collation conversions.

Hi Richard, I can’t think of an elegant way around your problem… With no one replying to your queries so far, I suspect that we’re all in the same boat… The only thing I can think of is setting up a second instance with a matching collation… Cheers
Twan
This is do-able, sp_MSHelpColumns tablename works, but I can’t for the life of me figure out what the difference is between sp_MSHelpColumns and the stored procedure I have written are, apart from the removal of information from the select that is unneeded for what I am trying to do. The main problem I have with sp_MSHelpColumns is that I cannot do the following: insert into #sometable
execute sp_MSHelpColumns tablename The temporary table I need to create would have a definition exceeding the capacity in bytes allowed for a table. (This is mainly because of the two fields with a column name of text). Any help would be greatly appreciated. Regards,
Rik

]]>