Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 12: Stored Procedures 408 feed or stock quote), even though the structure and complex communications required would have ruled out such a function in prior versions. Without going into too much detail on them for now, let’s look at the syntax for adding an assembly to your database: CREATE ASSEMBLY AUTHORIZATION FROM WITH PERMISSION_SET = [SAFE | EXTERNAL_ACCESS | UNSAFE] The CREATE ASSEMBLY part of things works as pretty much all our CREATE statements have — it indicates the type of object being created and the object name. Then comes the AUTHORIZATION — this allows you to set a context that the assembly is always to run under. That is, if it has tables it needs to access, how you set the user or rolename in AUTHORIZATION will determine whether it can access those tables or not. After that, we go to the FROM clause. This is essentially the path to your assembly, along with the manifest for that assembly. Finally, we have WITH PERMISSION_SET. This has three options: ❑ SAFE: This one is, at the risk of sounding obvious, well . . . safe. It restricts the assembly from accessing anything that is external to SQL Server. Things like files or the network are not available to the assembly. ❑ EXTERNAL_ACCESS: This allows external access, such as to files or the network, but requires that the assembly still run as managed code. ❑ UNSAFE: This one is, at the risk of again sounding obvious, unsafe. It allows your assembly not only to access external system objects, but also to run unmanaged code. I cannot stress enough the risks you are taking when running .NET assemblies in anything other than SAFE mode. Even in EXTERNAL_ACCESS mode you are allowing the users of your system to access your network, files, or other external resources in what is essentially an aliased mode — that is, they may be able to get at things that you would rather they not get at, and they will be aliased on your network to whatever your SQL Server login is while they are making those accesses. Be very, very careful with this stuff. .NET assemblies will be discussed extensively in Professional SQL Server 2008 Programming.

Summary Wow! That’s a lot to have to take in for one chapter. Still, this is among the most important chapters in the book in terms of being able to function as a developer in SQL Server. Sprocs are the backbone of code in SQL Server. We can create reusable code and get improved performance and flexibility at the same time. We can use a variety of programming constructs that you might be familiar with from other languages, but sprocs aren’t meant for everything. Pros to sprocs include: ❑ Usually better performance ❑ Possible use as a security insulation layer (control how a database is accessed and updated) ❑ Reusable code ❑ Compartmentalization of code (can encapsulate business logic) ❑ Flexible execution depending on dynamics established at runtime Cons to sprocs include: Chapter 12: Stored Procedures ❑ Not portable across platforms (Oracle, for example, has a completely different kind of implementation of sprocs) ❑ May get locked into the wrong execution plan in some circumstances (actually hurting performance) Sprocs are not the solution to everything, but they are still the cornerstones of SQL Server programming. In the next chapter, we’ll take a look at the sprocs’ very closely related cousin — the UDF. 409

Chapter 12: Stored Procedures<br />

408<br />

feed or stock quote), even though the structure and complex communications required would have<br />

ruled out such a function in prior versions.<br />

Without going into too much detail on them for now, let’s look at the syntax for adding an assembly to<br />

your database:<br />

CREATE ASSEMBLY AUTHORIZATION FROM <br />

WITH PERMISSION_SET = [SAFE | EXTERNAL_ACCESS | UNSAFE]<br />

The CREATE ASSEMBLY part of things works as pretty much all our CREATE statements have — it indicates<br />

the type of object being created and the object name.<br />

Then comes the AUTHORIZATION — this allows you to set a context that the assembly is always to run<br />

under. That is, if it has tables it needs to access, how you set the user or rolename in AUTHORIZATION<br />

will determine whether it can access those tables or not.<br />

After that, we go to the FROM clause. This is essentially the path to your assembly, along with the manifest<br />

for that assembly.<br />

Finally, we have WITH PERMISSION_SET. This has three options:<br />

❑ SAFE: This one is, at the risk of sounding obvious, well . . . safe. It restricts the assembly from<br />

accessing anything that is external to <strong>SQL</strong> <strong>Server</strong>. Things like files or the network are not available<br />

to the assembly.<br />

❑ EXTERNAL_ACCESS: This allows external access, such as to files or the network, but requires<br />

that the assembly still run as managed code.<br />

❑ UNSAFE: This one is, at the risk of again sounding obvious, unsafe. It allows your assembly not<br />

only to access external system objects, but also to run unmanaged code.<br />

I cannot stress enough the risks you are taking when running .NET assemblies in<br />

anything other than SAFE mode. Even in EXTERNAL_ACCESS mode you are allowing<br />

the users of your system to access your network, files, or other external resources in<br />

what is essentially an aliased mode — that is, they may be able to get at things that<br />

you would rather they not get at, and they will be aliased on your network to whatever<br />

your <strong>SQL</strong> <strong>Server</strong> login is while they are making those accesses. Be very, very<br />

careful with this stuff.<br />

.NET assemblies will be discussed extensively in Professional <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> Programming.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!