Can we pass a parameter to a view in Microsoft SQL Server?
I tried to create view
in the following way, but it doesn't work:
create or replace view v_emp(eno number) as select * from emp where emp_id=&eno;
Can we pass a parameter to a view in Microsoft SQL Server?
I tried to create view
in the following way, but it doesn't work:
create or replace view v_emp(eno number) as select * from emp where emp_id=&eno;
no you can pass the parameter to the procedure in view
we can write a stored procedure with input parameters and then use that stored procedure to get a result set from the view. see example below.
the stored procedure is
and the view from which we can get the result set is
As already stated you can't.
A possible solution would be to implement a stored function, like:
This allows you to use it as a normal view, with:
There are 2 ways to acheive what you want unfortunatly neither can be done using a view.
You can either create a table valued user defined function that takes the parameter you want and returns a query result
Or you can do pretty much the same thing but create a stored procedure instead of a user defined function.
For Example
the stored procedure would look like
Or the user defined function would look like
Here is an option I have not seen so far:
Just add the column you want to restrict on to the view:
A view is nothing more than a predifined 'SELECT' statement. So the only real answer would be: No, you cannot.
I think what you really want to do is create a stored procedure, where in principle you can use any valid SQL to do whatever you want, including accept parameters and select data.
It seems likely that you really only need to add a where clause when you select from your view though, but you didn't really provide enough details to be sure.