Insert data

  • Hi,

    Iam Creating a table and inserting data into it as below:

    CREATE TABLE dbo.Departments (DeptID tinyint NOT NULL PRIMARY KEY, DeptName nvarchar(30), Manager nvarchar 50));

    INSERT INTO [Mydb].[dbo].[Departments]

    ([DeptID]

    ,[DeptName]

    ,[Manager])

    VALUES

    (1

    ,'Human Resource'

    ,'Frank')

    If I want to Insert mant values, How shoulld I do?

    Should I repeat this below step repeatedly and changing values for DeptId, deptName and Manager or Any other way

    INSERT INTO [Mydb].[dbo].[Departments]

    ([DeptID]

    ,[DeptName]

    ,[Manager])

    VALUES

    (2

    ,'Management'

    ,'Franklin')

  • Hi,

    you can use the following.

    INSERT INTO [Mydb].[dbo].[Departments]

    ([DeptID]

    ,[DeptName]

    ,[Manager])

    select 1,'Human Resource','Frank'

    union all

    select 2,'Management','Franklin'

    union all

    select 3,'something','something'

    Cheers

    Alejandro Pelc

  • Hello,

    Does the data that you want to insert already exist in electronic form e.g. in a Flat File, another DB Table etc, or do you have to manually type it in?

    If the data is already in a table or file then there are many ways to insert the data in one go

    e.g. Insert Into MyTable (MyCols) Values (Select MySourceCols From MySourceTable).

    Please note that MySourceTable could equally be a data source outside of SQL Server that you connect to with OpenRowSet or a Linked Server.

    Regards,

    John Marsh

    www.sql.lu
    SQL Server Luxembourg User Group

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply