Click here to Skip to main content
16,007,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one problem for writing query my problem is that i want write a query like this

in my database there is one main table "Contacts" in that there is two field Category1Code and Category2Code but i want to replace these Code into name.There is table for Category1Code and Category2Code.

table structure is like this
"Contacts"

ContactID Category1Code Category2Code FirstName LastName

C000001 C1018 C2025 ABC XYZ


the category1code table look like this

Category1Code CategoryName

C1018 PQR



the category2code table

Category2Code Category1Code Categoryname
C2001 C1018 TTT


i want replace these code into its name into Contact table.
Please give query
thnx in advance..

Finally i want output like this into Contact table

ContactID Category1Code Category2Code FirstName LastName

C000001 PQR TTT ABC XYZ
Posted
Updated 18-Mar-13 20:00pm
v2
Comments
akee seth 19-Mar-13 3:57am    
Can you give your query up to which you have tried. SO that it will be easy to help.

1 solution

SQL
alter procedure sp_contacts
	(
		@Category1Code nvarchar(20), 
		@Category2Code nvarchar(20), 
		@FirstName nvarchar(20), 
		@LastName nvarchar(20)
	)
AS 
begin
	Declare @CategoryName1  varchar(20)
	Declare @CategoryName2  varchar(20)
	select @CategoryName1= CategoryName from Category1Code where Category1Code = @Category1Code 
	select @CategoryName2= CategoryName from Category2Code where Category2Code = @Category2Code and Category1Code = @Category1Code
   Insert Into contacts
	(
		Category1Code ,
		Category2Code ,
		FirstName,
		LastName
	)
	values
	(
		@Category1Code ,
		@Category2Code ,
		@FirstName,
		@LastName
	)
	update contacts set 
	Category1Code= @CategoryName1,Category2Code= @CategoryName2 where  Category1Code = @Category1Code and Category2Code = @Category2Code
end 
 
 exec sp_contacts 'C1018', 'C2001', 'ABC','XYZ'



i hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900