dbTalk Databases Forums  

Setting up a store procedure...

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Setting up a store procedure... in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
JJ297
 
Posts: n/a

Default Setting up a store procedure... - 12-20-2007 , 10:13 AM






I'm developing a library and want to display the alphabets across the
screen. When a user clicks on one of the alphabets I want all titles
beginning with that letter to appear on the screen.

How would I write this stored procedure?

My table is called Titles

Fields:
Title ID
Titles

Thanks!

Reply With Quote
  #2  
Old   
Anith Sen
 
Posts: n/a

Default Re: Setting up a store procedure... - 12-20-2007 , 01:19 PM






Not sure how involved this requirement is. If you want to get the list of
alphabets from the existing titles, you can have simple query like:

SELECT DISTINCT LEFT( titles, 1 ) AS "alpha"
FROM tbl
ORDER BY "alpha" ;

In many cases, for fast searching and indexing, people implement other
alternatives. One variation is to have another table that is referenced back
to the titles table. Another one with smaller list could be a computed
column that is indexed with the expression: LEFT( titles, 1 ) etc.

If you want all the alphabets displayed regardless of existing titles, you
can easily generate them on the client side itself. Most web development
tools have build in functions that does this in a quick loop. If you want to
do this with t-SQL, you can use the CHAR() function on a simple query like:

SELECT CHAR( n )
FROM Nbrs
WHERE n BETWEEN 65 AND 90 ;

Regarding how the table Nbrs is created, see:
www.projectdmx.com/tsql/tblnumbers.aspx

--
Anith



Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Setting up a store procedure... - 12-20-2007 , 01:51 PM



You can simply pass the letter selected by the user as a parameter to the
stored procedure and then pull the list. The stored procedure can be
something like this:

CREATE PROCEDURE GetTitles(
@letter NVARCHAR(1))
AS
SELECT title_id, title
FROM Titles
WHERE title LIKE @letter + '%'
ORDER BY title

Anith has a good suggestion to create a computed column on LEFT(title, 1)
that is indexed. Then the WHERE filter will be computed_column = @letter.

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #4  
Old   
JJ297
 
Posts: n/a

Default Re: Setting up a store procedure... - 12-20-2007 , 02:26 PM



On Dec 20, 2:51 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
You can simply pass the letter selected by the user as a parameter to the
stored procedure and then pull the list. The stored procedure can be
something like this:

CREATE PROCEDURE GetTitles(
@letter NVARCHAR(1))
AS
SELECT title_id, title
FROM Titles
WHERE title LIKE @letter + '%'
ORDER BY title

Anith has a good suggestion to create a computed column on LEFT(title, 1)
that is indexed. Then the WHERE filter will be computed_column = @letter.

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thanks Plamen this is easy enough and it works! Thanks Anith for your
suggestion too!


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.