Can a view be queried with parameters like a stored procedure?

Source: Internet
Author: User

Can a view be queried with parameters like a stored procedure?
---------------------------------------------------------------

Embedded User-Defined Functions

Embedded user-defined functions are a subset of user-defined functions that return table. Embedded functions can be used to implement parameterized views.

See the following view:

Create view vw_customernamesinwa
Select customerid, companyName
From northwind. DBO. MERs
Where region = 'wa'

You can create a more general version of vw_customernamesinregion by replacing where region = 'wa 'With where region = @ regionparameter and letting users specify the viewing area of interest. However, a view does not support parameters specified in the WHERE clause.

Embedded user-defined functions can be used to support the search criteria specified in the WHERE clause. The following is a function example that allows users to specify a region in their selection:

Create Function fn_customernamesinregion
(@ Regionparameter nvarchar (30 ))
Returns table
As
Return (
Select customerid, companyName
From northwind. DBO. MERs
Where region = @ regionparameter
)
Go
-- Example of calling the function for a specific region
Select *
From fn_customernamesinregion (n'wa ')
Go

Embedded user-defined functions follow the following rules:

The returns clause only contains the keyword table. You do not need to define the format of the returned variable because it is set by the format of the result set of the SELECT statement in the return clause.


Function_body cannot be separated by begin and end.


The return clause contains a single SELECT statement in parentheses. The result set of the SELECT statement constitutes the table returned by the function. The Select statements used in nested functions are subject to the same restrictions as the select statements used in views.


The table value function only accepts the constant or @ local_variable parameter.
Embedded functions can also be used to improve the indexing view capability. The index view itself cannot use parameters in its WHERE clause search conditions, and the stored result set needs to be adjusted for specific users. However, you can define the index view that stores the complete dataset matching the view, and then define nested functions on the index view that contains the parameterized search conditions that allow users to adjust their results. If the view definition is complex, most of the work to be performed in the generated result set involves generating an aggregation or joining multiple tables when creating a clustered index on The View. If you create an embedded function that references the view, the function can apply the user's parameterized filtering to extract specific rows generated by the create index statement from the result set. Perform complex aggregation and join operations when executing create index, and then filter rows from the simplified storage results for all queries that reference nested functions. For example:

Defines the view of aggregating all sales data to the result set vw_quarterlysales, which reports the summary sales data of all stores on a quarterly basis.


Create a clustered index on vw_quarterlysales to specify the result set that contains the summarized data.


Create an embedded function for filtering and summarizing data:
Create Function fn_quarterlysalesbystore
(
@ Storeid int
)
Returns table
As
Return (
Select *
From salesdb. DBO. vw_quarterlysales
Where storeid = @ storeid
)

Then, you can select an embedded function to obtain data from a specific store:
Select *
From fn_quarterlysalesbystore (14432)

Most of the work required to meet the query requirements sent out in step 1 will aggregate sales data quarterly. This operation takes one step at 2nd. Each SELECT statement in step 1 uses the fn_quarterlysalesbystore function to filter the aggregated data specific to a user's store.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.