Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to add create multiple rows in a single instance with SQL Server 2008

Posted on 2010-08-28
5
321 Views
Last Modified: 2012-06-27
Hi,

I am trying to work with SQL Server 2008 and pre-populate a number of rows with some data that has already ben pre-defined.

I am struggling to create/insert into multiple lines all at once using an insert into statement.

When I right click the table and click script as, Insert into I get the following code but do not know where to put the data after that.

If anyone can help that would be very much appreciated.

Many thanks

Simon
INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])
     VALUES
           (<Record, int,>
           ,<Objecive, varchar(255),>
           ,<Goal, varchar(255),>
           ,<Strategy_1, varchar(255),>
           ,<Measure_1, varchar(255),>
           ,<Owner_1, varchar(255),>
           ,<Strategy_2, varchar(255),>
           ,<Measure_2, varchar(255),>
           ,<Owner_2, varchar(255),>
           ,<Strategy_3, varchar(255),>
           ,<Measure_3, varchar(255),>
           ,<Owner_3, varchar(255),>
           ,<Strategy_4, varbinary(50),>
           ,<Measure_4, varchar(255),>
           ,<Owner_4, varchar(255),>
           ,<Strategy_5, varchar(255),>
           ,<Measure_5, varchar(255),>
           ,<Owner_5, varchar(255),>
           ,<Strategy_6, varchar(255),>
           ,<Measure_6, varchar(255),>
           ,<Owner_6, varchar(255),>)
GO

Open in new window

0
Comment
Question by:SimonPrice33
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33549037
The markers like

<Record, int,>

are there to let you know that an "int" value goes there to be stored into "Record" column.
Using some dummy values, the below is a sample statement.
INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])
     VALUES
(1,'Objective','Goal','Strategy_1','Measure_1','Owner_1','Strategy_2','Measure_2',.....,'Owner_6')
GO

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33549042
In SQL Server 2008, you can insert multiple rows at once using commas between each set of values.
e.g.

insert sometable (a,b,c,d) values
(1,'a',3,4),
(2,'c',5,6),
(3,'f',9,10)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 33549044
I don't know what exactly you want. you can use SELECT statement in INSERT like:

Insert into Table1 (column1,column2)
select 'colVal1','ColVal1'
UNION ALL
select 'colVal2','ColVal2'

in select statement, you can directly put the value or put your variable name or SELECT (recordset) from other tables. by this way, you can insert multiple row at the same time.  below is the example script of your table. I have used same variable name in both SELECT statement which shouldn't be same in both SELECT otherwise, same row will be inserted twice.

INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])

   SELECT [@Record]
           ,[@Objecive]
           ,[@@Goal]
           ,[@Strategy_1]
           ,[@Measure_1]
           ,[@Owner_1]
           ,[@Strategy_2]
           ,[@Measure_2]
           ,[@Owner_2]
           ,[@Strategy_3]
           ,[@Measure_3]
           ,[@Owner_3]
           ,[@Strategy_4]
           ,[@Measure_4]
           ,[@Owner_4]
           ,[@Strategy_5]
           ,[@Measure_5]
           ,[@Owner_5]
           ,[@Strategy_6]
           ,[@Measure_6]
           ,[@Owner_6]
union all
   SELECT [@Record]
           ,[@Objecive]
           ,[@@Goal]
           ,[@Strategy_1]
           ,[@Measure_1]
           ,[@Owner_1]
           ,[@Strategy_2]
           ,[@Measure_2]
           ,[@Owner_2]
           ,[@Strategy_3]
           ,[@Measure_3]
           ,[@Owner_3]
           ,[@Strategy_4]
           ,[@Measure_4]
           ,[@Owner_4]
           ,[@Strategy_5]
           ,[@Measure_5]
           ,[@Owner_5]
           ,[@Strategy_6]
           ,[@Measure_6]
           ,[@Owner_6]
           
   
GO

Open in new window

0
 
LVL 2

Expert Comment

by:IngCharlie
ID: 33549046
This is a representation of you values <Measure_6, varchar(255),> of type varchar(255)
so if you want Measure_6 to have the value "value"

INSERT INTO [OGSM].[dbo].[OGSM]
([Measure_6])
VALUES ('value')

so just replace the statements <...> with your values and don't forget to use ' ' for non numberic values
0
 
LVL 10

Accepted Solution

by:
Marcjev earned 500 total points
ID: 33549048
an insert statement is of the form

insert into tablename(fieldname1, fieldname2, [and so on]) values (valieforfield1, valueforfield2, [and so on]).

so if I have a table 'person' with fields firstname, lastname, streetname
I would need something like

insert into person(firstname, lastname, streetname) values ('john', 'doe', 'unknown')

for sql script i would repeat multiple times like and ad a go at the end. Enter this in a sql query window.

insert into person(firstname, lastname, streetname) values ('john', 'doe', 'unknown')
insert into person(firstname, lastname, streetname) values('Elisa', 'Doolitle', 'Firststreet')
go
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question