Wednesday, March 7, 2012

insert query ?

my application will add and delete and update records in db

my problem is when to insert

I have one text box and one dropdownbox one to write the name of db and the dropdownbox to choose the holding server ..

this is the structure of each table >>

servers_tbl : SRV_ID,Server_Name

DB_tbl : DB_ID,DB_Name

srvdb_tbl : DB_ID,SRV_ID(forign keys from the previous tables)

so >>>

I want to add a new db to a server

so I am writing the new db name in the textbox and choose the server from the dropdownbox and press a button to add the db name in the DB_tbl.DB_Name and add the db id in the DB_tbl.DB_ID to the srvdb_tbl.DB_ID and server id in the Servers_tbl.SRV_ID

any one can help me ...

You need a stored procedure along the lines of

CREATE PROCEDURE dbo.AddDbServer ( @.DB_Name VARCHAR(50), @.SRV_ID INT) AS

DECLARE @.DB_ID INT

IF NOT EXISTS(SELECT * FROMDB_tbl WHERE DB_NAME = @.DB_NAME)

BEGIN INSERT INTO DB_tbl (DB_NAME) VALUES (@.DB_Name)

SELECT @.DB_ID = SCOPE_IDENTITY

END

ELSE

SELECT @.DB_ID = SELECT DB_ID FROMDB_tbl WHERE DB_NAME =@.DB_Name

END

INSERT INTOsrvdb_tbl(DB_ID,SRV_ID) VALUES (@.DB_ID , @.SRV_ID)

You will need to test the stored procedure before incorporating it into your program.

No comments:

Post a Comment