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!

Local Variables - benefits 1

Status
Not open for further replies.

dbomrrsm

Programmer
Feb 20, 2004
1,709
GB
Probably an really simple answer to this and probably one I should know !!

The below two statements return the same results so what is the value of using the local variable in the first ?

USE pubs
DECLARE @find varchar(30)
SET @find = 'Ring%'
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE @find

USE pubs
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE 'Ring%'

Thanks

DBomrrsm
 

In your examples, thier is no difference, both will run exactly the same.

Where Variables come in very useful in the above context is in Stored Procedures.

If you want @find to be dynamic, then create it as a parameter for a stored procedure, then each time you run the Stored Proc, you can pass in any value you want to @Find

e.g. (run this in query analyser)

Code:
CREATE PROCEDURE proc_find
(@Find varchar(200))
AS

SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE @find + '%'

GO

To execute (then running this, you can pass any value in you want)

Code:
EXEC proc_find 'Ring'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top