Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Blastrix
Posting Yak Master
208 Posts |
Posted - 2003-11-14 : 16:52:52
|
I have the following procedure which works correctly on one server, but incorrect on another server. The procedure queries INFORMATION_SCHEMA views and some system tables to return the columns in a table, if they're part of a primary key, and their description.Create Procedure dbo.sp_SelectTableColumns ( @Schema varchar(255), @TableName varchar(255), @IncludePrimaryKeyColumns bit = 1, @IncludeNonPrimaryKeyColumns bit = 1 )AsSELECT ColumnName = C.COLUMN_NAME, IsPrimaryKeyColumn = IsNull(KEYS.IsPrimaryKeyColumn, 0), ColumnDescription = Convert(nvarchar(4000), SP.value)FROM INFORMATION_SCHEMA.COLUMNS C LEFT OUTER JOIN sysobjects SO ON C.TABLE_SCHEMA = user_name(SO.uid) AND C.TABLE_NAME = SO.name LEFT OUTER JOIN sysproperties SP ON SO.id = SP.id AND C.ORDINAL_POSITION = SP.smallid AND SP.type = 4 AND SP.name = 'MS_Description' LEFT OUTER JOIN ( SELECT KCU.COLUMN_NAME, IsPrimaryKeyColumn = 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME WHERE TC.TABLE_SCHEMA = @Schema AND TC.TABLE_NAME = @TableName AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY' ) KEYS ON C.COLUMN_NAME = KEYS.COLUMN_NAMEWHERE C.TABLE_SCHEMA = @Schema AND C.TABLE_NAME = @TableName AND IsNull(KEYS.IsPrimaryKeyColumn, 0) = CASE WHEN @IncludePrimaryKeyColumns = 1 AND @IncludeNonPrimaryKeyColumns = 1 THEN IsNull(KEYS.IsPrimaryKeyColumn, 0) ELSE @IncludePrimaryKeyColumns END Assuming the table below:CREATE TABLE dbo.tblJunk ( JunkKey int NOT NULL IDENTITY (1, 1), Junk varchar(50) NOT NULL ) ON [PRIMARY]GOALTER TABLE dbo.tblJunk ADD CONSTRAINT PK_tblJunk PRIMARY KEY CLUSTERED ( JunkKey ) ON [PRIMARY]GODECLARE @v sql_variant SET @v = N'This is the PK.'EXECUTE sp_updateextendedproperty N'MS_Description', @v, N'user', N'dbo', N'table', N'tblJunk', N'column', N'JunkKey'GODECLARE @v sql_variant SET @v = N'This is not the PK.'EXECUTE sp_updateextendedproperty N'MS_Description', @v, N'user', N'dbo', N'table', N'tblJunk', N'column', N'Junk'GO Given the following queries/output GoodServer is the correct server, BadServer is the one that doesn't work.--Return only primary key columnsexec sp_SelectTableColumns 'dbo', 'tblJunk', 1, 0GoodServer:JunkKey 1 This is the PK.BadServer:JunkKey 1 This is the PK.Junk 1 This is not the PK.--Return only non PK columnsexec sp_SelectTableColumns 'dbo', 'tblJunk', 1, 0GoodServer:Junk 1 This is not the PK.BadServer:Nothing--Return all columnsexec sp_SelectTableColumns 'dbo', 'tblJunk', 1, 1GoodServer:JunkKey 1 This is the PK.Junk 1 This is not the PK.BadServer:NothingAs you can see from the results above, something is really wrong with the BadServer. What's interesting is in the query where only PKs should be returned, the bad server returns both columns, and indicates that the non PK column is a PK. If ISNULL(KEYS.IsPrimaryKeyColumn, 0) is replaced with only KEYS.IsPrimaryKeyColumn, the non PK column shows it's IsPrimaryKeyColumn value as NULL.So first I want to figure out what the heck is going on here? Second, how does a value that's NULL, get it's value set to 1, when the only place it's value can be altered(ISNULL check), it would be changed to 0?Am I missing a server or database setting somewhere? As mentioned, this works flawlessly on another machine.Thanks,Steve |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-11-14 : 17:05:07
|
| Different service pack levels on the servers?Tara |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-14 : 17:40:56
|
| Interesting what sp_pkeys @table_name='authors' will show on the BadServer? |
 |
|
|
Blastrix
Posting Yak Master
208 Posts |
Posted - 2003-11-14 : 18:06:06
|
| Tara, you rock as usual! We had just thrown up that SQL Server today to do some general testing, and it seems that in the process someone forgot to put on the latest and greatest SP(oops).Thanks |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2003-11-14 : 18:50:38
|
| Tara, the all knowing SQL Warrior Princess to the rescue!We are all lucky to have you around on these boards Tara!Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-11-14 : 18:52:19
|
Tara |
 |
|
|
Blastrix
Posting Yak Master
208 Posts |
Posted - 2003-11-14 : 20:12:22
|
| Does anyone know exactly what bug(I'm assuming it was a bug) it was that was fixed that was causing this, and in which service pack? |
 |
|
|
|
|
|
|
|