Inserting data in temporary table using union all


create table #temptable
    (
        UName varchar(1000) null,
        date datetime null,
        vchstatus varchar(100) null,
        ID int
        
    )
    
   
   
INSERT INTO #temptable (UName,date,vchstatus,id) 
 
select distinct isnull(users.vchFirstName,'Scrapper') + ' ' + isnull(users.vchLastName,'') as UName
,property_status.dtmdatecreated as date
,vchStatus
,'1'
from property_status (nolock)
left outer join users on property_status.intManualUpdateBy = users.intUserID
left outer join property_enumStatus es on property_status.tntstatusid = es.tntstatusid
where intpropertyid = @pintPropertyID
union all

SELECT ISNULL(u.vchFirstName, 'UNKNOWN') + ' ' + ISNULL(u.vchLastName, '') AS UName
,pdv.dtmmodhistory as date
, null as vchStatus
,'2'
FROM PropertyDeskValUpd_History pdv
LEFT OUTER JOIN users u ON u.intUserId = pdv.intModificationUser
where intpropertyid = @pintPropertyID and intOrderId = 1


union all

SELECT ISNULL(u.vchFirstName, 'UNKNOWN') + ' ' + ISNULL(u.vchLastName, '') AS UName
,pfvh.dtmmodhistory as date
, null as vchStatus
,'3'
FROM PropFieldVal_History pfvh
LEFT OUTER JOIN users u ON u.intUserId = pfvh.intModificationUser
where intpropertyid = @pintPropertyID and intOrderId = 1

select * 
from #temptable
order by date desc

Comments

Popular posts from this blog

TSQL To Get All the Names of tables and Databases whose data have changed in last 24 Hours

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases

Apply paging in procedure