您好我的存储过程有问题,我想插入三个表,我创建了一个存储的procdure来做到这一点,但我有这两个表名为wishlist和general。我想插入wishlist表,如果dateaquired行为null但我创建的脚本无论如何插入到表中,有人可以改进我的脚本,以便它不会插入到我的wishlist表中,如果我的日期表中的dateaquired行不是空的。
USE [MediaPlayer]
GO
/****** Object: StoredProcedure [dbo].[CreateBooks] Script Date: 12/03/2013 19:05:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateBooks]
-- Add the parameters for the stored procedure here
@Name nvarchar (250),
@FileName nvarchar (250),
@FilePath nvarchar (50),
@FileSize float,
@DateAdded date,
@MediaLength nvarchar (50),
@MediaSubType nvarchar(50),
@MediaType nvarchar(50),
@Thumbnail image,
@DateAquired nvarchar(50),
@BooksName nvarchar (50),
@Publisher nvarchar(50),
@Author nvarchar(50),
@YearOfPublication date,
@Genre nvarchar(50),
@ISBN nvarchar (50),
@Synoposis nvarchar(50),
@SeriesTitle nvarchar(50),
@SeriesNumber nvarchar(50),
@GeneralID int output,
@BookID int output,
@WishListID int output
AS
BEGIN
Insert into dbo.General
(Name, FileName, FilePath, FileSize, DateAdded, MediaLength,
MediaSubType, MediaType, Thumbnail, DateAquired)
values (@Name, @FileName, @FilePath, @FileSize, @DateAdded, @MediaLength,
@MediaSubType, @MediaType, @Thumbnail, @DateAquired)
SET @GeneralID = @@IDENTITY
insert into dbo.Book
(GeneralID, BooksName, Publisher, Author, [Year of publication], Genre,
ISBN, Synoposis,[Series Title],[Series Number])
Values (IDENT_CURRENT('dbo.General'), @BooksName, @Publisher, @Author, @YearOfPublication, @Genre,
@ISBN, @Synoposis, @SeriesTitle, @SeriesNumber)
SET @BookID = @@IDENTITY
Select GeneralID, Name, FileName, FilePath,FileSize,DateAdded,MediaLength,MediaSubType,MediaType, Thumbnail,DateAquired As Wishlist
From General where NULLIF(DateAquired,'')IS Null
Select * from WishLists
select GeneralID, MediaSubType, Name
From General where NOT EXISTS (Select Name from WishLists Where Name =@Name);
insert into Wishlists (generalID ,MediaType, Name)
values ((IDENT_CURRENT('dbo.General')),@MediaSubType, @Name)
SET @WishListID = @@IDENTITY
select * from wishlists
END
答案 0 :(得分:0)
我可能错了,但对我来说,似乎你错过了一些逻辑案例。
您的代码:
Select GeneralID, Name, FileName, FilePath, FileSize, DateAdded,
MediaLength,MediaSubType,MediaType, Thumbnail, DateAquired As Wishlist
From General where NULLIF(DateAquired,'')IS Null
这样做是从General中选择指定的字段,其中DateAcquired为Null。在旁注中,我相信(我可能错了)如果你想要所有这些字段作为愿望清单,你应该将它们全部放在括号中,如:
Select (GeneralID, Name, FileName, FilePath, FileSize, DateAdded,
MediaLength,MediaSubType,MediaType, Thumbnail, DateAquired) As Wishlist
From General where NULLIF(DateAquired,'')IS Null
无论如何,一旦你掌握了这些数据,你就没有做任何事情,你正在进入一个新的选择查询,从Wishlists中选择*。然后,您再次从General中选择名称不在表中的位置。然后,您将向愿望列表中插入dbo.General,@ MediaSUbType,@ Name的当前标识值。
因此,所有这些代码实际上都在运行一些查询,然后不管这些结果如何,都将dbo.General的Identity值和状态值@MediaSubType和@Name插入到表wishlist中。为了使之前运行的所有查询对插入的内容产生影响,您需要使用案例。
我对表格和事情的了解不够,无法编写您需要的代码,但this answer应该让您了解如何使用CASE WHEN来做您需要的事情。
希望这有助于一些人,而不仅仅是来自我的无用的谣言。
答案 1 :(得分:0)
如果您只是尝试跳过基于@DateAquired
为空的WishList插入,您可以修改您的脚本,如下所示。如果这不是您想要的,请回复。
BEGIN
Insert into dbo.General(Name, FileName, FilePath, FileSize, DateAdded, MediaLength, MediaSubType, MediaType, Thumbnail, DateAquired)
values (@Name, @FileName, @FilePath, @FileSize, @DateAdded, @MediaLength, @MediaSubType, @MediaType, @Thumbnail, @DateAquired);
SET @GeneralID = scope_identity(); --use this instead of @@IDENTITY
insert into dbo.Book(GeneralID, BooksName, Publisher, Author, [Year of publication], Genre, ISBN, Synoposis,[Series Title],[Series Number])
Values (@GeneralID, @BooksName, @Publisher, @Author, @YearOfPublication, @Genre, @ISBN, @Synoposis, @SeriesTitle, @SeriesNumber)
SET @BookID = scope_identity(); --use this instead of @@IDENTITY
-- you already have the DateAquired from the insert into dbo.General above, so just use the param @DateAquired
if(@DateAquired is null)
begin
insert into Wishlists (generalID, MediaType, Name)
values (@GeneralId, @MediaSubType, @Name)
SET @WishListID = scope_identity(); --use this instead of @@IDENTITY
end
select [InsertedGeneralId] = @GeneralID, [InsertedBookID] = @BookID, [InsertedWishListID] = @WishListID
END
答案 2 :(得分:0)
AS
BEGIN
Insert into dbo.General
(Name, FileName, FilePath, FileSize, DateAdded, MediaLength,
MediaSubType, MediaType, Thumbnail, DateAquired)
values (@Name, @FileName, @FilePath, @FileSize, @DateAdded, @MediaLength,
@MediaSubType, @MediaType, @Thumbnail, @DateAquired)
SET @GeneralID = SCOPE_IDENTITY() --<--
insert into dbo.Book
(GeneralID, BooksName, Publisher, Author, [Year of publication], Genre,
ISBN, Synoposis,[Series Title],[Series Number])
Values (IDENT_CURRENT('dbo.General'), @BooksName, @Publisher, @Author,
@YearOfPublication, @Genre, @ISBN, @Synoposis, @SeriesTitle, @SeriesNumber)
SET @BookID = SCOPE_IDENTITY()
Select GeneralID, Name, FileName, FilePath,FileSize,DateAdded,MediaLength
,MediaSubType,MediaType, Thumbnail,DateAquired As Wishlist
From General where NULLIF(DateAquired,'')IS Null
Select * from WishLists
select GeneralID, MediaSubType, Name
From General where NOT EXISTS (Select Name from WishLists Where Name =@Name);
IF (@DateAquired IS NULL)
BEGIN
insert into Wishlists (generalID ,MediaType, Name)
values ((IDENT_CURRENT('dbo.General')),@MediaSubType, @Name)
SET @WishListID = SCOPE_IDENTITY()
END
select * from wishlists
END
同样使用SCOPE_IDENTITY()
而不是@@IDENTITY
,因为它将返回该列中的最后一个生成标识,即使它是由并发连接创建的。