Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2010-02-01 : 16:10:35
|
| Hi,I have this code here for creating a table and then populating that table with a record.Now i want to create a variable called rec_count at the top.when populating the table i want to create rows which equal to the number in rec_count.I want to put ths insert statement in a loop like declare rec_count intwhile i < rec_countINSERT [dbo].[tbl_RecordNum] ([Rec_ID], [Rec_NM], [Rec_TM]) VALUES (1, NULL, NULL)end whileIs this possible?How can i do this?IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbl_RecordNum]') AND type in (N'U'))DROP TABLE [dbo].[tbl_RecordNum]GO/****** Object: Table [dbo].[tbl_RecordNum] Script Date: 02/01/2010 14:39:47 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOIF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbl_RecordNum]') AND type in (N'U'))BEGINCREATE TABLE [dbo].[tbl_RecordNum]( [Rec_ID] [int] IDENTITY(1,1) NOT NULL, [Rec_NM] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Rec_TM] [datetime] NULL, CONSTRAINT [PK__tbl_RecordNum__1A9EF37A] PRIMARY KEY CLUSTERED ( [Rec_ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON))ENDGOSET IDENTITY_INSERT [dbo].[tbl_RecordNum] ONINSERT [dbo].[tbl_RecordNum] ([Rec_ID], [Rec_NM], [Rec_TM]) VALUES (1, NULL, NULL)SET IDENTITY_INSERT [dbo].[tbl_RecordNum] OFF |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-01 : 16:19:38
|
yes that is possible.vars like i or rec_count must be declared and var names need always a @ in frontdeclare @i intdeclare @rec_count int now you need to set @rec_count to the wanted value and @i to the startvalueset @rec_count = 10set @=1 then you can do the loopwhile @i < @rec_countbegin insert tbl_RecordNum select @i,null,null set @i=@i+1end But now I see that your Rec_Id is an identity, so you have only to insert the nullvalues...while @i < @rec_countbegin insert tbl_RecordNum select null,null set @i=@i+1end No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2010-02-01 : 16:35:43
|
| Webfred,Thanks for this info.I am getting this error.Must declare the scalar variable "@".I am decaring the variables after creating the table.Should i declare them earlier in the code?declare @i intdeclare @rec_count intset @rec_count = 10set @=1SET IDENTITY_INSERT [dbo].[totalBFT] ONwhile @i < @rec_countBEGININSERT [dbo].[totalBFT] select null,nullset @i=@i+1SET IDENTITY_INSERT [dbo].[totalBFT] OFFEND |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-01 : 16:38:00
|
my bad!set @=1 is a typo. it should be set @i=1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-01 : 16:39:37
|
and with identity_insert on you should use my first approach (inserting the @i,null,null), you know? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
rowter
Yak Posting Veteran
76 Posts |
Posted - 2010-02-01 : 16:48:52
|
| Hi Webfred thanks,It failed when i had @i=1 in the code. It failed a couple of times .but now, it seems to work. No clue as to what i did different.After it worked i deleted the table again and mad this change moved the identity insert OFF after the ENDdeclare @i intdeclare @rec_count intset @rec_count = 10set @i=1SET IDENTITY_INSERT [dbo].[totalBFT] ONwhile @i < @rec_countBEGININSERT [dbo].[totalBFT] select null,nullset @i = @i+1ENDSET IDENTITY_INSERT [dbo].[totalBFT] OFFThanks a Lot |
 |
|
|
|
|
|
|
|