Stored Procedure Problem

5 pts.
Tags:
SQL
Stored Procedures
Hi Greg, I am using SQL queries as in-script and now decided to use them as stored procedure. I am facing some problem while converting them into stored procedures. It’s a book search functionality based on book name and author name. My Old SQL query, which I was using with asp. SQL = “Select * From Tbl_Name Where Name LIKE '%xyz%' ” & strOrderBy Where strOrderBy is provided by user input at the time of search, so that they can sort book listing as they want. Variable strOrderBy, is asp variable and its value being determined by user at the time of search. I was passing the value of strOrderBy like this. Select Case strOrderByInput Case "pubdateasc" strOrderBy = "Order by publication DESC” Case "bookname" 'strOrderBy = " Order by BookName ASC " Case "Author" 'strOrderBy = " Order by AuthorName ASC " Case Else strOrderBy = " Order by publication ASC" End Select The whole thing is working fine when im using this in ASP as in-line query, but when I changed it into stored procedure. I am not able to pass strOrderBy variable as I was doing it earlier. Please can you help me how to do this in stored procedure? Regards Anil

Answer Wiki

Thanks. We'll let you know when a new response is added.

In order to have a dynamic order by you have two options.

1. Use dynamic SQL.
2. Use a branching storage procedure.

Option 1:
<pre>CREATE PROCEDURE MyProc
@OrderBy NVARCHAR(30)
AS
DECLARE @SQL NVARCHAR(4000)
SET @SQL = N“Select * From Tbl_Name Where Name LIKE ‘%xyz%’ ” + @OrderBy
exec (@SQL)
GO</pre>

Option 2:
<pre>CREATE PROCEDURE MyProc
@OrderBy INT
AS
IF @OrderBy = 1
SELECT * FROM Tbl_Name WHERE Name LIKE ‘%xyz%’ ORDER BY publication DESC
IF @OrderBy = 2
SELECT * FROM Tbl_Name WHERE Name LIKE ‘%xyz%’ Order by BookName ASC
IF @OrderBy = 3
SELECT * FROM Tbl_Name WHERE Name LIKE ‘%xyz%’ Order by AuthorName ASC
IF @OrderBy = 4
SELECT * FROM Tbl_Name WHERE Name LIKE ‘%xyz%’ Order by publication ASC
GO</pre>

The problem with dynamic SQL is there are security risks with using it.
The problem with the branching code is that SQL can’t do much with caching the execution plan (especially in SQL 2000 and below).

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following