NEED A STORED PROCEDURE TO CHECK VALUES FROM MULTIPLE TABLES

Posted by Parthibansk under Sql Server on 11/8/2012 | Points: 10 | Views : 1200 | Status : [Member] | Replies : 3
i got a registration form in which we have to check username and companyname from two tables...[username from table1,companyname from table2]..if name already exists it should throw error,else insert records into table 3..

TABLE1:

USERNAME
COMPANYNAME
PASSWORD
ADMINISTRATOR

TABLE2:

COMPANYNAME
ADDRESS
CONTACT
LICENSE

TABLE3:

USERNAME
PASSWORD
COMPANYNAME
CONTACT




Responses

Posted by: Krv on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
if not exist (select username from table1 where username = @username and select companyname from table2 where companyname = @companyname )
begin
--insert into table3
end
else
begin
--throw exp
end

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
hi..Parthibansk

will you please take an example ........for this...
like..

these ...are records and i have this output....

then it will more easy for us to understand your requirement.....:-)

Sourabh07

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Seng2hs on: 11/8/2012 [Member] Starter | Points: 25

Up
0
Down
if (Select COUNT(USERNAME) from table1 T1 inner join table2 T2 ON T1.companyname=T2.companyname
where T1.username = @username and T2.companyname=@companyname) = 0
BEGIN
insert into table3(USERNAME,PASSWORD,COMPANYNAME,CONTACT) values (@USERNAME,@PASSWORD,@COMPANYNAME,@CONTACT)
END
ELSE
BEGIN
throw error
END

Thanks

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response