4

我正在尝试生成一个新的 GUID 并将该值分配给NewReportID. 但是,我不确定如果我替换@NewReportID过程中的任何地方newid(),它将为每一行生成一个新的 GUID。

我必须生成一个 GUID 并为 NewReportID 分配 GUID 的值?

我在变量声明中尝试过:@NewReportID varchar(50)=newid()但这给了我很多错误。

ALTER PROCEDURE [dbo].[AmendInsertDuplicateFields] (@ReportID varchar(50), @NewReportID varchar(50))
AS

    Begin           

INSERT INTO [MVCOmar].[dbo].[PrideMVCCollisionBegin]
  ([ReportID], [LocalIncidentNum], [version], [MTOReferenceNo], [Submitted])
SELECT @NewReportID, [LocalIncidentNum], [version], [MTOReferenceNo], [Submitted]
FROM [MVCOmar].[dbo].[PrideMVCCollisionBegin] WHERE [ReportID]=@ReportID;

INSERT INTO [MVCOmar].[dbo].[PrideMVCCollisionDetails] ([Classification]      ,
    [ReportType]      ,[CollisionDate]      ,[CollisionDay]      ,
    [CollisionTime]      ,[CollisionLoc]      ,[ImpactLoc]      ,[ThruLaneNo]      ,
    [Weather1]      ,[Weather2]      ,[Light]      ,[TrafficControl]      ,
    [TrafficControlCond]      ,[RoadChar1]      ,[RoadChar2]      ,
    [RoadSurface1]      ,[RoadSurface2]      ,[RoadCond1]      ,[RoadCond2]      ,
    [RoadSurfaceCond1]      ,[RoadSurfaceCond2]      ,[RoadAlignment1]      ,
    [RoadAlignment2]      ,[RoadPavementMarking1]      ,[RoadPavementMarking2]      ,
    [OtherCollisionLoc]      ,[OtherImpactLoc]      ,[OtherWeather1]      ,
    [OtherWeather2]      ,[OtherLight]      ,[OtherTraffic]      ,
    [OtherRoadSurface1]      ,[OtherRoadSurface2]      ,[OtherRoadSurfaceCond1]      ,
    [OtherRoadSurfaceCond2]      ,[OtherClassification]      ,
    [DiagramDescription]      ,[R1NumLanes]      ,[R1MaxSpeed]      ,
    [R1AdviseSpeed]      ,[R2NumLanes]      ,[R2MaxSpeed]      ,[R2AdviseSpeed]      ,
    [NumInvolved]      ,[OfficerID]      ,[Checked]      ,[LastModified]      ,
    [LastModifiedBy]      ,[StartTime]      ,[EndTime]      ,[Display]      ,
    [ReportID]      ,[InitialImpactType]      ,[OtherInitialImpactType]      ,
    [SelfReported])
SELECT [Classification]      ,[ReportType]      ,[CollisionDate]      ,
    [CollisionDay]      ,[CollisionTime]      ,[CollisionLoc]      ,[ImpactLoc]      ,
    [ThruLaneNo]      ,[Weather1]      ,[Weather2]      ,[Light]      ,
    [TrafficControl]      ,[TrafficControlCond]      ,[RoadChar1]      ,
    [RoadChar2]      ,[RoadSurface1]      ,[RoadSurface2]      ,[RoadCond1]      ,
    [RoadCond2]      ,[RoadSurfaceCond1]      ,[RoadSurfaceCond2]      ,
    [RoadAlignment1]      ,[RoadAlignment2]      ,[RoadPavementMarking1]      ,
    [RoadPavementMarking2]      ,[OtherCollisionLoc]      ,[OtherImpactLoc]      ,
    [OtherWeather1]      ,[OtherWeather2]      ,[OtherLight]      ,
    [OtherTraffic]      ,[OtherRoadSurface1]      ,[OtherRoadSurface2]      ,
    [OtherRoadSurfaceCond1]      ,[OtherRoadSurfaceCond2]      ,
    [OtherClassification]      ,[DiagramDescription]      ,[R1NumLanes]      ,
    [R1MaxSpeed]      ,[R1AdviseSpeed]      ,[R2NumLanes]      ,[R2MaxSpeed]      ,
    [R2AdviseSpeed]      ,[NumInvolved]      ,[OfficerID]      ,[Checked]      ,
    [LastModified]      ,[LastModifiedBy]      ,[StartTime]       

    End
4

3 回答 3

4

使用正确的数据类型?uniqueidentifier

declare @NewReportID uniqueidentifier 
set @NewReportID = NEWID()

您使用的是什么风格的 sql-server?我试过你说的错误:

我在变量声明中尝试过:@NewReportID varchar(50)=newid() 但这给了我很多错误。

但这对我有用

在此处输入图像描述

于 2012-06-27T12:14:02.500 回答
3

您使用 a 有什么原因VARCHAR()吗?

理想情况下,您应该使用该uniqueidentifier类型。

DECLARE
  @myGUID    uniqueidentifier
SELECT
  @myGUID    = NewID()


编辑

由于您对 a感兴趣VARCHAR(),因此您可以将其转换为字符串。这比任何真正的技术问题都更浪费空间......

DECLARE
  @myGUID    VARCHAR(50)
SELECT
  @myGUID    = CONVERT(varchar(50), @myid)

在您的 SP 中,@newReportID 是一个输入参数。因此,您可以将 SP 更改为不需要输入,也可以在输入之前设置它


使用当前的 SP...

DECLARE
  @myGUID    VARCHAR(50)
SELECT
  @myGUID    = CONVERT(varchar(50), @myid)

EXEC [dbo].[AmendInsertDuplicateFields] @reportID, @myGUID


改变 SP...

ALTER PROCEDURE [dbo].[AmendInsertDuplicateFields] (@ReportID varchar(50))
AS
BEGIN

DECLARE
  @NewReportID VARCHAR(50)
SELECT
  @NewReportID = CONVERT(varchar(50), @myid)

-- Then use @NewReportID onwards in the SP

并且只用一个参数调用 SP...

EXEC [dbo].[AmendInsertDuplicateFields] @reportID

http://msdn.microsoft.com/en-us/library/ms190348.aspx

于 2012-06-27T12:15:28.920 回答
2
   DECLARE @NewReportID VARCHAR(50)
   SET @NewReportID = cast(NEWID() AS VARCHAR(50))
于 2012-06-27T12:10:38.177 回答