If you want to retrieve the SQL code that created a given view, the query is:
SELECT
m.definition
FROM
sys.views v
INNER JOIN sys.sql_modules m ON m.object_id = v.object_id
WHERE
name = 'YourViewName'
Then you will have to transform it so that you can use variables. For example:
SQL query:
SELECT
Name
,Title
,City
,State
FROM
Contacts
WHERE
City = @city
AND Title IN ('Master', 'Trade')
OR Name <> @name
Usage:
string sqlQuery = "SELECT Name, Title, City, State FROM Contacts WHERE City = @city AND Title IN ('Master', 'Trade') OR Name <> @name";
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sqlQuery, cn))
{
cmd.Parameters.AddWithValue("@city", "New Jersey");
cmd.Parameters.AddWithValue("@name", "Mark");
}
}
The WHERE clause is ambiguous; do you mean
WHERE
(City = @city AND Title IN ('Master', 'Trade'))
OR Name <> @name
or
WHERE
City = @city
AND (Title IN ('Master', 'Trade') OR Name <> @name)
?
Hope this helps.