answersLogoWhite

0


Best Answer

SQL Server is a great database, and you can get a free developer edition from Microsoft. Stored procedures are a good way to develop your application as they provide several key benefits over inline SQL.

* Precompiled execution plans make for faster data access.

* Use of parameters means less chance for sql injection vulnerabilities.

* Centralised location of data access code means more re-use opportunity.

* Procedures can be easily shared by different applications and languages using the same data store.

This can be used as a pretty generic template for a stored procedure. It always ensures that the old version is removed before creating the new one, and it shows some examples of using SQL Server functions to do variable manipulation: --Remove any existing versions of procedure

IF EXISTS (SELECT name

FROM sysobjects

WHERE name = N'sp_GetArticle'

AND type = 'P')

DROP PROCEDURE sp_GetArticle

GO

--Takes three arguments, the last one is optional

CREATE PROCEDURE sp_GetArticle (@AuthorId int, @DateFrom DateTime, @DateTo DateTime = NULL)

AS

BEGIN

--strip any time portion from the date passed in

SELECT @DateFrom = Convert(datetime,convert(char(10),@DateFrom,101))

--if no 'dateto' provided, then default to 23 hours, 59 minutes and 59 seconds

--this is because dates are stored by default as 00:00:00, and

--we only want to get data for one day

IF @DateTo IS NULL

BEGIN

SET @DateTo = DATEADD ( second , -1, @DateFrom + 1 )

END

SELECT a.Name,

a.Body,

t.Description,

u.Name

FROM Article a

INNER JOIN Topic t on a.TopicId = t.Id

LEFT OUTER JOIN Users u on u.Id = a.AuthorId

WHERE a.AuthorId = @AuthorId

AND a.CreatedDate >= @DateFrom

AND e.CreatedDate <= @DateTo

END

GO

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a stored procedure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can You Write Trigger in Stored Procedures?

A trigger is a stored procedure. It is a special stored procedure that runs in response to some defined event, such as an insert into a table.


Difference between a local procedure and stored procedure?

local procedure wont store in database. Stored procedure store in database permanently and we can use it whenever we require. Other program also can use this stored procedure. And the transaction of stored procedure take care by DBA. But the local procedure transaction is take care by manually only


What is the meaning of the term stored procedure?

A stored procedure is considered a subroutine, which is considered to be only available to the applications with access to a relational database system.


Is it possible to call a stored procedure from java script function and populate an aspnet dropdown list HTML control using the value returned by the SP?

No, a stored procedure can not be called from Javascript. A dropdown list however can be populated using a stored procedure.


What are stored procedures in data bases?

Stored procedure is the pl-sql block in precomplile from and can be used to excecute plsql statement


You created a stored procedure in sql server 2005 database and you called through aspnet page with connection then you are getting error as - Could not find stored procedure 'GetLoginInfo' solve pls?

The connections bruk.


What is the examples of Stored procedure?

sp_helpdb , sp_who2, sp_renamedb are a set of system defined stored procedures. We can also have user defined stored procedures which can be called in similar way.


What is the difference between a stored database procedure and a batch of SQL statements submitted by a client application?

Stored procedures prevent unauthorized access to data


What is the difference between stored procedure and index?

As stored procedure in procedural code, which usually calculates or changes some data. An index is a database object which aids in quick location of records because it is internally ordered in a optimized way for the specific key


How to draw E-R diagram for library management system?

how to draw the er diagram of library management system


What is grivance write a procedure to deal it?

GW...rite!!!


What is the procedure of storing the ac current?

AC current cannot be stored, it must be used as it is generated.