Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:08+00:00 2026-06-04T07:24:08+00:00

I have two tables: CREATE TABLE [NEWS] ( [ID] INT IDENTITY(1,1) NOT NULL, [TITLE]

  • 0

I have two tables:

CREATE TABLE [NEWS]
(
    [ID]      INT IDENTITY(1,1) NOT NULL,
    [TITLE]   VARCHAR(500) NULL,
    [CONTENT] VARCHAR(800) NULL,
    [CREATED] DATETIME DEFAULT(GETDATE())

    PRIMARY KEY ([ID])
)

CREATE TABLE [LOG]
(
    [ID]      INT IDENTITY(1,1) NOT NULL,
    [ACTION]  VARCHAR(500) NULL,
    [CREATED] DATETIME DEFAULT(GETDATE())

    PRIMARY KEY ([ID])
)

I want to do the following procedure:

I have an input parameter @NewsId.

STEP 1

  • If NewsId is NULL : I want to save the row into the table (NEWS) .
  • If newsid is defined then I want to update the row.

STEP 2

  • I want to do step 1 and then save the record into a table named LOG.
  • INSERT INTO LOG ("Action") VALUES ("insert or update")

How can I do these two steps using stored procedure?

How can I make one step after the successful completion and go to step 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:24:09+00:00Added an answer on June 4, 2026 at 7:24 am

    Here is a simple sample to get you going.

    create procedure MyProc (@NewsId int) as
    Begin
    
       -- you should really pass these in?
       declare @title   varchar(500) = 'A title'
       declare @content varchar(800) = 'A piece of content'
    
    
       if @NewsId is null
       begin
    
         Begin Try 
           insert into News (Title, Content)  values (@title, @content)
    
           -- get the new id just inserted
           set @NewsId = SCOPE_IDENTITY()
    
           insert into Log (Action) values ('insert')
         End Try
    
         Begin Catch
          .... handle error 
         end catch
    
       end
       else
       begin 
            update News set Title = @title, Content = @content
            where id = @NewsId
           insert into Log (Action) values ('update')
    
       end
    
    end
    

    from CodeProject:

    Begin Try 
     The_Query_for_which_we_need_to_do_the_ Error_Handling 
    End Try 
    Begin Catch 
    
      If there is some error in the query within the Try block, this flow 
      will be passed to this Catch block. 
    
    End catch 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: CREATE TABLE [dbo].[Task]( [SysTask] [int] IDENTITY(1,1) NOT NULL, [TaskStatus] [int]
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have two tables: CREATE TABLE 'sales_sheet' ( `_id` int(11) NOT NULL AUTO_INCREMENT, `_typed_by`
i have two tables: CREATE TABLE public.auctions ( id VARCHAR(255) NOT NULL, auction_value_key VARCHAR(255)
I have two tables: CREATE TABLE InmarsatZenith.dbo.ClientJob (JobRef int PRIMARY KEY NOT NULL, IntRef
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
I have two tables: CREATE TABLE dbo.country ( cntry_id VARCHAR(2) NOT NULL, name VARCHAR(50)
Ok, I have two MYSQL tables: CREATE TABLE `sessions` ( `id` int(11) NOT NULL
I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I have these two tables: CREATE TABLE x ( id INT NOT NULL, exclude

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.