Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use newid() to assign a value to a stored procedure variable?

I am trying to generate a new GUID and assign that value to NewReportID. But, I am unsure that if I replace @NewReportID everywhere in the procedure by newid() that it will generate a new GUID for each line.

What do I have to generate just one GUID and assign NewReportID the value of the GUID?

I have tried in the variable declaration: @NewReportID varchar(50)=newid() but that gave me lots of errors.

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
like image 687
Bulvak Avatar asked Jan 16 '23 06:01

Bulvak


1 Answers

Use the correct data type? uniqueidentifier

declare @NewReportID uniqueidentifier 
set @NewReportID = NEWID()

What flavour of sql-server are you using? I tried what you said was erroring:

I have tried in the variable declaration: @NewReportID varchar(50)=newid() but that gave me lots of errors.

But this works for me

enter image description here

like image 171
Chris Moutray Avatar answered Jan 31 '23 02:01

Chris Moutray