Monday, June 29, 2015

Thursday, June 18, 2015

SQL nested case when then

 CASE
WHEN alertcon.alert_type = 'E'
THEN
CASE
    WHEN alertgat.gateway_id = 'M' THEN escUser.login_id
    WHEN alertgat.gateway_id = 'E' THEN escUser.email_id
    ELSE ''
END
WHEN alertcon.alert_type = 'R'
THEN
CASE
   WHEN alertgat.gateway_id = 'M' THEN escUser.login_id
   WHEN alertgat.gateway_id = 'E' THEN escUser.email_id
    ELSE ''
END
END as Recepient

Monday, June 15, 2015

Static Cursors

A static cursor populates the result set at the time of cursor creation and query result is cached for the lifetime of the cursor. A static cursor can move forward and backward direction. A static cursor is slower and use more memory in comparison to other cursor. Hence you should use it only if scrolling is required and other types of cursors are not suitable.
No UPDATE, INSERT, or DELETE operations are reflected in a static cursor (unless the cursor is closed and reopened). By default static cursors are scrollable. SQL Server static cursors are always read-only.

  1. SET NOCOUNT ON
  2. DECLARE @Id int
  3. DECLARE @name varchar(50)
  4. DECLARE @salary int
  5. DECLARE cur_emp CURSOR
  6. STATIC FOR
  7. SELECT EmpID,EmpName,Salary from Employee
  8. OPEN cur_emp
  9. IF @@CURSOR_ROWS > 0
  10. BEGIN
  11. FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
  12. WHILE @@Fetch_status = 0
  13. BEGIN
  14. PRINT 'ID : '+ convert(varchar(20),@Id)+', Name : '+@name+ ', Salary : '+convert(varchar(20),@salary)
  15. FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
  16. END
  17. END
  18. CLOSE cur_emp
  19. DEALLOCATE cur_emp
  20. SET NOCOUNT OFF

The ‘NULL’ Debate, and a few other interesting facts

Printing Time variable and string on same line in SQL

print 'There are ' + CAST(@OfficeStartTime AS VARCHAR) + ' alias combinations did not match a record'

SQL case when then else

CASE WHEN ebv.db_no IN (22978,23218,23219) THEN 'WECS 9500' ELSE 'WECS 9520' END

SQL Nested cursors

-- nestedcursor 'dave'

ALTER procedure nestedcursor
@LoginId varchar(50)
as

begin
declare @FileId int = null
declare @Sender varchar(200) = null
declare @Recepient varchar(200) = null
declare @RecStatus char(1)
declare @IsProcessed char(1)
declare @ProcessedOn datetime = null
declare @EstProcessedOn datetime = null
declare @ProcessType char(1) = null
declare @TemplateId int = null
declare @SeqNum int = null
declare @OfficeStartTime time
declare @CurrentTime time
declare @Status BIT
declare @TemplateCode varchar(20) = null
declare @StageCode int
declare @AlertType varchar(1) = null
 
 select @OfficeStartTime = CONVERT(TIME(0), DATEADD(MINUTE, 60*office_start_time, 0)),
@CurrentTime = CONVERT(VARCHAR(8),GETDATE(),108)
 from mst_comp
 where comp_id = (select comp_id
from mst_user
where login_id = 'ABCA')

--print 'Cursor start'

declare @TrackLogCursor CURSOR

if @CurrentTime < @OfficeStartTime
begin

SET @TrackLogCursor = CURSOR
FOR

select 1 as [file_id],'Shekhar' as Sender,'Satish' as Recepient,'P' as rec_status,'N' as is_processed,ttracklog.tr_end_dt as processed_on,
  hdrtrack.tpt_code,ttracklog.stage_code,ttracklog.tr_seq_num
from   trn_track ttracklog join trn_file hdrtrack on (hdrtrack.tracking_num=ttracklog.tracking_num)
  join trn lead on (lead.lead_id=hdrtrack.lead_id and lead.rec_status='A')
where ttracklog.tr_date is null and CONVERT(DATE, ttracklog.tr_end_dt) = CAST(CURRENT_TIMESTAMP AS DATE)
order by ttracklog.tracking_num desc


end
  else if @CurrentTime > @OfficeStartTime
begin

SET @TrackLogCursor = CURSOR
FOR

select 1 as [file_id],'Shekhar' as Sender,'Satish' as Recepient,'P' as rec_status,'N' as is_processed,ttracklog.tr_end_dt as processed_on,
  hdrtrack.tpt_code,ttracklog.stage_code,ttracklog.tr_seq_num
from   trn_track ttracklog join trn_file hdrtrack on (hdrtrack.tracking_num=ttracklog.tracking_num)
  join trn lead on (lead.lead_id=hdrtrack.lead_id and lead.rec_status='A')
where  ttracklog.tr_date is null and CONVERT(DATE, ttracklog.tr_end_dt) = CAST(CURRENT_TIMESTAMP AS DATE) and CAST(ttracklog.lastupd_stamp AS TIME) > @OfficeStartTime
order by ttracklog.tracking_num desc
   
end



--print 'Open Cursor'

OPEN @TrackLogCursor

FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@TemplateCode,@StageCode,@SeqNum

WHILE @@FETCH_STATUS = 0

BEGIN

begin

declare @AlertTypeCursor CURSOR
SET @AlertTypeCursor = CURSOR
FOR

select alertconfig.alert_type ,'' ,mgateway.template_id,alertgatway.gateway_id
from   mst_stage links
  left outer join mst_stage_link alertconfig on(links.stage_link_id=alertconfig.stage_link_id and alertconfig.rec_status='A')
  left outer join mst_stage_link_gat alertgatway on (alertgatway.alert_config_id=alertconfig.alert_config_id and alertgatway.rec_status='A')
  left outer join mst_alert_ga mgateway on (mgateway.gateway_code=alertgatway.gateway_id and mgateway.rec_status='A')
  left outer join mst_alert_t malert on (malert.alert_type_code=alertconfig.alert_type and malert.rec_status='A')
where links.tpt_cd = @TemplateCode and links.next_stage = @StageCode
order by links.stage_code,links.next_stage

OPEN @AlertTypeCursor

FETCH NEXT FROM @AlertTypeCursor INTO @AlertType,@EstProcessedOn,@TemplateId,@ProcessType

WHILE @@FETCH_STATUS = 0

BEGIN

print @TemplateCode
print @StageCode


FETCH NEXT FROM @AlertTypeCursor INTO @AlertType,@EstProcessedOn,@TemplateId,@ProcessType

END

CLOSE @AlertTypeCursor

DEALLOCATE @AlertTypeCursor


end  


FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@TemplateCode,@StageCode,@SeqNum

END

CLOSE @TrackLogCursor

DEALLOCATE @TrackLogCursor

--start cursor for insert update record in table

end

Friday, June 12, 2015

SQL Cursors

SQL cursor example 2


-- [CRM].[insert] 'dave'

Alter procedure [CRM].[insert]
@LoginId varchar(50)
as

begin
declare @FileId int = null
declare @Sender varchar(200) = null
declare @Recepient varchar(200) = null
declare @RecStatus char(1)
declare @IsProcessed char(1)
declare @ProcessedOn datetime = null
declare @EstProcessedOn datetime = null
declare @ProcessType char(1) = null
declare @TemplateId int = null
declare @SeqNum int = null
declare @OfficeStartTime time
declare @CurrentTime time
declare @Status BIT
 
 select @OfficeStartTime = CONVERT(TIME(0), DATEADD(MINUTE, 60*office_start_time, 0)),
@CurrentTime = CONVERT(VARCHAR(8),GETDATE(),108)
 from mst_comp
 where comp_id = (select comp_id
from mst_user
where login_id = 'ABCA')

--print 'Cursor start'

declare @TrackLogCursor CURSOR

if @CurrentTime < @OfficeStartTime
begin

SET @TrackLogCursor = CURSOR
FOR

select 1 as [file_id],'Shekhar' as Sender,'Satish' as Recepient,'P' as rec_status,'N' as is_processed,tl.tr_end_dt as processed_on,
  tl.tr_end_dt as est_processed_on,'S' as process_type,1 as template_id,tl.tr_seq_num
from trn_track tl
where tl.tr_date is null and CONVERT(DATE, tl.tr_end_dt) = CAST(CURRENT_TIMESTAMP AS DATE)

end
  else if @CurrentTime > @OfficeStartTime
begin

SET @TrackLogCursor = CURSOR
FOR

select 1 as [file_id],'Shekhar' as Sender,'Satish' as Recepient,'P' as rec_status,'N' as is_processed,tl.tr_end_dt as processed_on,
  tl.tr_end_dt as est_processed_on,'S' as process_type,1 as template_id,tl.tr_seq_num
from trn_track tl
where tl.tr_date is null and CONVERT(DATE, tl.tr_end_dt) = CAST(CURRENT_TIMESTAMP AS DATE)
 and CAST(lastupd_stamp AS TIME) > @OfficeStartTime
   
end



--print 'Open Cursor'



OPEN @TrackLogCursor

FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,
@SeqNum

WHILE @@FETCH_STATUS = 0

BEGIN

begin


IF EXISTS  (SELECT  1
FROM trn_alert
WHERE tracking_seq_num = @SeqNum and rec_status = 'P')
Update trn_alert
set est_processed_on = @EstProcessedOn,
updated_on = GETDATE()
where tracking_seq_num = @SeqNum

ELSE
 insert into trn_alert([file_id],Sender,Recepient,rec_status,is_processed,processed_on,est_processed_on,process_type,template_id,
               updated_by,updated_on,tracking_seq_num)
 values(@FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,@LoginId,GETDATE(),@SeqNum)

end  


FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,
@SeqNum

END

CLOSE @TrackLogCursor

DEALLOCATE @TrackLogCursor

--start cursor for insert update record in table

end

Tuesday, June 9, 2015

SQL EXISTS CONDITION

select *
 from trn_log tl
 where Exists( select 1
 from trn_queue aq
 where aq.track_num=tl.track_num and aq.code=tl.code
 )

SQL delete mutiple rows from table


  delete from table_name
  where id in (968,969,970)

SQL cursor example

-- [insert] 'ex'
create procedure[insert]

@LoginId varchar(50)

as

begin

-- DECLARE @iErrorDetailId INT                        

-- BEGIN TRANSACTION                                

-- BEGIN TRY

declare @FileId int = null

declare @Sender varchar(200) = null

declare @Recepient varchar(200) = null

declare @RecStatus char(1)

declare @IsProcessed char(1)

declare @ProcessedOn datetime = null

declare @EstProcessedOn datetime = null

declare @ProcessType char(1) = null

declare @TemplateId int = null

declare @StartDate datetime = null

declare @EndDate datetime = null

declare @TrackingNum int = null

declare @StageCode int

--print 'Cursor start'

declare @TrackLogCursor CURSOR

SET @TrackLogCursor = CURSOR

FOR

 select 1 as [file_id],'Shekhar' as Sender,'Satish' as Recepient,'P' as rec_status,'N' as is_processed,tl.tr_end_dt as processed_on,
 tl.tr_end_dt as est_processed_on,'S' as process_type,1 as template_id,tl.tr_start_dt as tr_start_dt,tl.tr_end_dt as tr_end_dt,
 tl.tracking_num as tracking_num,tl.stage_code as stage_code
 from tralog tl left outer join
  aleque aq on aq.tracking_num <> tl.tracking_num and aq.stage_code <> tl.stage_code
 where tl.tr_date is null and tl.tr_end_dt = CAST(CURRENT_TIMESTAMP AS DATE)
 order by tl.tr_end_dt

--print 'Open Cursor'

OPEN @TrackLogCursor

FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,
@StartDate,@EndDate,@TrackingNum,@StageCode

WHILE @@FETCH_STATUS = 0

BEGIN

begin

insert into aleque ([file_id],Sender,Recepient,rec_status,is_processed,processed_on,est_processed_on,process_type,template_id,
tr_start_dt,tr_end_dt,tracking_num,stage_code,updated_by,updated_on)
values(@FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,@StartDate,@EndDate,
  @TrackingNum,@StageCode,@LoginId,GETDATE())

end  

FETCH NEXT FROM @TrackLogCursor INTO @FileId,@Sender,@Recepient,@RecStatus,@IsProcessed,@ProcessedOn,@EstProcessedOn,@ProcessType,@TemplateId,
@StartDate,@EndDate,@TrackingNum,@StageCode

END

CLOSE @TrackLogCursor

DEALLOCATE @TrackLogCursor

--start cursor for insert update record in table

--COMMIT TRANSACTION      

--END TRY      



--BEGIN CATCH      

--ROLLBACK TRANSACTION      

--EXEC [uspLog] @psSchemaName = NULL, @piErrorDetailId=@iErrorDetailId OUTPUT;      

--RAISERROR('Error: [CRM].[syncuserinfo]',-1,-1)      

--END CATCH
end

Thursday, June 4, 2015

asp.net call exe from web application

 Process p1 = new Process();
            try
            {
                //p1.StartInfo.WorkingDirectory = "C:\\Inetpub\\wwwroot"; //sets the working directory in which the exe file resides
                p1.StartInfo.WorkingDirectory = "C:\\Users\\shekhar\\Desktop\\ScanUtility"; //sets the working directory in which the exe file resides
                p1.StartInfo.FileName = "ScanUtility.exe";   // actual file name
                p1.StartInfo.Arguments = "D:\\Temp\\Pic_{0}";//arguments here i had taken hello as first argument and Murali as second argument and splited them in exe file
                p1.Start();
                p1.WaitForExit();
                if (p1.HasExited) p1.Dispose();
                else
                {
                    p1.Kill();
                }

            }
            catch (Exception e1) { e1.Message.ToString(); }

Tuesday, June 2, 2015

Get a list of Installed Software using vb or c# and the wmi

asp.net Scanner in Both TWAIN and WIA

1) Download Source code from - https://sites.google.com/site/eduardfilefepositore/

2) Open ScannerAdapterFactory.cs

3) Check Below Method and update according to your situation

 public static ScannerAdapterBase GetScannerAdapter(nessScanning control, IMessageFilter messageFilter, IntPtr handle)
        {
            lock (locker)
            {
                bool isWiaDeviceSelected = false;
                bool isTwainDeviceSelected = false;

                if (m_ScannerAdapterBase != null)
                {
                    return m_ScannerAdapterBase;
                }

                try
                {
                    //commented by shekhar 6/2/2015 - If you want to use WIA then uncomment this
                    //Checks WIA Devices
                    //m_ScannerAdapterBase = new WiaScannerAdapter();
                    //m_ScannerAdapterBase.InitAdapter(control, messageFilter, handle);
                    //isWiaDeviceSelected = m_ScannerAdapterBase.SelectDevice();
                    //if (isWiaDeviceSelected)
                    //{
                    //    return m_ScannerAdapterBase;
                    //}
                    //end of commented by shekhar 6/2/2015 - If you want to use WIA then uncomment this

                    //Checks TWAIN Devices
                    m_ScannerAdapterBase = new TwainScannerAdapter();
                    m_ScannerAdapterBase.InitAdapter(control, messageFilter, handle);
                    isTwainDeviceSelected = m_ScannerAdapterBase.SelectDevice();
                    if (isTwainDeviceSelected)
                    {
                        return m_ScannerAdapterBase;
                    }
                }
                catch (ScannerException ex)
                {
                    throw ex;
                }

                return null;
            }
        }