Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 displaying related names by passing parameter

Author  Topic 

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 03:25:22
Hi all
I have a table called books.

The columns are bookid,bookname.

I want to write a stored procedure.

The parameter is @partname.

for example if i give @partname = 'ram'
then all books which are related to 'ram' should display

like
bookname
-----------
The Ramaian
Ramaian by molla
Ram Ram Ram
The Ramayanam by valmiki

can any one help me!


Thanks.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 04:24:53

Select bookname from table
where bookname like '%ram%'

Note that this wont make use of index if it is defined on bookname column

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 04:31:14
As you use parameter it should be

Select bookname from table
where bookname like '%+partname+'%'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 04:47:52
Hi Madhivanan

I tried it.
But it is not working.

can u please check it once again.

thank you

quote:
Originally posted by madhivanan

As you use parameter it should be

Select bookname from table
where bookname like '%+partname+'%'


Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 05:01:12
Did you get any error?
Did you get unexpected error?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 05:04:00
No I did not get any error.
but it displays nothing
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 05:06:34
I think some error is there in your query.

like '%+partname+'%'


quote:
Originally posted by madhivanan

Did you get any error?
Did you get unexpected error?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 05:11:33
Madhavan

Here I am writing my sp. please check it! correct or not !

create procedure bookpart
@part varchar(100)
as
begin
select bookname from books
where bookname like '%+@part+%'
end


quote:
Originally posted by madhivanan

Did you get any error?
Did you get unexpected error?

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 05:26:07
1 My name is Madhivanan
2 You didnt see my code correctly your sp code should be now

create procedure bookpart
@part varchar(100)
as
begin
select bookname from books
where bookname like '%'+@part+'%'
end

See how it differs from yours

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

js.reddy
Yak Posting Veteran

80 Posts

Posted - 2007-07-12 : 05:31:47
Thank you Madhivanan
Now it is ok.

quote:
Originally posted by madhivanan

1 My name is Madhivanan
2 You didnt see my code correctly your sp code should be now

create procedure bookpart
@part varchar(100)
as
begin
select bookname from books
where bookname like '%'+@part+'%'
end

See how it differs from yours

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 06:24:40
I hope you know how to execute the SP

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -