简体   繁体   中英

MSCRM, sql select error invalid column name

trying to get some data from Account in MSCRM 8 onpremise

we have sql query something like this:

select distinct top 100 acc.accountid,acc.OwnerId, ...,  acc.v_ownerid 
from account acc
where acc.statecode = 0 and (1=0 or acc.accountid= xxxx)

When I Try to run this select from my C# code I am getting an error:

System.Data.SqlClient.SqlException: Invalid column name 'acc.v_ownerid'.

The field 'acc.v_ownerid' for 100% exists in DB

  1. If I will run this select in Sql Management studio it will give me result
  2. If I run SQL profiler I will get sql query, which also returns result
  3. If I will run it from C# code, It will give me error above, if this field is removed it will work, but I need it..

Please where can be the problem?

Petr

With MSCRM on-prem running SQL queries against the tables is unsupported.

Microsoft supports only two operations against an MSCRM SQL database:

  1. Creating custom indexes
  2. Running SELECT queries against the filtered views... eg FilteredAccount.

In addition to providing the text values for lookups and option sets, the filtered views also enforce security at the database level.

You might want to refactor your SQL to use the FilteredAccount view and see how you make out with that.

Also, please note that the Account table is called AccountBase.

帐户库

The database object named Account is actually a view (which it is also unspported to query, as it is not a Filtered view.)

帐户视图

The only supported way to query for Accounts in SQL is via the FilteredAccount view:

过滤帐户

Well, the solution is to use this select

Select distinct top 100 acc.accountid,acc.OwnerId, ...,  acc.v_ownerid from 
X.dbo.account acc
where acc.statecode = 0 and (1=0 or acc.accountid= xxxx)

instead of:

select distinct top 100 acc.accountid,acc.OwnerId, ...,  acc.v_ownerid 
from account acc
where acc.statecode = 0 and (1=0 or acc.accountid= xxxx)

Then it returns all columns, do not know why, but it is working...

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