如何通过存储过程检查表日历中是否已存在park_id?

时间:2011-05-19 13:49:43

标签: vb.net sql-server-2008

存储过程

ALTER PROCEDURE [dbo].[KalenderVullen]
@park_id numeric,
@startdate Datetime 

AS
BEGIN

;WITH Dates AS (
        SELECT DATEADD(day,DATEDIFF(day,0,@startdate),0) as DateOf,
            CASE WHEN datename(weekday,@startdate) IN ('Saturday','Sunday') THEN 'Weekend'
                 ELSE 'Week' 
            END DayType,
            datename(weekday,@startdate) DayOfWeekName
        UNION ALL 
        SELECT DateOf+1,
            CASE WHEN datename(weekday,DateOf+1) IN ('Saturday','Sunday') THEN 'Weekend'
                 ELSE 'Week' 
            END DayType,
            datename(weekday,DateOf+1) DayOfWeekName
        FROM Dates
        where DateOf < dateadd(yy, 10, @startdate)


)

INSERT INTO  kalender (park_id,datum,week_weekend,naam_dag)
   SELECT @park_id,DateOf,DayType,DayOfWeekName
    FROM Dates
    OPTION (MAXRECURSION 4000)
    SELECT @@IDENTITY AS [@@IDENTITY]
    print @@IDENTITY
    set @park_id = @@IDENTITY




END
VB.NET(DAL)中的

代码

Public Sub kalender_insert(ByVal i_startdatum As Date, ByVal i_park_id As Int16)

    Dim nwindConn As SqlConnection = New SqlConnection(conStr)
    Dim adocmd As SqlCommand
    Dim adopara As New SqlParameter

    Dim mytrans As SqlTransaction

    nwindConn.Open()

    'begin van commitstructuur
    '‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
    mytrans = nwindConn.BeginTransaction(IsolationLevel.ReadCommitted)

    adocmd = New SqlCommand("KalenderVullen", nwindConn)
    adocmd.CommandType = CommandType.StoredProcedure

    adocmd.Parameters.Add(New SqlParameter("@startdate", SqlDbType.DateTime, 50)).Value = i_startdatum
    adocmd.Parameters("@startdate").Direction = ParameterDirection.Input

    adocmd.Parameters.Add(New SqlParameter("@park_id", SqlDbType.Int, 50)).Value = i_park_id
    adocmd.Parameters("@park_id").Direction = ParameterDirection.Input

    '‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
    adocmd.Transaction = mytrans
    'het committen wordt afhankelijk gemaakt van het slagen
    '‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
    Try
        adocmd.ExecuteNonQuery()
        mytrans.Commit()
    Catch ex As Exception
        mytrans.Rollback()
    End Try



    mytrans.Dispose()
    mytrans = Nothing
    adocmd.Dispose()
    adocmd = Nothing
    nwindConn.Close()
    nwindConn.Dispose()
    nwindConn = Nothing
    MessageBox.Show("kalender is aangemaakt")
End Sub
BLL中的

代码

????????????

2 个答案:

答案 0 :(得分:2)

如果您将@park_id作为参数输入,为什么要为@park_id指定新值?

始终使用SCOPE_IDENTITY

猜测,在SQL中尝试这个

INSERT INTO  kalender (park_id,datum,week_weekend,naam_dag)
SELECT @park_id,DateOf,DayType,DayOfWeekName
FROM Dates
WHERE NOT EXISTS (SELECT * FROM Dates WHERE park_id = @park_id
OPTION (MAXRECURSION 4000);

SELECT @park_id = SCOPE_IDENTITY();

答案 1 :(得分:0)

我不确定如果我理解正确,但也许你正在寻找:

IF EXISTS (SELECT TOP 1 1 FROM kalender WHERE park_id = @park_id) BEGIN
--- here comes you code
END

SELECT TOP 1 1是检查存在给定id的记录的有效形式(假设park_id列上有索引)。

@gbn在评论中指出不需要TOP 1 1。以下是我的理由,为什么它们有用:

  1. 内部查询可能会返回大量记录(取决于查询条件)。这可能会导致糟糕的查询执行计划 - 我不时会在MS SQL 2005上看到它。
  2. 重要的是我从磁盘读取了多少数据。使用*而不是常量也可能导致糟糕的查询计划。特别是如果DB认为有很多记录要返回。
  3. 当然,如果我按primary key列(或unique列)进行查询,则记录不应超过1条。在许多其他情况下 - 我不会指望查询优化器,所以我认为更准确地说出我需要的东西(在每种情况下)。我使用TOP 1 1EXISTS作为习语。