3

我试图找到一种方法,仅当表中不存在新记录时才插入它。

这是存储过程:

USE [ABSTest]
GO
/****** Object:  StoredProcedure [dbo].[sp_InsertBudgetCommessaInPreventivo]    Script     Date: 01/07/2013 09:12:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Alex Ziani>
-- Create date: <04/01/2013>
-- Description: <Insert di valori con inserimento di parametri>
-- =============================================
ALTER PROCEDURE [dbo].[sp_InsertBudgetCommessaInPreventivo] 
@Progressivo                    nvarchar(50),
@Durata                         float,
@Costo                          money,
@CostoStandard                  money,
@Tariffa                        money,
@SpeseStandard                  money,
@Codice_Tipo                    nvarchar(50),
@Codice_FiguraProfessionale     nvarchar(10),
@Codice_Risorsa                 nvarchar(50),
@Codice_Commessa                nvarchar(20),
@Codice_ODL                     nvarchar(30),
@Task                           nvarchar(100)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

IF NOT EXISTS   (SELECT * FROM Preventivo WHERE Progressivo = @Progressivo 
            AND Quantita = @Durata 
            AND Costo = @Costo  
            AND CostoStandard = @CostoStandard 
            AND Tariffa = @Tariffa 
            AND SpeseStandard = @SpeseStandard 
            AND FkCodiceEnumTipoPreventivo = @Codice_Tipo 
            AND FkCodiceEnumFiguraProfessionale = @Codice_FiguraProfessionale
            AND FKCodiceRisorsa = @Codice_Risorsa 
            AND FkCodiceCommessa = @Codice_Commessa 
            AND FkCodiceOrdineDiLavoro = @Codice_ODL
            AND Task = @Task)               
BEGIN           
    INSERT INTO [ABSTest].[dbo].[Preventivo]
           ([Progressivo] 
           ,[Quantita] 
           ,[Costo]
           ,[CostoStandard]
           ,[Tariffa]
           ,[SpeseStandard]
           ,[FkCodiceEnumTipoPreventivo]
           ,[FkCodiceEnumFiguraProfessionale]
           ,[FKCodiceRisorsa]
           ,[FkCodiceCommessa]
           ,[FkCodiceOrdineDiLavoro]
           ,[Task])
     VALUES
           (@Progressivo
           ,@Durata
           ,@Costo
           ,@CostoStandard 
           ,@Tariffa
           ,@SpeseStandard 
           ,@Codice_Tipo 
           ,@Codice_FiguraProfessionale
           ,@Codice_Risorsa 
           ,@Codice_Commessa 
           ,@Codice_ODL
           ,@Task)
 END
 ELSE
 BEGIN
    RAISERROR 50001 'ERRORE: RECORD GIà PRESENTE'
 END
END

这很好用,但是所有值(除了主键)都是Nullable,所以如果我把它作为参数Null值,它就不起作用。我怎样才能设计它与Nulls 一起工作?

4

5 回答 5

2

您可以使用以下代码进行检查:

IF NOT EXISTS   (SELECT * FROM Preventivo WHERE ISNULL(Progressivo,'') = ISNULL(@Progressivo,'') 
        AND ISNULL(Quantita,0) = ISNULL(@Durata,0) 
        AND ISNULL(Costo,0) = ISNULL(@Costo,0)
        AND ISNULL(CostoStandard,0) = ISNULL(@CostoStandard,0)
        AND ISNULL(Tariffa,0) = ISNULL(@Tariffa,0)
        AND ISNULL(SpeseStandard,0) = ISNULL(@SpeseStandard,0)
        AND ISNULL(FkCodiceEnumTipoPreventivo,'') = ISNULL(@Codice_Tipo,'')
        AND ISNULL(FkCodiceEnumFiguraProfessionale,'') = ISNULL(@Codice_FiguraProfessionale,'')
        AND ISNULL(FKCodiceRisorsa,'') = ISNULL(@Codice_Risorsa,'') 
        AND ISNULL(FkCodiceCommessa,'') = ISNULL(@Codice_Commessa,'')
        AND ISNULL(FkCodiceOrdineDiLavoro,'') = ISNULL(@Codice_ODL,'')
        AND ISNULL(Task,'') = ISNULL(@Task,'') )
于 2013-01-07T12:13:50.933 回答
1

使用 ISNULL 作为比较运算符。可能会慢一点

SELECT * FROM Preventivo WHERE ISNULL(Progressivo, '') = ISNULL(@Progressivo, '')
于 2013-01-07T11:06:21.917 回答
1

您可以使用 Case 语句来实现这一点

例如

AND Quantita = CASE @Durata
WHEN null THEN Quantita
ELSE @Durata

因此,当 @Durata==null 案例将返回列 Quantita 本身时,结果将为真(Quantita =Quantita )

于 2013-01-07T11:16:16.483 回答
0

您的存储过程正在尝试做

NULL = NULL

更改存储过程的第一条语句,它应该可以工作

SET ANSI_NULLS OFF;

如果您使用的是 SQL 2008 R2 或更高版本,请考虑使用 MERGE 语句。

拉吉

于 2013-01-07T11:10:54.473 回答
0

对于 null 你需要把条件

where column_name  is null

在您的事业中,您可以IsNull()像这样使用 sql server 中可用的功能

 where IsNull(column_name,-1) = Isnull(@variable,-1)

将变量的值与上面给出的列进行比较,轻松解决问题

于 2013-01-07T11:04:52.703 回答