SELECT COUNT(DISLABBARKODNO) AS SAYI1 FROM HESAPISL WHERE (DISLABBARKODNO IS NOT NULL) and (DISLABBARKODNO <> '0')

IF NOT EXISTS (SELECT * FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSOBJECTS.id = SYSCOLUMNS.id 
              WHERE SYSCOLUMNS.NAME = 'DISLABBARKODNO2' AND SYSOBJECTS.name = 'HESAPISL')
BEGIN
 ALTER TABLE HESAPISL ADD DISLABBARKODNO2 NVARCHAR(15) NULL
END
GO

IF EXISTS (SELECT * FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSOBJECTS.id = SYSCOLUMNS.id 
              WHERE SYSCOLUMNS.NAME = 'DISLABBARKODNO2' AND SYSOBJECTS.name = 'HESAPISL')
BEGIN
	UPDATE C SET C.DISLABBARKODNO2 = O.DISLABBARKODNO
	FROM HESAPISL C 
	JOIN HESAPISL O ON C.HESAPISLID = O.HESAPISLID
	WHERE (O.DISLABBARKODNO IS NOT NULL) AND (O.DISLABBARKODNO <> '0')
END
GO

IF NOT EXISTS
(
	SELECT X.* FROM 
	(
	SELECT COUNT(DISLABBARKODNO) AS SAYI1 FROM HESAPISL WHERE (DISLABBARKODNO IS NOT NULL) and (DISLABBARKODNO <> '0')
	EXCEPT
	SELECT COUNT(DISLABBARKODNO2) AS SAYI2 FROM HESAPISL WHERE (DISLABBARKODNO2 IS NOT NULL) and (DISLABBARKODNO2 <> '0')
	)X
) 
BEGIN
	ALTER TABLE HESAPISL DROP COLUMN DISLABBARKODNO
	EXEC sp_RENAME 'HESAPISL.DISLABBARKODNO2', 'DISLABBARKODNO', 'COLUMN'
END

SELECT COUNT(DISLABBARKODNO) AS SAYI1 FROM HESAPISL WHERE (DISLABBARKODNO IS NOT NULL) and (DISLABBARKODNO <> '0')

GO