0

我正在编写一个存储过程,但我无法弄清楚编译器根本不会给我任何错误规范。只是它说查询完成但有错误。我看不到错误..?!

这将用作更新两个表的存储过程,代码本身非常不言自明。

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[insertRankingData]
@domannamn [varchar](100),
@keyword [varchar](100),
@dagensdatum [Date],
@rankingposition [Decimal]
AS
DECLARE @domanID int 
DECLARE @datumen date 

IF(SELECT COUNT(*) FROM [dbo].[t_doman] WHERE doman_namn = @domannamn) = 0
BEGIN
INSERT INTO [dbo].[t_doman] (doman_namn) 
    VALUES(@domannamn)

    SELECT @domanID = SCOPE_IDENTITY()
SELECT @todaysdate = GETDATE()
END
ELSE
BEGIN
SELECT @domanID = [doman_id] 
    FROM [dbo].[t_doman] 
    WHERE doman_namn = @domannamn

SELECT @todaysdate = GETDATE()
END

IF NOT @domanID IS NULL
BEGIN 
IF NOT EXISTS (SELECT (ranking_date, ranking_keyword, ranking_id_doman) 
                    FROM [dbo].[t_ranking] 
                    WHERE ranking_id_doman = @domanID
                    AND ranking_keyword = @keyword
                    AND ranking_date = @datumen)
BEGIN

       INSERT INTO [dbo].[t_ranking] (ranking_id_doman, ranking_date, ranking_position, ranking_keyword) 
       VALUES(@domanID, @todaysdate, @rankingposition, @keyword)    
END
ELSE
BEGIN 
       UPDATE [dbo].[t_ranking] 
       SET ranking_position = @rankingposition
       WHERE ranking_date = @todaysdate
       AND ranking_keyword = @keyword
       AND ranking_id_doman = @domanID 
END
  END
4

1 回答 1

1

尝试这个:

ALTER PROCEDURE [dbo].[insertRankingData]
@domannamn [varchar](100),
@keyword [varchar](100),
@dagensdatum [Date],
@rankingposition [Decimal]
AS
DECLARE @domanID int 
DECLARE @datumen date 
IF EXISTS(SELECT 1 FROM [dbo].[t_doman] WHERE doman_namn = @domannamn)
BEGIN
INSERT INTO [dbo].[t_doman] (doman_namn) VALUES(@domannamn)
SELECT @domanID = SCOPE_IDENTITY()
SELECT @todaysdate = GETDATE()
END
ELSE
BEGIN
SELECT @domanID = [doman_id] FROM [dbo].[t_doman] WHERE doman_namn = @domannamn
SELECT @todaysdate = GETDATE()
END
IF @domanID IS NOT NULL
BEGIN 
IF NOT EXISTS (SELECT ranking_date, ranking_keyword, ranking_id_doman
                    FROM [dbo].[t_ranking] 
                    WHERE ranking_id_doman = @domanID
                    AND ranking_keyword = @keyword
                    AND ranking_date = @datumen)
BEGIN
INSERT INTO [dbo].[t_ranking] (ranking_id_doman, ranking_date, ranking_position, ranking_keyword) 
    VALUES(@domanID, @todaysdate, @rankingposition, @keyword)   
END
ELSE
BEGIN 
    UPDATE [dbo].[t_ranking] 
    SET ranking_position = @rankingposition
    WHERE ranking_date = @todaysdate
    AND ranking_keyword = @keyword
    AND ranking_id_doman = @domanID 
END
  END

希望这可以帮助!!

于 2012-11-01T19:38:29.107 回答