开发者

sql delete statement

I have a .cs file, in which I'm looking into a db and a site for specific IDs. And if the ID on the site is not in db (yet) it saves it into db. Easy as that.

But what if the ID is i开发者_如何学编程n the db and NOT on the page. How to check for that. And how to delete it if its not on the site anymore.

I hope I was clear enough.

Thanks

Sebastian


If I'm understanding you correctly, you would need to do a pre-check. So, assuming that from the FB wall you have the wall's 'master' id and you relate all posts in the wall to that id in your database, you first grab all post ids for the wall and check against your database. If they exist and if you have one in your database that is no longer on the wall, simply delete it. Then move on with your regular insertion.

Say, all of your data looks something like this:

FBWall {
    WallId  1,
    Posts = Posts[4] {
        Post { PostId = 1 },
        Post { PostId = 2 },
        Post { PostId = 4 },
        Post { PostId = 5 }
    }
}

DBWall {
    WallId = 1,
    Posts = Posts[4] {
        Post { PostId = 1 },
        Post { PostId = 2 },
        Post { PostId = 3 },
        Post { PostId = 4 }
    }
}

In the FBWall you have 4 posts with ids 1,2,4,5; in the DBWall you have 4 posts with ids 1,2,3,4. The difference is that post 3 from FBWall is gone so you need to delete it from DBWall. Also in FBWall you now have post 5 which is not in DBWall so you need to insert it.

Now that I think about it, the order of operations is irrelevant. You can either insert your new posts in the database first and then delete obsolete posts, or vice-versa. The end result is the same.

Hope this helps you a little.


SQl Server 2008 has a MERGE statment, I suggest you look at it, you can in one statement delte bad records and update existing and insert new. It's pretty cool. Loot at it in Books Online'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜