Solved

SQL Command

Posted on 2011-02-24
4
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
U could also take a look at SqlBulkCopy, which is quite fast.
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now