我有一些数据,如下所示:我想将数据复制到其他用户。但是怎么做呢?数据是产品列表
select WL.Name, WL.ID,WL.UserID, WP.ID, WP.WishListID
from bdWishList WL
join
bdWishListProducts WP
on
WL.ID = WP.WishListID
where WL.UserID = '47512'
但现在我想将数据从userid = 47512复制到userid = 47509,如下所示:
rotterdam oudedijk 295 267 47512 3437 267
rotterdam oudedijk 295 267 47512 3438 267
rotterdam oudedijk 295 267 47512 3439 267
rotterdam oudedijk 295 267 47512 3440 267
rotterdam oudedijk 295 267 47512 3441 267
rotterdam oudedijk 295 267 47512 3442 267
rotterdam oudedijk 295 267 47512 3443 267
rotterdam oudedijk 295 267 47512 3444 267
rotterdam oudedijk 295 267 47512 3445 267
rotterdam oudedijk 295 267 47512 3446 267
rotterdam oudedijk 295 267 47512 3447 267
rotterdam oudedijk 295 267 47512 3448 267
rotterdam oudedijk 295 267 47512 3449 267
rotterdam oudedijk 295 267 47512 3450 267
rotterdam oudedijk 295 267 47512 3451 267
rotterdam oudedijk 295 267 47512 3452 267
rotterdam oudedijk 295 267 47512 3453 267
rotterdam oudedijk 295 evohome 268 47512 3454 268
rotterdam oudedijk 295 evohome 268 47512 3455 268
rotterdam oudedijk 295 evohome 268 47512 3456 268
rotterdam oudedijk 295 evohome 268 47512 3457 268
rotterdam oudedijk 295 evohome 268 47512 3458 268
rotterdam oudedijk 295 evohome 268 47512 3459 268
rotterdam oudedijk 295 evohome 268 47512 3460 268
rotterdam oudedijk 295 evohome 268 47512 3461 268
rotterdam oudedijk 295 evohome 268 47512 3462 268
waddinxveen 276 47512 3621 276
waddinxveen 276 47512 3622 276
waddinxveen 276 47512 3623 276
waddinxveen 276 47512 3624 276
waddinxveen 276 47512 3625 276
waddinxveen 276 47512 3626 276
waddinxveen 276 47512 3627 276
waddinxveen 276 47512 3628 276
waddinxveen 276 47512 3629 276
waddinxveen 276 47512 3630 276
waddinxveen 276 47512 3631 276
waddinxveen 276 47512 3632 276
waddinxveen 276 47512 3633 276
waddinxveen 276 47512 3634 276
waddinxveen 276 47512 3635 276
waddinxveen 276 47512 3636 276
waddinxveen 276 47512 3637 276
waddinxveen 276 47512 3638 276
waddinxveen 276 47512 3639 276
waddinxveen 276 47512 3640 276
waddinxveen 276 47512 3641 276
waddinxveen 276 47512 3642 276
waddinxveen 276 47512 3643 276
waddinxveen 276 47512 3644 276
waddinxveen raditoren 277 47512 3645 277
waddinxveen raditoren 277 47512 3646 277
waddinxveen raditoren 277 47512 3647 277
waddinxveen raditoren 277 47512 3648 277
waddinxveen raditoren 277 47512 3649 277
waddinxveen raditoren 277 47512 3650 277
waddinxveen raditoren 277 47512 3651 277
waddinxveen raditoren 277 47512 3652 277
waddinxveen raditoren 277 47512 3653 277
waddinxveen raditoren 277 47512 3654 277
waddinxveen raditoren 277 47512 3655 277
waddinxveen raditoren 277 47512 3656 277
waddinxveen raditoren 277 47512 3657 277
waddinxveen raditoren 277 47512 3658 277
waddinxveen raditoren 277 47512 3659 277
waddinxveen raditoren 277 47512 3660 277
但是如何复制数据?
谢谢
现在我的意思是什么呢?
两个创建表脚本是:
USE [Dynamicweb-Verploegen-Test] GO
/****** Object: Table [dbo].[bdWishListProducts] Script Date: 06/24/2016 09:26:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[bdWishListProducts](
[ID] [int] IDENTITY(1,1) NOT NULL,
[WishListID] [int] NULL,
[ProductID] [nvarchar](30) NULL,
[ProductVariantID] [nvarchar](255) NULL,
[Quantity] [numeric](18, 0) NULL,
[ShopID] [nvarchar](50) NULL,
CONSTRAINT [PK_bdWishListProducts] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[bdWishListProducts] WITH CHECK ADD CONSTRAINT [FK_bdWishListProducts_bdWishList] FOREIGN KEY([WishListID])
REFERENCES [dbo].[bdWishList] ([ID])
GO
ALTER TABLE [dbo].[bdWishListProducts] CHECK CONSTRAINT [FK_bdWishListProducts_bdWishList]
GO
和
USE [Dynamicweb-Verploegen-Test]
GO
/****** Object: Table [dbo].[bdWishList] Script Date: 06/24/2016 09:28:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[bdWishList](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NULL,
[Name] [varchar](255) NULL,
[DateCreated] [datetime] NULL,
[DateModified] [datetime] NULL,
[Active] [bit] NULL,
CONSTRAINT [PK_bdOrderTemplate] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[bdWishList] ADD CONSTRAINT [DF_bdWishList_DateCreated] DEFAULT (getdate()) FOR [DateCreated]
GO
ALTER TABLE [dbo].[bdWishList] ADD CONSTRAINT [DF_bdWishList_DateModified] DEFAULT (getdate()) FOR [DateModified]
GO
ALTER TABLE [dbo].[bdWishList] ADD CONSTRAINT [DF_bdWishList_default] DEFAULT ((1)) FOR [Active]
GO
@ gofr1 问题是:
user1:
userlist1: products: user1List1product1, user1List1product2
userlist2, products: user1List2Product3, user1List2Product4
User2:
Userlist3, products: user2List3Prodcuts5
Userlist4, products User2Lis4tProduct6
Then I copy the lists from User1 to User2:
lists user2:
userlist1: products: user1List1product1, user1List1product2
userlist2, products: user1List2Product3, user1List2Product4
Userlist3, products: user2List3Prodcuts5, User2Lis4tProduct6
Userlist4, products : empty and it has to be:
Userlist4, products User2Lis4tProduct6
因此复制了所有列表,但不复制产品。
答案 0 :(得分:1)
根据您桌子的创建脚本,我建议您先插入bdWishList
:
DECLARE @wlid int = <>
INSERT INTO bdWishList ([UserID],[Name]) --[DateCreated],[DateModified],[Active] will get default values
SELECT 47509,
WL.Name --Maybe you should use another name
FROM bdWishList WL
WHERE WL.UserID = 47512 and WL.ID = @wlid
然后获取插入行的ID:
DECLARE @id int
SELECT @id = IDENT_CURRENT('bdWishList')
然后在bdWishListProducts
中插入数据:
INSERT INTO bdWishListProducts ([WishListID], [ProductID], [ProductVariantID], [Quantity], [ShopID])
SELECT @id,
WP.ProductID,
WP.ProductVariantID,
WP.Quantity,
WP.ShopId
FROM bdWishList WL
INNER JOIN bdWishListProducts WP
ON WL.ID = WP.WishListID
where WL.UserID = 47512 and WL.ID = @wlid
如果您想一次复制许多愿望清单:
DECLARE @temp TABLE (
[NewId] int,
[OldId] int
)
MERGE INTO ##bdWishList as target
USING (
SELECT ID,
47509 as UserID,
WL.Name --Maybe you should use another name
FROM ##bdWishList WL
WHERE WL.UserID = 47512
) AS source
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT ([UserID],[Name])
VALUES (source.[UserID],source.[Name])
OUTPUT inserted.ID, source.ID
INTO @temp;
INSERT INTO bdWishListProducts ([WishListID], [ProductID], [ProductVariantID], [Quantity], [ShopID])
SELECT t.[NewId],
WP.ProductID,
WP.ProductVariantID,
WP.Quantity,
WP.ShopId
FROM bdWishList WL
INNER JOIN bdWishListProducts WP
ON WL.ID = WP.WishListID
INNER JOIN (SELECT DISTINCT [NewId],[OldId] FROM @temp) t
ON t.[OldId] = WL.ID
where WL.UserID = 47512
答案 1 :(得分:0)
最简单的方法是插入声明ala:
Insert into bdWishListProducts (
select @NewID, WP.ID, WP.WishListID
from bdWishList WL
join
bdWishListProducts WP
on
WL.ID = WP.WishListID
where WL.UserID = '47512'
(假设您不想要单独的心愿单ID
更明智的做法是
Insert into bdWishList (name, id, userid) Values ('NEWNAME', 'NewID', 'USERID')
Insert into bdWishListProducts (
select WP.ID, NEWID
from bdWishList WL
join
bdWishListProducts WP
on
WL.ID = WP.WishListID
where WL.UserID = '47512'
)
显然 - 您需要确保提供所有必填字段 - 我只是尽快输入