SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using Views


Using Views

Author
Message
Steve Jones
Steve Jones
SSC Guru
SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)SSC Guru (249K reputation)

Group: Administrators
Points: 249375 Visits: 19809
Comments posted to this topic are about the item Using Views

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
xsevensinzx
xsevensinzx
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8209 Visits: 3507
Believe it or not, but this is taught in a course I just took for the SQL Server 2008 R2: Database Developer certification. It's covered with creating and altering views.

In my case, it was covered when defining uses for indexed views. Some applications hard code access to specific tables. A view can be created to replace the old object with the new structure and suffer little to no downtime from the application.

I myself have not practiced this technique, but it did catch my attention when I heard it from the course. That's because it surely sounded like a best practice that I'm sure everyone should pick up on.

Here are my notes from that particular section being I have it up.


What can you use indexed views for?

1. Improve Performance
1a. You can have indexed views compute and store data to improve performance.

2. Filter or Restrict Access To Data
2a. Views can be used to filter or restrict access to data because you specify which columns are included in a view in a defining query.

3. Manipulate Data
3a. If you often need to execute queries that require one or more tables to be joined together, you can use a view to always to it.

4. Restructure Tables
4a. Some applications hard-code access to specific tables. View can be created in replace of the old object with the new structure.

5. Customize Data
5a. Views can be used to customize data for the user viewing it.

6. Importing and Exporting Data
6a. You can define the data you wish to export in the views definition. The BCP utility can then use the view as the source.


Gary Varga
Gary Varga
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38282 Visits: 6562
This editorial highlights a key reason why I believe in using stored procedures. All I want from a stored procedure is to answer some question (no, not query) or to store a set of data (no, not a data set). I don't care how these happen and that gives power to those either side of the stored procedure interface definition. Anything can change on either side as long as the contract remains unbroken.

As a systems developer that often has a RDBMS at the back end, I want to give as much leeway to DBAs to do performance tuning as well as allowing for multiple system access to a single database.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
call.copse
call.copse
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7428 Visits: 2089
In light of the QOTD is anyone using synonyms for such purposes yet or would they not work for that? Don't know much about them.

Views have been soured for me by a particular project (where they were done very wrong), but I guess they could have a place if done correctly - I might consider them for a project I have coming up.

The using all stored procedures to define the interface thing is fine but it can put an awful lot of business logic into fairly impenetrable code. It's got some advantages, but the lack of ability to do things in a very well structured way puts me off that course of action (as a developer, without any access to DBA services). Of course they always have a place in my book for some jobs - just not simple CRUD stuff.

Nice win for our lads at the Amex on another note Gary!
Jo Pattyn
Jo Pattyn
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13082 Visits: 10244
Synonyms work in Oracle, anxious to test them out in MS SQL Server.
fregatepllada
fregatepllada
Say Hey Kid
Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)Say Hey Kid (668 reputation)

Group: General Forum Members
Points: 668 Visits: 322
If I am not mistaken, it works only when View built on top of a SINGLE table and includes table's PK column(s)?
Gary Varga
Gary Varga
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38282 Visits: 6562
call.copse (8/1/2014)
...Nice win for our lads at the Amex on another note Gary!


Goes to show how unlucky you were not to get the League 1 silverware those few years ago. [Sorry everyone for going off topic]

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
lshanahan
lshanahan
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2571 Visits: 438
fregatepllada (8/1/2014)
If I am not mistaken, it works only when View built on top of a SINGLE table and includes table's PK column(s)?


Only if you try an update directly through the view. If you define appropriate triggers you can do multi-table updates. Triggers have their own issues, and if you think about it, by the time you write the logic for those, you've probably written a fair piece of a stored procedure.

I inherited a database with an Access front-end that was linked directly to the tables (you may wince now) and I used views that mimicked the exsiting tables to de-couple the tables from Access so I could re-work the schema without blowing up the whole thing. Stored procedures can be used with Access via pass-through queries but there are some extra gymnastics since SQL Server doesn't know a thing about the Access object model. Views on the other hand act the same as a linked table.

It's still a work in progress with the eventual goal of moving to a tiered architecture, but using views has certainly simplified the process - not to mention allowing me to learn a good bit of SQL in the process.

This isn't to say you should or souldn't use views. Like all things SQL Server, they're another tool in the toolbox and you have to evaluate when, where and how to use them.

____________
Just my $0.02 from over here in the cheap seats of the peanut gallery - please adjust for inflation and/or your local currency.
Xavon
Xavon
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1140 Visits: 452
We have leveraged this option to some degree. But our system also uses replication, and each new view is another article of replication we must add. We are already over the recommended limit, so our DBA pushes back hard any time we try to add more.
SQLHammer
SQLHammer
SSC Veteran
SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)SSC Veteran (283 reputation)

Group: General Forum Members
Points: 283 Visits: 225
I personally embrace project based deployment methods such as the use of SSDT and dacpacs. I think that this method would prove a challenge in that environment because you'd have to create more than one build of the dacpac and deploy them in stages. The alternative would be to put everything in the pre-scripts but then you lose the value that is gained with that type of project.

For implementations that aren't doing this and prefer to execute hand-made scripts, I find it a useful tactic.

Derik Hammer
@SQLHammer
www.sqlhammer.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search