Creating a stored procedure

I would like to create a simple stored procedure in SQL Server 2005, where there is one argument used as a search string for a LIKE clause.

For example to run it I would like to type

spFindThis 'partoffilename'

Where the search string would get inserted into a query such as

SELECT * FROM TheTable WHERE SearchedField LIKE '%@TheArgument%'

In the code snippet I will describe further what I don't understand....


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
	-- Add the parameters for the stored procedure here
	<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
    -- Insert statements for procedure here
	SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
 
 
-- THIS IS OBVIOUSLY WHAT IS CREATED BY DEFAULT WHEN I WANT TO MAKE A STORED PROCEDURE BUT I DON'T REALLY UNDERSTAND WHERE TO PUT THE ARGUMENTS OR WHAT TO PUT IN THE <Procedure_Name, sysname, ProcedureName>  BIT. CAN ANYONE HELP? THANKS

Open in new window

LVL 1
butterhookAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brejkCommented:
You can create the procedure like below but this is not a best practice from security (dynamic SQL) and performance (%something% pattern) point of view.

CREATE PROC dbo.YourProcedureName
@TheArgument nvarchar(100)
AS
SET NOCOUNT ON
EXEC dbo.sp_executesql
  N'SELECT * FROM TheTable WHERE SearchedField LIKE ''%'' + @param + ''%''',
  N'@param nvarchar(100)',
  @param = @TheArgument
GO  
0
brejkCommented:
One more thing about what you have put in the snippet. You have used a template. In Management Studio when you have a script based on a template press Ctrl+Shift+M to replace template parameters with your custom values.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
butterhookAuthor Commented:
Yes this made it much easier, and the good thing about it is that I can see what it wrote in to the code in order to do it manually in future and get  abetter understanding.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.