Thursday, March 25, 2010

How to check if column exists in SQL Server table

CREATE FUNCTION ColumnExists(@TableName varchar(100), @ColumnName varchar(100))
RETURNS varchar(1) AS
BEGIN
DECLARE @Result varchar(1);
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)
BEGIN
SET @Result = 'T'
END
ELSE
BEGIN
SET @Result = 'F'
END
RETURN @Result;
END
GO

GRANT EXECUTE ON [ColumnExists] TO [whoever]
GO

USAGE:
IF ColumnExists('LG_107_SPECODES', 'MARKA') = 'F'
BEGIN
ALTER TABLE LG_107_SPECODES
ADD MARKA varChar(50) NOT NULL
END
GO

Thursday, March 11, 2010

Tiger2 Bordro Asgari Geçim İndirimi Hatası

Tiger2 Bordro programında Asgari Geçim İndirimde çıkan sorun için Aile bireyleri tablosunu kontrol ederek FAMILYHIST tablosuna kayıt açan SQL scripti.
Not: Bu scripti çalıştırmadan önce FAMILYHIST tablosunun yedeğini alınız.
Örnek yedek alma komutu SELECT * INTO LH_001_FAMILYHIST_20100310 FROM LH_001_FAMILYHIST


--Personel'e ait aile bireyleri girişini düzenler
DECLARE @FamilyLref INT
DECLARE @PersonLref INT
DECLARE @Support INT
DECLARE @LastSeq INT
DECLARE @IsAvail INT
DECLARE @Donem DATETIME
SET @Donem='20100201'
DECLARE processes CURSOR FOR
SELECT
dbo.LH_001_FAMILY.LREF AS FAMILY_LREF,
dbo.LH_001_PERSON.LREF AS PERSON_LREF,
dbo.LH_001_FAMILY.SUPPORTED AS SUPPORT
FROM dbo.LH_001_PERIDINF RIGHT OUTER JOIN
dbo.LH_001_FAMILY ON dbo.LH_001_PERIDINF.LREF = dbo.LH_001_FAMILY.IDREF RIGHT OUTER JOIN
dbo.LH_001_PERSON ON dbo.LH_001_FAMILY.PERREF = dbo.LH_001_PERSON.LREF
WHERE dbo.LH_001_PERSON.FIRMNR = 382
OPEN processes
FETCH NEXT FROM processes
INTO @FamilyLref,@PersonLref,@Support
WHILE @@FETCH_STATUS = 0
BEGIN
SET @IsAvail = 0
SELECT @IsAvail = LREF FROM [LH_001_FAMILYHIST] where [CARDREF] = @FamilyLref AND [BEGDATE] = @Donem
IF @IsAvail = 0
BEGIN
print @FamilyLref
SELECT @LastSeq = LASTLREF from LH_001_FAMILYHISTSEQ where ID=1
UPDATE dbo.LH_001_FAMILYHISTSEQ SET LASTLREF = @LastSeq + 1 WHERE ID=1
INSERT INTO [bordro_db].[dbo].[LH_001_FAMILYHIST] ([LREF],[BEGDATE],[ENDDATE],[CARDREF],[SUPPORTED],[INJUREDEGREE],[MINWGDISCSTAT],[ORGLOGICREF],[RECSTATUS],[SITEID])
VALUES(@LastSeq,@Donem,NULL,@FamilyLref,@Support,0,1,0,0,0)
END
FETCH NEXT FROM processes
INTO @FamilyLref,@PersonLref,@Support
END
CLOSE processes
DEALLOCATE processes