if ( 0 = dbo.udf_Conv_DoesColumnExist ('PatchAgentConfigurations','DeviceGuid')) 
begin 

    DECLARE @debug BIT
	set @debug = 0

    --SELECT @@NESTLEVEL AS [SELECT @@NESTLEVEL]
	declare @tran int 
	set @tran = @@trancount


	declare @Script nvarchar(1000) 
	set @Script = '7480_102-MR3_PatchAgentConfigurations_ExpressOnly.sql'
    IF @debug = 1 PRINT @Script

	declare @Step   nvarchar(1000) 
	set @Step = 'Start'

	begin try
  
	if @tran = 0    
	BEGIN TRANSACTION   


        SET @Step = 'Alter Table dbo.PatchAgentConfigurations add DeviceGuid uniqueidentifier    NULL'
        IF @debug = 1 PRINT @Step
        --------------------
		exec('
		Alter Table dbo.PatchAgentConfigurations add DeviceGuid uniqueidentifier    NULL
		')

        

        SET @Step = 'UPDATE DeviceGuid'
        IF @debug = 1 PRINT @Step
        --------------------         
        EXEC('
             update trgt
			    set DeviceGuid = src.DeviceGuid
			   from dbo.PatchAgentConfigurations AS trgt
			   join dbo.Device                   as src    on trgt.DeviceID = src.DeviceID
        ')
       

	    SET @Step = 'Alter Table dbo.PatchAgentConfigurations alter column DeviceGuid uniqueidentifier  NOT null'
        IF @debug = 1 PRINT @Step
        --------------------
		exec('
		Alter Table dbo.PatchAgentConfigurations alter column DeviceGuid uniqueidentifier  NOT null
		')

	if @tran = 0  
		   COMMIT   
		---------------------------------
end try
---------------------
begin catch

    declare @ErrorMessage       nvarchar(max)
    set @ErrorMessage = [dbo].[udf_ErrorInfo_Script](@Script,@Step)

    if (XACT_STATE() != 0) 
	and @tran = 0  
		rollback transaction

    RaisError (@ErrorMessage,16,255)

end catch
end 
go








