I've been learning about stored procedures and calling them from ASP pages. So just wanted to jot down some examples/templates…
(note from my technical editor, Mike: This code is specific to MS SQL Server and probably wouldn't work with other databases.)
Stored Procedure:
/*set DataBase*/
Use enterDatabaseNameHere
/*Check to see if table exists*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[enterTableNameHere]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[enterTableNameHere]
GO
/*Create Table*/
CREATE TABLE [dbo].[enterTableNameHere] (
[firstname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[lastname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[address1] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[city] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[state] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[zip] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[phonenumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[emailaddress] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[timestamp] [datetime] NOT NULL,
[idnumber] [int] IDENTITY (1, 1) NOT NULL
) ON [PRIMARY]
GO
/*Grant permission On table */
GRANT SELECT , UPDATE , INSERT , DELETE ON [dbo].[enterTableNameHere] TO [enterDBUserNameHere]
GO
/********************************************************/
/*Create Stored procedure that inserts data */
/*Check to see if it already exists*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[enterStoredProcedureNameHere]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[enterStoredProcedureNameHere]
GO
/*Create Stored Procedure that inserts data */
CREATE PROCEDURE enterStoredProcedureNameHere
@vcfirstname varchar (50),
@vclastname varchar (50),
@vcaddress1 varchar (50),
@vccity varchar (50),
@vcstate varchar (50),
@vczip varchar (50),
@vcphonenumber varchar (50),
@vcemailaddress varchar (50)
As
DECLARE @dttimestamp datetime
SET @dttimestamp=Getdate()
INSERT INTO enterTableNameHere
(firstname, lastname, address1, city, state, zip, phonenumber, emailaddress, timestamp)
VALUES (@vcfirstname, @vclastname, @vcaddress1, @vccity, @vcstate, @vczip, @vcphonenumber, @vcemailaddress, @dttimestamp)
GO
/*Grant permission On Stored Procedure */
GRANT Execute On [dbo].[enterStoredProcedureNameHere] To [enterUserNameHere]
GO
/********************************************************/
/*Create stored procedure that selects data */
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[enterSelectingStoredProcedureNameHere]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[enterSelectingStoredProcedureNameHere]
GO
CREATE PROCEDURE enterSelectingStoredProcedureNameHere
AS
SELECT * from enterTableNameHere
GO
/* Grant permission On stored procedure */
GRANT Execute On [dbo].[enterSelectingStoredProcedureNameHere] To [enterDBUserNameHere]
GO
Run all that in the SQL Query Analyzer in your database. You'll only need to do that once.
Calling/Using the stored procedure from your ASP page: