0

I want to compare two different table columns (ID) if the both id's are same i want to insert Location Name Having in the first table to second table

Table 1
-------
    Id   Name
    ---------
    1    Hyd
    2    Banglore
    ---------

Table 2
-------
    Id Name
    -------
    1 
    2 

From the first table id and second table id if both are equal i want to insert name column in the second column which existed in the same id row Using C# Help me

1
  • 1
    Where are you stuck? You can compare values with the equality operator (==), you can conditionally run code based on that comparison with a conditional block if (someCondition) { ... }, you can connect to the database in lots of different ways. What have you tried and what isn't working? Commented Jul 3, 2014 at 11:33

2 Answers 2

1

Well without a lot of info here goes.

This is Assuming that when you say c# you actually mean C# to SQL.

so in SQL do something like this

UPDATE Table2
SET Table2.Name = Table1.name
FROM Table1
WHERE Table2.ID = table1.ID

make this a stored procedure, then use c# to call run the procedure.

again this is just an example and is based on the assumption that you actually want this done in SQL and use c# to run it

Sign up to request clarification or add additional context in comments.

Comments

0

Try following:

create table #testsource
(ID int,city nvarchar(200))

insert into #testsource (ID,city)
values(1,'mumbai'),
(2,'Pune')

select *from #testsource

create table #testdestination
(ID int,city nvarchar(200))

//insert rows in table
insert into #testdestination (ID,city) values(1,''),(2,''),(3,'')

//select rows from table
select *from #testdestination

//get the values from Source table and update to Destination table
update #testdestination set city=s.city 
from #testsource s 
where s.ID=#testdestination.ID

select *from #testdestination

drop table #testsource
drop table #testdestination

Results: enter image description here

Assuming your two table names with 'Table1' & 'Table2'

CREATE PROCEDURE [dbo].[p_UpdateDestinationTable]
AS
BEGIN
UPDATE Table2
SET Table2.Name = t1.name
FROM Table1 t1
WHERE Table2.ID = t1.ID

NOTE: This is a sample used with Temporay Table plelase do not use "as is". make changes according to your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.