如何创建一个将值从一个表插入另一个表的sp

时间:2013-11-26 12:35:38

标签: sql

您好我想创建一个存储过程,从一个表中获取特定值并将它们插入到另一个表中,如果特定的表是'DateAquired'为null我在下面有这个存储过程,但它看起来不像预期请帮助。  提前致谢

这是我的代码

USE [MediaPlayer]
GO
/****** Object:  StoredProcedure [dbo].[sp_Wishlists]    Script Date: 11/26/2013 11:43:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_Wishlists]
    -- 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),
    @WishList int output ,
    @CName nvarchar  (50)output ,
    @Media nvarchar (50)output,
    @WishListID int output

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
Select GeneralID, Name, FileName, FilePath,FileSize,DateAdded,MediaLength,MediaSubType,MediaType, Thumbnail,DateAquired As Wishlist 
From General where NULLIF(DateAquired,'')IS Null

insert into Wishlists (generalID ,MediaType, Name)
values ((IDENT_CURRENT('dbo.General')),(IDENT_CURRENT('dbo.General')),(IDENT_CURRENT('dbo.General')))
SET @WishListID = @@IDENTITY
select GeneralID, MediaSubType, Name

From General where NOT EXISTS (Select Name from WishLists Where Name =@Name);
END

1 个答案:

答案 0 :(得分:0)

尝试下面的插入语句

insert into Wishlists (generalID ,MediaType, Name)
select GeneralID, MediaSubType, Name
From General where NOT EXISTS (Select Name from WishLists Where Name =@Name);