开发者

update statement in a stored procedure using temp table and real table

I've found many other posts here on how to do this but unsure how to write the initial select statement which checks for the system name and the update statement since there is a temp table involved. I'm very green to working with stored procedures let alone temp tables so I'm at a loss. What I understand to be happening is data is fed to the SP via an XML feed (that step is not shown here). The data from the xml feed is then stored in a temp table. As the SP is written right now, the SP inserts the data from the temp table into the real table. I need to add a step that checks to see if the system name exists and if it does, update it and if it doesn't, then insert it. I need help with the IF EXISTS select statement and the update query, please.

Here is the original statement which only consists of an insert.

original statement

insert into si_systemdetail(projectname, systemname, contactsbcuid, sdverifier, systemtype, location, proposedlivedate,  status, bkpsa, pripsa,platform)
   select 
        @project, systemname, contactsbcuid, sdverifier,systemtype, location, 
        proposedlivedate, 'Initial', bkpsa, pripsa, @platform
   from @systemInfo 
   where status in ('Production','Production w/o Appl') 
     and systemname not in (select systemname from si_systemdetail) 
     and @project is not null`

updated statement

IF EXISTS (select systemname from si_systemdetail WHERE systemname = (select systemname from @systemInfo where systemname in (select systemname from si_systemdetail) and @project is not null))  
BEGIN  
    -- update query
    UPDATE si_systemdetail
        SET  **I DO NOT KNOW HOW TO WRITE THIS SECTI开发者_如何学CON**
    WHERE   
        systemname IN (select systemname from si_systemdetail)  
        AND @project is not null
END
ELSE
BEGIN
    -- Write your insert query
    insert into si_systemdetail(projectname, systemname, contactsbcuid, sdverifier, 
                systemtype, location, proposedlivedate,  status, bkpsa, pripsa, platform)
       select 
            @project, systemname, contactsbcuid, sdverifier,systemtype, location, 
            proposedlivedate, 'Initial', bkpsa, pripsa, @platform
       from @systemInfo 
       where status in ('Production','Production w/o Appl') 
         and systemname not in (select systemname from si_systemdetail) 
         and @project is not null
END


You can save one query by simply attempting to run the UPDATE and then checking @@ROWCOUNT. If it is 0, you can try the INSERT. In SQL Server 2008 you could replace this messy logic with MERGE.

UPDATE d
    SET d.projectname   = i.projectname,
        d.contactsbcuid = i.contactsbcuid,

        -- other columns here

        d.[platform]    = @platform
FROM
    dbo.si_systemdetail AS d
    INNER JOIN @systemInfo AS i
    ON i.systemname     = d.systemname
WHERE
    i.[status] IN ('Production', 'Production w/o Appl')
    AND @project IS NOT NULL;

IF @@ROWCOUNT = 0
BEGIN
    insert into ...
END

Not clear what you want to do when there is a match on systemname but @project is NULL or the [status] is not in those two values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜