Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variables in the same view that there were declared in

Status
Not open for further replies.

partridge80

Technical User
Nov 26, 2002
12
GB
Forgive me as I am very new to SQL. I've been finding that if you introduce a new variable in a view it seems you cannot use it in that particular view. Is this the case or am I missing something?
 
Unless I misunderstand you, no you are not mistaken. In fact you cannot use variables at all in a view. If you need to execute a query which requires variables (parameters) you will need to use a stored procedure, which allows you to declare the variables and use them.

Hope this helps
 
If you are using SQL Server 2000 then a better option could be to use a User Defined Function. These can operate as the equivalent of a parameterised view and be referenced anywhere that a table or view can be, eg in the FROM clause of a query.

You can't use a parameter in a view and you can't reference a stored procedure in a FROM clause. --James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top