I have table structre like this:

Table `MainTable`

Columns: 

```
Id INT, 
TableName Varchar(50),
StartValue VARCHAR(50) 

```

Here TableName column have names of all the tables present in the database

Now I need to update "StartValue" column in `MainTable` from corresponding tables. Any idea how to achieve this?

Example 

`MainTable`

```
Id TableName StartValue
----------------------
1  table1      NULL  
2  Table2     Null

```

I need to update `StartValue` column of `MainTable` by getting top 1 value from table name present in the tables

Means record 1 will get first value from table1 and record 2 will get first value from table2

Any idea how to achieve this?
['https://Stackoverflow.com/questions/27188074', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1654393/']
Adding a check for `EXISTS` on the `INSERT` statement should not have a significant effect on performance. 

```
INSERT INTO Employee ([Name] ,[Lname] ,[Code])
SELECT  [Name] ,[Lname] ,@Code 
FROM    @tblEmp AS t
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    Employee AS e
            WHERE   e.Name = t.Name
            AND     e.Lname = t.Lname
        );

```

This is *fairly* safe, but still vulnerable to a [race condition](http://en.wikipedia.org/wiki/Race_condition). I think the safest way to do the insert, with is to use `MERGE` with the locking hint `HOLDLOCK`:

```
MERGE Employee WITH(HOLDLOCK) AS t
USING @tbl AS s
    ON s.Name = t.Name
    AND s.LName = t.LName
WHEN NOT MATCHED THEN 
    INSERT ([Name] ,[Lname] ,[Code])
    VALUES (s.Name, s.Lname, @Code);

```
If the table has a primary key that is not set to auto generate then it will error when you try to insert a record without the key. You will need to either set the primary key field as an identity seed or you can include the primary key with the insert.