Tuesday, January 4, 2011

Migration sample

DECLARE @i INT ,@cnt INT ,@roleid INT,@companyid INT,@merchantId INT
SET @i=1
CREATE TABLE #tab (id INT IDENTITY,roleid INT  ,rolename VARCHAR(50),companyId INT, merchantid INT, isdefault BIT)
INSERT INTO #tab SELECT roleid,rolename,companyid,merchantid,isdefault FROM rolemaster
SELECT @cnt=COUNT(*) from #tab
WHILE(@i<=@cnt)
BEGIN
    SELECT @roleid=roleid,@companyid=companyid,@merchantId=merchantid FROM #tab WHERE id =@i

    IF @companyid is not null and @merchantId is not null
        BEGIN
            UPDATE rolemaster SET rolename=rolename+'_'+CAST(ISNULL(@companyid,'') AS VARCHAR(50))+'_'+CAST(ISNULL(@merchantId,'') AS VARCHAR(50)) WHERE roleid=@roleid and isdefault=1 and roleid >5
        END
    ELSE
        BEGIN
            UPDATE rolemaster SET rolename=rolename+'_'+CAST(ISNULL(@companyid,'') AS VARCHAR(50)) WHERE roleid=@roleid and isdefault=1 and roleid >5
        END
    SET @i=@i+1
END

No comments:

Post a Comment