如何从视图传递值? [英] How to pass values from view...?

查看:58
本文介绍了如何从视图传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我和工会有三张桌子.

我想从视图中传递参数.

例如,

我有3条选择语句.

从TAB1中选择否,名称
UNION
从TAB2中选择No,Name,Class
UNION
从TAB3中选择部门,地点"

最后,我将视图名称创建为View1.

我的查询是,

我想在创建视图后通过where条件.

例如,

从View1中选择*,其中TAB1.No =''''

你能告诉我上述条件是否可能.如果不是这样的话,如何通过?...

Hi,

I have three table with union.

I would like to pass the parameter from the views.

For Example,

I have 3 select statement.

Select No,Name from TAB1
UNION
Select No,Name,Class from TAB2
UNION
Select Dept,Place from TAB3

Finally i have created view name as View1.

My query is,

I would like to pass the where condition after creating view.

For Example,

select * from View1 where TAB1.No=''''

Can you tell me whether the above condition is possible. if not how to pass like the above condition...?

推荐答案

CREATE FUNCTION fn_FunctionName (@NO INT)
RETURNS TABLE
AS
RETURN
    (Select No,Name from TAB1 where No=@NO
    UNION
    Select No,Name,Class from TAB2 where No=@NO
    UNION
    Select Dept,Place   from TAB3 where No=@NO)
Go


用这种方式


Use it this way

SELECT * FROM fn_FunctionName(10)


示例取自此处
MSDN:创建视图 [
The example is taken from here
MSDN: Create View[^]
This example uses the @@ROWCOUNT function as part of the view definition.
USE pubs
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS
      WHERE TABLE_NAME = ''myview'')
   DROP VIEW myview
GO
CREATE VIEW myview
AS
   SELECT au_lname, au_fname, @@ROWCOUNT AS bar
   FROM authors
   WHERE state = ''UT''
GO
SELECT * 
FROM myview



当视图创建虚拟表时,您可以对该视图执行大多数SQL操作,例如下面的简单示例



As a view creates a virtual table you can then do most SQL operations on that view such as the simple example below

SELECT *
FROM myview
WHERE bar >= 10


这篇关于如何从视图传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆