問題描述
我有以下聲明:
insert into forecast_entry.user_role_xref
( user_master_id ,
role_id ,
created_date ,
created_by
)
values
( 276 , -- user_master_id - int
101 , -- role_id - int
getdate() , -- created_date - datetime
'MICHAELSK' -- created_by - varchar(20)
)
我需要為 role_id 101-355 生成一行(所以與上面的語句相同,除了隨著 role_id 遞增而重復).什么是最好的方法來做到這一點?為了完成工作,我打算編寫一個具有循環的快速 C# 應用程序,但我確信這不是最好的方法,并希望在這里學習一些東西以避免將來不得不這樣做(因為我我相信這種場景很常見).
I need to generate a row for role_id 101-355 (so the same statement above, except repeated with the role_id incrementing). What would be the best way to do this? To get the job done I'm intending on writing a quick C# application that will have a loop but I'm sure this isn't the best way and hope to learn something here to avoid having to do that in future (as I'm sure this kind of scenario is common).
推薦答案
你應該使用 數字表,如果你沒有,你可以像這樣使用 master..spt_values
:
You should make use of numbers table and if you don't have one you can use master..spt_values
like this:
insert into forecast_entry.user_role_xref
( user_master_id ,
role_id ,
created_date ,
created_by
)
select 276, -- user_master_id - int
number, -- role_id - int
getdate() , -- created_date - datetime
'MICHAELSK' -- created_by - varchar(20)
from master..spt_values
where type = 'P' and
number between 101 and 355
這篇關于如何插入多行 - 需要一個循環?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!