需要创建存储过程但有问题

时间:2012-06-06 19:03:08

标签: c# sql sql-server stored-procedures

我不确定为什么下面的代码在我尝试创建存储过程时会出错。我对SP相对缺乏经验。

CREATE PROCEDURE [dbo].[InsertDuplicateFields] 
    (@UniqueColID varchar(50), @IndividualID varchar(50) = null)
AS
Begin           
    INSERT INTO TableCollisionBegin 
        SELECT * FROM TableCollisionBegin 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableCollisionDetails 
        SELECT * FROM TableCollisionDetails 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableCollisionLocation 
        SELECT * FROM TableCollisionLocation 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableDriver 
        SELECT * FROM TableDriver 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableFollowUp 
        SELECT * FROM TableFollowUp 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableOfficerLogs 
        SELECT * FROM TableOfficerLogs 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TablePolice 
        SELECT * FROM TablePolice 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableRecVerified 
        SELECT * FROM TableRecVerified 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableSignature 
        SELECT * FROM TableSignature 
        WHERE [IndividualID] = @IndividualID;

    INSERT INTO TableTrailer 
        SELECT * FROM TableTrailer 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableValidateLog 
        SELECT * FROM TableValidateLog 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableVehicle 
        SELECT * FROM TableVehicle 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableWitness 
        SELECT * FROM TableWitness 
        WHERE [UniqueColID] = @UniqueColID;

    INSERT INTO TableInvolvedPerson 
        SELECT * FROM TableInvolvedPerson 
        WHERE [IndividualID] = @IndividualID;

    INSERT INTO TableStatement 
        SELECT * FROM TableStatement 
        WHERE [UniqueColID] = @UniqueColID; 

    INSERT INTO TableCollisionDiagram 
        SELECT * FROM TableCollisionDiagram 
        WHERE [UniqueColID] = @UniqueColID;
END

我得到的错误是:

  

第21行无效的列名称'IndividualID'。

如果我删除插入individualID的代码行,那么它会对UniqueColID

给出相同的错误

这很奇怪,因为在给定的表中有一列IndividualID所以我不知道为什么它不会识别它....我在MS Excel中键入此代码然后将其粘贴到记事本中,这可能是代码无效的原因吗?

我从该表中复制了一行,你可以看到它有IndividualID

IndividualID
059D1263-F0F3-4D19-8C56-0FC7D2B5266E

4 个答案:

答案 0 :(得分:3)

为什么在参数周围使用单引号?这两个参数都是varchar,你不需要这样做。

答案 1 :(得分:1)

仔细检查您的表TableInvolvedPerson并确保列IndividualID的拼写相同。

<强>更新

更新问题后,您为IndividualID添加了值。您添加的值是Guid,它可能作为UNIQUEIDENTIFIER列存储在SQL Server中。但是您在存储过程中声明的类型是VARCHAR。你能仔细检查你的类型吗?在原始表和您要复制的表上,IndividualID的类型是什么?他们匹配吗?

答案 2 :(得分:1)

请仔细检查一下看起来的某个奇怪的Unicode字符,就像你期望的那样,但不是。

我还会在每个语句后添加错误检查,以便在其中一个语句失败时获得合理的错误消息;然后,如果需要全部或全无更新,您还可以回滚事务。

答案 3 :(得分:0)

请记住像这样的查询

INSERT INTO `a` SELECT * FROM `a` WHERE `c`=null

无论价值是多少,都不会插入任何内容。如果c中的数据为空,则必须使用IS NULL从不 =null