Click here to Skip to main content
15,924,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...?
Posted

SQL
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
SQL
SELECT * FROM fn_FunctionName(10)
 
Share this answer
 
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


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

SQL
SELECT *
FROM myview
WHERE bar >= 10
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900