我有三张桌子:
DECLARE @UserTable TABLE(
[UserId] [int] IDENTITY(1,1) NOT NULL,
UserName varchar(100))
DECLARE @UserRoles TABLE(
[UserRoleId] [int] IDENTITY(1,1) NOT NULL,
RoleName varchar(100),
UserId int)
DECLARE @UserProjects TABLE(
[UserProjectId] [int] IDENTITY(1,1) NOT NULL,
ProjectName varchar(100),
UserId int)
我想要做的是,每当我在@UserTable
中创建新记录时,我都想从该记录中获取ID并在@UserRoles
和@UserProjects
中创建新记录。在逐个插入记录的基础上,我可以使用SCOPE_IDENTITY
获取新ID,然后使用该值中的值进行第2次和第3次插入...
但是当我尝试将这样的内容添加到@UserTable中的100条记录时,它将无法正常工作,因为它是批量插入。最好的方法是什么?用光标?我对这些想法持开放态度,因为我以前从来没有这样做过......用FOREACH loop
会很简单,但据我所知,SQL并没有像我们一样处理这些问题。会期望Java或C#,对吧?
答案 0 :(得分:2)
除非我弄错了,否则您要做的是在表上强制执行参照完整性并对级联操作进行说明。有一篇不错的文章here展示了如何设置它。
答案 1 :(得分:0)
您可以像这样写一个INSERT TRIGGER。当新行插入到用户表中时,此触发器将为每个执行的语句(不是每行)“触发”。因此,在BULK Insert的情况下,我们必须使用临时表变量来保存所有新插入的用户ID,然后在UserRoles和UserProjects表中执行插入。
CREATE TRIGGER trg_Insert_Role_And_Project
ON [UserTable]
FOR INSERT
AS
Begin
Declare @NewUserIDsTable TABLE
( NewUserID INT NOT NULL )
INSERT INTO @NewUserIDsTable SELECT ID FROM INSERTED
INSERT INTO UserRoles (UserId, RoleName)
SELECT NewUserID, 'Default Role'
FROM @NewUserIDsTable
INSERT INTO UserProjects (UserId, ProjectName)
SELECT NewUserID, 'Default Project'
FROM @NewUserIDsTable
End
@UserRoles和@UserProject表中的RoleName和ProjectName是否具有默认值?对于此代码示例,我使用了默认值'Default Role'
和'Default Project'
。
答案 2 :(得分:0)
你需要在你的UserTable上定义一个触发器,它会为你做一些级联插入其他表格
CREATE Trigger tr_ForInserts
ON UserTable
FOR INSERT
AS
BEGIN
SET NOCOUNT ON;
/*Get all the new insert into a temp table */
SELECT * INTO #TempTable
FROM inserted
/*Insert Statement to Insert recods in UserRoles Table */
INSERT INTO UserRoles (Column1,Column2,Column3,...)
SELECT Column1,Column2,Column3
FROM #TempTable
/*Insert Statement to Insert recods in UserProjects Table */
INSERT INTO UserProjects (Column4,Column5,Column6,...)
SELECT Column4,Column5,Column6
FROM #TempTable
END
答案 3 :(得分:0)
使用真实的表格(而不是变量),您可以创建一个触发器:
CREATE TABLE UserTable( [UserId] [int] IDENTITY(1,1) NOT NULL, UserName varchar(100))
CREATE TABLE UserRoles ( [UserRoleId] [int] IDENTITY(1,1) NOT NULL, RoleName varchar(100), UserId int)
CREATE TABLE UserProjects ( [UserProjectId] [int] IDENTITY(1,1) NOT NULL, ProjectName varchar(100), UserId int)
GO
if object_id('UserTableTrigger') is not null
drop trigger UserTableTrigger
go
create trigger UserTableTrigger ON Usertable
for INSERT
as
begin
insert into UserRoles(RoleName,UserId)
select 'Role_'+UserName, inserted.UserId
from inserted
insert into UserProjects(ProjectName,UserId)
select 'Project_'+UserName, inserted.UserId
from inserted
end
go
如果你想检查一下:
insert into UserTable (UserName)
select 'User_'+name from master..spt_values
select * from UserTable
left join UserRoles on UserTable.USerId = UserRoles.UserId
left join UserProjects on UserTable.USerId = UserProjects.UserId
答案 4 :(得分:0)
试试这个解决方案:
-- Parameters
DECLARE @UserName VARCHAR(100), @RoleName VARCHAR(100), @ProjectName VARCHAR(100);
SELECT @UserName = 'User CocoJambo',
@RoleName = 'Role MamboJambo',
@ProjectName = 'Project Jumbo';
-- End of Parameters
DECLARE @NewUserID TABLE (UserID INT);
INSERT @UserTable (UserName)
OUTPUT inserted.UserId INTO @NewUserID (UserID)
VALUES (@UserName);
INSERT @UserRoles (UserID, RoleName)
SELECT u.UserId, @RoleName
FROM @UserTable u;
INSERT @UserProjects (UserId, ProjectName)
SELECT u.UserId, @ProjectName
FROM @UserTable u;