Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Command

Posted on 2011-02-24
4
Medium Priority
?
296 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
[X]
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
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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:
Ephraim Wangoya earned 1000 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 41

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

722 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