sql server - Bulk insert using loop -
i have been trying insert 500 records table (dbo.product
) getting error:
msg 102, level 15, state 1, line 31
incorrect syntax near 'p'
here query:
declare @startloop int, @endloop int, @productid int, @productname nvarchar(250), @bestbefore datetime, @manufacturedate datetime, @productnumber nvarchar(25), @weight decimal, @productstocklevelid int select @startloop =1, @endloop = 500, @productid = '', @productname = 'yoghurt', @bestbefore = '', @manufacturedate = '' , @productnumber = '', @weight = '', @productstocklevelid = '' while @startloop <= @endloop begin insert dbo.product p (productid, productname, bestbefore, manufacturedate, productnumber, [weight], productstocklevelid) select @productid + cast(@startloop int), @productname + cast(@startloop nvarchar(250)), @bestbefore + cast(@startloop datetime), @manufacturedate + cast(@startloop datetime), @productnumber + cast(@startloop nvarchar(25)), @weight + cast(@startloop decimal), @productstocklevelid + cast(@startloop int) select @startloop = @startloop + 1 end
can please tell me doing wrong?
thank in advance.
remove p
in insert command
insert dbo.product p (productid
to
insert dbo.product (productid
reference link insert sql mdsn
and way insert product table without while
use cte
recursive function
;with temp ( select @startloop rowindex union select t.rowindex + 1 temp t t.rowindex < @endloop ) -- temp table result @startloop,@startloop +1 ,... -> @endloop insert dbo.product (productid, productname, bestbefore, manufacturedate, productnumber, [weight],productstocklevelid) select @productid + cast(t.rowindex int), @productname + cast(t.rowindex nvarchar(250)), @bestbefore + cast(t.rowindex datetime), @manufacturedate + cast(t.rowindex datetime), @productnumber + cast(t.rowindex nvarchar(25)), @weight + cast(t.rowindex decimal), @productstocklevelid + cast(t.rowindex int) temp t option (maxrecursion 0 )
Comments
Post a Comment