How to update data in one table from corresponding data in another table in SQL Server 2005

前端 未结 8 1778
谎友^
谎友^ 2020-11-30 00:24

I have two tables in different databases on the same database server.

Both the databases have the same structure, but different data. Database1 (Test1) is the latest

相关标签:
8条回答
  • 2020-11-30 01:10

    this works wonders - no its turn to call this procedure form code with DataTable with schema exactly matching the custType create table customer ( id int identity(1,1) primary key, name varchar(50), cnt varchar(10) )

     create type custType as table
     (
     ctId int,
     ctName varchar(20)
     )
    
     insert into customer values('y1', 'c1')
     insert into customer values('y2', 'c2')
     insert into customer values('y3', 'c3')
     insert into customer values('y4', 'c4')
     insert into customer values('y5', 'c5')
    
     declare @ct as custType 
     insert @ct (ctid, ctName) values(3, 'y33'), (4, 'y44')
     exec multiUpdate @ct
    
     create Proc multiUpdate (@ct custType readonly) as begin
     update customer set  Name = t.ctName  from @ct t where t.ctId = customer.id
     end
    
    public DataTable UpdateLevels(DataTable dt)
            {
                DataTable dtRet = new DataTable();
                using (SqlConnection con = new SqlConnection(datalayer.bimCS))
                {
                    SqlCommand command = new SqlCommand();
                    command.CommandText = "UpdateLevels";
                    command.Parameters.Clear();
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@ct", dt).SqlDbType = SqlDbType.Structured;
                    command.Connection = con;
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                    {
                        dataAdapter.SelectCommand = command;
                        dataAdapter.Fill(dtRet);
                    }
                }
    }
    
    0 讨论(0)
  • 2020-11-30 01:12
    UPDATE table1
    SET column1 = (SELECT expression1
                   FROM table2
                   WHERE conditions)
    [WHERE conditions];
    
    0 讨论(0)
提交回复
热议问题