MS SQL updatable Views

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
FILLrate
Posts: 19
Joined: Thu 28 Jan 2010 09:14

MS SQL updatable Views

Post by FILLrate » Sat 08 Jun 2013 20:26

MS SQL Server supports updatable views.
For example, we have a table TCustomers (Fid, FName) and view VCustomers (select Fid, FName from TCustomers)
So, we can write

Code: Select all

update VCustomers set FName = "Igor" where FId = 18 
and it will work. It’s very useful feature when user have restricted access to the tables (deny select / insert / update / delete for table TCustomers, but allow to do it with view VCustomers).

So, the problem is SDAC replaces the name of the view with appropriate table. If I write query like

Code: Select all

select * from VCustomers,
and try to modify data, SDAC will generate code like

Code: Select all

update TCustomers set FName = "Igor" where FId = 18. 
VCustomers was replased with TCustomers :-(
Of course the server will reject the transaction, because user doesn’t have permission to edit table TCustomers.
I wrote about a similar problem before (SQL Generator doesn't want to update VIEW http://forums.devart.com/viewtopic.php?f=6&t=24136)

I have to reading the forum and a little digging in the Sdac source code. So I found solution.
When query is opened, I manually replace table TCustomers with view VCustomers

Code: Select all

TDBAccessUtils.GetTablesInfo (TCustomDADataSet (DataSet)). Items [0]. TableName: = 'VCustomers'; 
After that sdac generated code is true:

Code: Select all

update VCustomers set FName = "Igor" where FId = 18 and. 
The question in the following. Is it possible to disable replacing view with table? If not, how can I get origin view name (VCustomers) for a query instead of manually replacing it (TDBAccessUtils...TableName: = 'VCustomers'). May be sdac has a function like GetOrignUpdateTable? :-)

AndreyZ

Re: MS SQL updatable Views

Post by AndreyZ » Mon 10 Jun 2013 07:06

You can avoid this problem by providing your own SQL statements in the SQLUpdate, SQLInsert, SQLDelete, etc. properties. For example:

Code: Select all

MSQuery.SQL.Text := 'select * from VCustomers';
MSQuery.SQLUpdate.Text := 'update VCustomers set FName = :FName where FId = :FId';
For more information, please read the SDAC documentation.

FILLrate
Posts: 19
Joined: Thu 28 Jan 2010 09:14

Re: MS SQL updatable Views

Post by FILLrate » Tue 11 Jun 2013 19:35

We discussed your suggestion in my previous request. I don’t want to set SQLUpdate, SQLInsert, SQLDelete because:
1. It’s require to write down a lot of sql code inside of client application.
2. SQLUpdate, SQLInsert require to define all column. It’s unacceptable because user change only small part of them and it’s not necessary to send to server all column values (especially long string/blobs).
3. Writing my own dynamic sql generator does not make sense, because it was already done in sdac.

I need only SUPPORT OF EDITABLE MS SQL VIEW BY SDAC without writing down tons of code. I found one of the possible solutions, but as I described above it’s not so convenient for me.

AndreyZ

Re: MS SQL updatable Views

Post by AndreyZ » Wed 12 Jun 2013 11:28

To generate SQL statements automatically, SDAC uses the meta information that is returned by SQL Server. When you use the query

Code: Select all

select * from VCustomers
, SQL Server returns the table name TCustomers. That is why auto-generated SQL statements use the TCustomers table name instead of VCustomers.
We will investigate this problem and ways to avoid it.

FILLrate
Posts: 19
Joined: Thu 28 Jan 2010 09:14

Re: MS SQL updatable Views

Post by FILLrate » Fri 14 Jun 2013 13:24

Ok, thx.

AndreyZ

Re: MS SQL updatable Views

Post by AndreyZ » Wed 19 Jun 2013 06:59

If any other questions come up, please contact us.

Post Reply