Solved

SQL Command

Posted on 2011-02-24
4
269 Views
Last Modified: 2012-05-11
How can I insert 50 records in a table as follows using sql command.

table1
ItemNo(varchar)   name (varchar)
A_1                       some Text
A_2                       some Text
...
A_50                     some Text

ayha
0
Comment
Question by:ayha1999
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 34972781
declare @No integer
set @No = 1

while @No <= 50
begin
  insert into table1(ItemNo, name)
  select 'A_1' + cast(@No as varchar), 'some text'
  set @No = @No + 1
end
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 34972786
sorry
declare @No integer
set @No = 1

while @No <= 50
begin
  insert into table1(ItemNo, name)
  select 'A_' + cast(@No as varchar), 'some text'
  set @No = @No + 1
end

Open in new window

0
 
LVL 20

Expert Comment

by:informaniac
ID: 34972821
U could also take a look at SqlBulkCopy, which is quite fast.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34973399
You can try CTE.
declare @table as table (StartValue int, EndValue int)
insert into @table values (1,10) -- change this number to required value
;with CTE as (
select StartValue,EndValue from @table union all
select StartValue+1,EndValue from CTE where StartValue+1 <= EndValue)
select 'A_'+CONVERT(varchar,StartValue) ItemNo,'some text' name 
  from CTE
/*
ItemNo	name
A_1	some text
A_2	some text
A_3	some text
A_4	some text
A_5	some text
A_6	some text
A_7	some text
A_8	some text
A_9	some text
A_10	some text
*/

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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