简体   繁体   中英

SQL error when modifying table design from sql management studio

I've got a rather large table (20+ columns) on an SQL server 2008. I'm using Microsofts SQL Server Management Studio to open the tables design view and add a column. After adding the column i move it up in the column sorting.

The image below shows the column i added and where i'm trying to move it to by just dragging it a few places up.

圈出的栏正在添加并向上移动

After i've done this i'm getting an exception when i'm trying to open up the website. Everything works fine when i add the column without moving it up in the column sorting.

Can someone help me figuring out this problem. Is this an bug in MSSQL server, the management studio or is something else going wrong?

The exception

Operand type clash: bit is incompatible with uniqueidentifier

The stacktrace:

[SqlException (0x80131904): Operand type clash: bit is incompatible with uniqueidentifier]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +404
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +412
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1363
   System.Data.SqlClient.SqlDataReader.HasMoreRows() +301
   System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) +422
   NHibernate.Driver.NHybridDataReader.Read() +28
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +1383
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +114
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +195

[ADOException: could not execute query
[ SELECT * from SomeFunction(@p0,@p1) ]
  Name:Id - Value:3429fb7e-dba3-4c74-b41b-6f2e0bbb33f8  Name:Moment - Value:7-1-2011 12:16:45
[SQL: SELECT * from SomeFunction(@p0,@p1)]]
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +637
   NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +23
   NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results) +438
   NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results) +373
   NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters) +340
   NHibernate.Impl.SqlQueryImpl.List() +258
   CMS.ResourceAccess.DataAccessLogic.Repositories.NodeRepository.GetAncestors(Guid nodeId) in C:\Projects\Website\DataAccessLogic\Repositories\Repository.cs:228
   CMS.Business.Components.Services.NodeService.GetAncestors(Guid nodeId) in C:\Projects\Website\DataAccessLogic\Repositories\Service.cs:921
   CMS.Business.Components.Services.NodeService.GetSiteByNodeId(Guid nodeId) in C:\Projects\Website\DataAccessLogic\Repositories\Service.cs:1280
   Plugin.Wysiwyg.Business.Components.Services.WysiwygSearchService.RebuildIndex() +1232
   CMS.Business.Components.Services.SearchService.RebuildIndexForSites(IEnumerable 1 sites, ConfigurationManager configurationManager) in C:\Projects\Website\Services\Service.cs:303
   CMS.Business.Components.Services.SearchService.RebuildIndex() in C:\Projects\Website\DataAccessLogic\Repositories\Service.cs:252
   CMS.Backend.MvcApplication.Application_Start() in C:\Projects\Website\Global.asax.cs:49

[HttpException (0x80004005): could not execute query
[ SELECT * from SomeFunction(@p0,@p1) ]
  Name:Id - Value:3429fb7e-dba3-4c74-b41b-6f2e0bbb33f8  Name:Moment - Value:7-1-2011 12:16:45
[SQL: SELECT * from SomeFunction(@p0,@p1)]]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +3988565
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +325
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375

[HttpException (0x80004005): could not execute query
[ SELECT * from SomeFunction(@p0,@p1) ]
  Name:Id - Value:3429fb7e-dba3-4c74-b41b-6f2e0bbb33f8  Name:Moment - Value:7-1-2011 12:16:45
[SQL: SELECT * from SomeFunction(@p0,@p1)]]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11529072
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4784373

EDIT: I'm using NHibernate as ORM

There's a SELECT * lurking somewhere in your SQL code (possibly in the function, or a view it relies on), by my reckoning. This is just one of many reasons SELECT * is dangerous. Here's an example of something similar, to demonstrate one such failure mode:

create table dbo.T (
    ID int not null,
    FilterID uniqueidentifier not null,
    Val1 varchar(10) not null
)
go
insert into dbo.T (ID,FilterID,Val1)
select 1,'00000000-0000-0000-0000-000000000000','abc'
go
create view dbo.V
as
    select * from dbo.T
go
create function dbo.F ()
returns table
as
    return (select ID,Val1 from dbo.V where FilterID='00000000-0000-0000-0000-000000000000')
go
select * from dbo.F()
go

The above returns a single result row, as expected. Now we perform the change the same way SSMS will be doing, behind the scenes:

create table dbo.Temp_T (
    ID int not null,
    Flag bit null,
    FilterID uniqueidentifier not null,
    Val1 varchar(10) not null
)
go
insert into dbo.Temp_T (ID,FilterID,Val1)
select ID,FilterID,Val1 from dbo.T
go
drop table dbo.T
go
sp_rename 'dbo.Temp_T','T'
go

And now we query our F function again:

select * from dbo.F()

And we get:

Conversion failed when converting the varchar value '00000000-0000-0000-0000-000000000000' to data type bit.


In fact, if I modify the F function to be:

create function dbo.F ()
returns table
as
    return (select ID,Val1 from dbo.V where FilterID=CONVERT(uniqueidentifier,'00000000-0000-0000-0000-000000000000'))
go

I can get:

Operand type clash: uniqueidentifier is incompatible with bit

How are you accessing your table from code?? The error would indicate that somewhere in your code, you access the table and your code depends on the sequence of the columns.

Since you moved away your column to another place, now suddenly your code is trying to access column no. 17 (or whatever) and assumes that this is a uniqueidentifier column - but that's no longer the case, since you reorganized your table's column order....

Simply recompile all views, stored procedures, UDFs, and TVF's that reference this table.

SQL Server converts the column names to numbers internally. When you shift columns around, this causes unexpected conditions when cached query plans are run.

I believe you can also just restart SQL Server to cause it to flush all plans. I haven't tried this, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM