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
 General SQL Server Forums
 New to SQL Server Programming
 store procedure to insert values into 3 tables..

Author  Topic 

bluestar
Posting Yak Master

133 Posts

Posted - 2008-09-03 : 11:22:59
Hello

I am very new to write SP,so need guidance in doing the following.

I need to write a stored procedure,which will insert data into 3 tables.

These are the 3 table

Table-TOCHeaderSchema
Columns-TOCHeaderSchemaID
-TocHeaderScemaNameText
-CreatedByLID

Table-TOCHSElement
Columns-TOCHSElementID
-TOCHSLevel
-TOCHSPrefixText
-TOCHSCounterStyleID
-TOCHeaderSchemaID

Table-TOCHSCounterStyle
Columns-TOCHSCounterStyleID
-TOCHSCounterNumber----this is autogenerated
-TOCHSCounterLabelText


Basically these tables are changing the numbering of elements.

like for example.

this is the numbering of a document named Transportation TP 1.1.1
so this will be changed to 1.A.1 or 1.a.1 depending upon the choice.

so the table TOCHSCounterStyle is like
TOCHSCounterStyleID ---1
TOCHSCounterNumber----1,2,3,4,5(autogenerated)
TOCHSCounterLabelText----A,B,C,D,E

TOCHSCounterStyleID ---2
TOCHSCounterNumber----1,2,3,4,5(autogenerated)
TOCHSCounterLabelText----a,b,c,d

TOCHSCounterStyleID ---3
TOCHSCounterNumber----1,2,3,4,5(autogenerated)
TOCHSCounterLabelText----i,ii,iii,iv,v...


Thank You



nathans
Aged Yak Warrior

938 Posts

Posted - 2008-09-03 : 13:07:41
Heres an example of creating a test stored procedure that does a simple insert into a test table. Forgive me if this is too elementary for you, im just trying to start at the beginning of using stored procedures.



-- if table exists already we must delete first before trying to create it
if object_id('dbo.Test') is not null
begin
drop table dbo.Test
end

-- now create the new table with an identity (autogenerated) Primary Key
create table dbo.Test (TestId int identity(1,1) primary key, TestName varchar(25))

-- insert data into new table, do not insert into the identity because that is managed by the table identity
insert into dbo.Test (TestName)
values ('Test direct insert')

-- return the results
select TestId, TestName from dbo.Test

-- drop if exists, then create a new stored proc to take in the TestName as a parameter and do the insert
if object_id('dbo.usp_InsertTest') is not null
begin
drop procedure dbo.usp_InsertTest
end;
go

create procedure dbo.usp_InsertTest (@TestName varchar(25))
as
set nocount on

insert into dbo.Test (TestName)
values (@TestName)
go

-- exec the stored procedure
declare @TestName varchar(25)
set @TestName = 'Test from proc'

exec dbo.usp_InsertTest @TestName

-- return the results
select TestId, TestName from dbo.Test




Nathan Skerl
Go to Top of Page
   

- Advertisement -