You must Sign In to post a response.
  • Category: SQL Server

    Insert multiple records, selected from some column

    Hi

    I want to Insert Company code = XYZ for all the employees whose CompanyKod='ABC'

    For ex: If there are 10 employees with companyKod = 'ABC', then for all those 10 employees I have to insert 10 more rows with companyKod='XYZ'

    So , totally there should be 20 records after Insert.

    I tried with below Query,
    Insert into table1(EmpID, CompanyKod)
    values ((select EmpID from HandlaggareBolagKoppling where CompanyKod='ABC'),'XYZ')

    but as subquery returns more than 1 value, so insert is failing.

    Please help me to correct this Query.

    Thanks,
    Ashok
  • #762233
    Hi

    You can try this Query working Good



    INSERT INTO tblemps (EmpCode, EmpName)
    SELECT EmpCode, EmpName FROM tblemps WHERE Empcode='XYZ';



    I have attached Snapshot given below check them.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #762235
    Yeah, it worked. thanks.

  • #762236
    Hi

    Pramod

    If my answer fixed in your issue means select as best answer thats helpful to others and grow our DNS site.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762249
    Hello Pramod,

    You can refer the below query :

    INSERT INTO table1(EmpID, CompanyKod)
    SELECT EmpID FROM HandlaggareBolagKoppling WHERE CompanyKod = 'XYZ';


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762330
    Hi Promod,

    You can use the following query:

    INSERT INTO HandlaggareBolagKoppling(EmpID, CompanyKod)
    SELECT EmpID , 'XYZ' AS CompanyKod
    FROM HandlaggareBolagKoppling
    WHERE CompanyKod='ABC'

    If my answer fixed in your issue , select as best answer which will be helpful to others.

  • #763306
    Hello,

    Try with the below query for help :-

    Insert into table1(EmpID, CompanyKod)
    select EmpID, 'XYZ' as CompanyKod from HandlaggareBolagKoppling where CompanyKod='ABC'

    Thanks


Sign In to post your comments