Solved

SQL Command

Posted on 2011-02-24
4
283 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 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

749 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